Groups | Blog | Home
all groups > sql server clients > february 2006 >

sql server clients : sqlquery question


Patrick F
2/15/2006 1:22:27 AM
How do you get the result from one store procedure?

i have first this procedure (CheckIfUserIsInDatabase):
.....
DECLARE @selectrowcount int

SELECT @selectrowcount = count(*) FROM Users WHERE Username = @Username

IF (@selectrowcount > 0)
BEGIN
RETURN 1
END

RETURN 0
....

that returns if a user already is in the database or not.

The second procedure is this:
....
IF (EXEC CheckIfUserExcist @Username > 0)
BEGIN
RETURN -1
END
....

but that dosent work, how do you get the result from the first procedure to
Andrew J. Kelly
2/16/2006 9:25:01 AM
You can do one of two things. You can use an output parameter (which I
prefer) as outlined here (Note this is an xml example but it shows how to
use output parameters:

http://www.sqlxml.org/faqs.aspx?faq=54

Or you can do this:

DECLARE @Return INT

EXEC @Return = dbo.CheckIfUserIsInDatabase

One other comment. If you only want to see if a row exists or not you should
use the EXISTS command and not COUNT(*). Exists will stop after it finds the
very first matching row where as COUNT(*) will read every row that matches.
Makes a big difference with tables that have more than one row matching that
value.

IF EXISTS( SELECT * FROM Users WHERE Username = @Username)
RETURN 1
ELSE
RETURN 0


--
Andrew J. Kelly SQL MVP


[quoted text, click to view]

AddThis Social Bookmark Button