943,648 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 7941
  • MS SQL RSS
Mar 27th, 2009
0

Return Boolean based on a condition in query

Expand Post »
Hi guys,

I've been programming for quite a while now and just jumped in to SQL and Stored Procedures due to a Uni project I have due in.

I have a table called tblWebsiteMember and tblWebsiteMember has 2 Columns - ID and Name

MS SQL Syntax (Toggle Plain Text)
  1. CREATE PROCEDURE CreateWebsiteMember
  2. @wbmName
  3. AS
  4. INSERT INTO tblWebsiteMember (wbmName)
  5. VALUES (@wbmName)

MS SQL Syntax (Toggle Plain Text)
  1. CREATE PROCEDURE GetWebsiteMember
  2. @wbmName
  3. AS
  4. SELECT Name
  5. FROM tblWebsiteMembers
  6. WHERE Name = @wbmName

To create a member I execute CreateWebsiteMember and give the website member a name, then to check if the website member was added successfully I execute GetWebsiteMember with the newly created members name, all works fine.

My project is web based and I would prefer using 1 query in place of 2. So I figured I needed a way to return data on the query as a kind of validation.

MS SQL Syntax (Toggle Plain Text)
  1. CREATE PROCEDURE CreateWebsiteMember
  2. @wbmName
  3. AS
  4. INSERT INTO tblWebsiteMember (wbmName)
  5. VALUES (@wbmName)
  6. SELECT 'True' AS Response
  7. FROM tblWebsiteMember
  8. WHERE UserName = @wbmUN

How do I return False in this example?

MS SQL Syntax (Toggle Plain Text)
  1. CREATE PROCEDURE CreateWebsiteMember
  2. @wbmName
  3. AS
  4. INSERT INTO tblWebsiteMember (wbmName)
  5. VALUES (@wbmName)
  6. IF @wbmName = (SELECT Name FROM tblWebsiteMember WHERE Name = @wbmName)
  7. RETURN 'True'
  8. else
  9. RETURN 'False'

A bit of a mish mash of languages here but I hope it gets the point across.

I cannot understand any learning resources out there for SQL, they seem to be very scare and complex compared to programming tutorials, but if anyone knows of a simple, similar tutorial for this, please do point me there.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Smithy963 is offline Offline
1 posts
since Dec 2008
Apr 2nd, 2009
0

Re: Return Boolean based on a condition in query

Add an additional parameter for your id, this will return the newly inserted record's id, otherwise if it failed an exception will be thrown and you don't need to check for its existence

MS SQL Syntax (Toggle Plain Text)
  1.  
  2. CREATE PROCEDURE CreateWebsiteMember
  3. (
  4. @wbmName varchar(50),
  5. @ID int OUTPUT
  6. )
  7. AS
  8. INSERT INTO tblWebsiteMember (wbmName)
  9. VALUES (@wbmName)
  10.  
  11. SET @ID = SCOPE_IDENTITY()
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Apr 2nd, 2009
0

Re: Return Boolean based on a condition in query

MS SQL Syntax (Toggle Plain Text)
  1. CREATE PROCEDURE CreateWebsiteMember
  2. (
  3. @wbmName varchar(50)
  4. )
  5. AS
  6. INSERT INTO tblWebsiteMember (Name) VALUES(@wbmName)
  7. SELECT ID AS 'Result' FROM tblWebsiteMember WHERE NAME = @wbmName

Now check 'Result' for NULL value in your web application. At least, that is how I would do it.

MS SQL Syntax (Toggle Plain Text)
  1. CREATE PROCEDURE CreateWebsiteMember
  2. (
  3. @wbmName varchar(50)
  4. )
  5. AS
  6. INSERT INTO tblWebsiteMember (Name) VALUES(@wbmName)
  7.  
  8. IF EXISTS (SELECT 1 FROM tblWebsiteMember WHERE Name = @wbmName)
  9. SELECT 'Result'=1
  10. ELSE
  11. SELECT 'Result'=0
Last edited by Stylish; Apr 2nd, 2009 at 6:39 pm.
Reputation Points: 44
Solved Threads: 19
Junior Poster
Stylish is offline Offline
148 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: how to “store” stored procedures
Next Thread in MS SQL Forum Timeline: sp_changedbowner question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC