Return Boolean based on a condition in query

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 1
Reputation: Smithy963 is an unknown quantity at this point 
Solved Threads: 0
Smithy963 Smithy963 is offline Offline
Newbie Poster

Return Boolean based on a condition in query

 
0
  #1
Mar 27th, 2009
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

  1. CREATE PROCEDURE CreateWebsiteMember
  2. @wbmName
  3. AS
  4. INSERT INTO tblWebsiteMember (wbmName)
  5. VALUES (@wbmName)

  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.

  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?

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,160
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Return Boolean based on a condition in query

 
0
  #2
Apr 2nd, 2009
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

  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()
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 146
Reputation: Stylish is an unknown quantity at this point 
Solved Threads: 14
Stylish's Avatar
Stylish Stylish is offline Offline
Junior Poster

Re: Return Boolean based on a condition in query

 
0
  #3
Apr 2nd, 2009
  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.

  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.
I NEED AN ADULT!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC