Stefan Joseph
Stefan Joseph

Reputation: 63

Using an sql stored procedure for a yes no output

i have a database with

I would like to use a stored procedure so i can search the username and number together to return a simple 'yes' if they both match, or 'no' if they don't.

How would this be done in a stored procedure, i know there are easier ways but i need it in a stored procedure format.

Upvotes: 2

Views: 999

Answers (5)

Mitch Wheat
Mitch Wheat

Reputation: 300599

CREATE PROCEDURE dbo.IsExistingUser
     @username varchar(10)
    ,@usernumber varchar(10)
AS

IF EXISTS (SELECT 1 FROM tblUsers 
           WHERE username = @username AND usernumber = @usernumber) 
   SELECT 1
ELSE
   SELECT 0

GO

Upvotes: 4

Nithesh Narayanan
Nithesh Narayanan

Reputation: 11765

Try this stored procedure

 CREATE PROCEDURE [dbo].[UserCheck]
     @username varchar(10)
    ,@usernumber varchar(10)
AS
    SELECT (CASE (SELECT COUNT(*) FROM  tblUsers 
                        WHERE username=@username AND usernumber=@usernumber ) 
            WHEN 0 THEN 'No' 
            ELSE 'Yes' END)

Upvotes: 2

Joseph Le Brech
Joseph Le Brech

Reputation: 6653

Something like this

select case 
  (select id from usertable t where t.username = @username) = usernumber 
  then 
   1 
  else 
   0 
end

Upvotes: 1

Zenwalker
Zenwalker

Reputation: 1919

select count(usernumber) from tableX where unum = number if you recieve the count > 0 then you have a row. Then return 1 or 0 or wat ever you want.

Upvotes: 0

Martin
Martin

Reputation: 1536

you can just return a number from the stored procedure. 0 or 1.

If 1 then they match, if 0 they dont match

CREATE PROCEDURE [dbo].[YourProcedure] 
    @username varchar(10),
    @usernumber varchar(10)

AS
BEGIN
    SET NOCOUNT ON;

    if (Select Count(*) From YOURTABLE where username = @username and usernumber = @usernumber) > 0
    begin
        Select 1
    end
    else
    begin
        Select 0
    end

END

Upvotes: 1

Related Questions