Click to See Complete Forum and Search --> : SQL Server Stored Procedures
vbsquare
Jun 13th, 2000, 05:18 PM
Does anyone know how to write a SQL Server stored procedure that will check if an item is in a database? Here is what I have at the moment, but it doesn't seem to be working:
CREATE Procedure sp_mdLoginUser
@Username varChar(50),
@PassToken varChar(50)
As
DECLARE @UserID int
SELECT
Username,
PassToken
FROM
Users
WHERE
Username = @Username
If @@error = 0
BEGIN
SELECT @UserID = @@IDENTITY
return @UserID
END
ELSE
BEGIN
return(0)
END
Thanks
Mongo
Jun 13th, 2000, 10:29 PM
I'm not clear what you're asking, but this is a Sybase quickie I use a lot. Remember to be cautious about using a system stored procedure prefix in SQL Server. This or looking at the helptext for sp_who may give you a few ideas. Hope this helps.
create proc usp_use (@suid int = 0)
as
IF @suid = 0
BEGIN
SELECT master..sysprocesses.spid
, master..syslogins.suid
, master..syslogins.fullname
, master..sysprocesses.status
, master..sysprocesses.hostname
, master..sysprocesses.cmd
, master..sysprocesses.cpu
, master..sysprocesses.physical_io
, master..sysprocesses.blocked
, master..sysprocesses.program_name
, master..syslogins.dbname
FROM master..sysprocesses
, master..syslogins
WHERE master..sysprocesses.suid = master..syslogins.suid
COMMIT
END
ELSE
BEGIN
SELECT master..sysprocesses.spid
, master..syslogins.suid
, master..syslogins.fullname
, master..sysprocesses.status
, master..sysprocesses.hostname
, master..sysprocesses.cmd
, master..sysprocesses.cpu
, master..sysprocesses.physical_io
, master..sysprocesses.blocked
, master..sysprocesses.program_name
, master..syslogins.dbname
FROM master..sysprocesses
, master..syslogins
WHERE master..sysprocesses.suid = master..syslogins.suid
AND master..syslogins.suid = @suid
COMMIT
END
lenin
Jun 13th, 2000, 10:29 PM
What happens when you execute the current SP? he SQL itself looks fine.
Mongo
Jun 14th, 2000, 12:41 AM
When I run "usp_use 115" it returns the following which gives me an idea how I'm doing. My suid is 115.
spid suid fullname status hostname cmd cpu physical_io blocked program_name dbname
----- ----- ------------------------------ ------------ ---------- ---------------- ---------- ----------- ------- ---------------- ------------------------------
78 115 my Name running SELECT 258 0 0 db_name
87 115 my Name sleeping COMMIT TRANSACTI 101 1 0 db_name
Oops, I'm a dope. Lenin, I misread your question, thinking you were the originator, vbsquare. Nevermind. *blush*
[Edited by Mongo on 06-14-2000 at 01:45 PM]
vbsquare
Jun 14th, 2000, 01:31 AM
If you're refering to the SQL Stored Proc that I posted, then it simply returns a value of 0 each time even when correct details are passed.
Any ideas?
lenin
Jun 14th, 2000, 02:48 PM
Why not use a simple if count(*)?
something like:
if ( select count(*) from query_status where Username = @Username ) >= 1
begin
SELECT @UserID = @@IDENTITY
return @UserID
end
else
return(0)
vbsquare
Jun 14th, 2000, 10:56 PM
This is what i've got and it still isn't working:
CREATE Procedure sp_mdLoginUser
@Username char(50)
As
DECLARE @UserID int
If (select count(*) from Users Where Username = @Username) >= 1
begin
SELECT @UserID = @@Identity
return @UserID
end
else
return(0)
Clunietp
Jun 15th, 2000, 10:40 AM
from SQL 7 books online
After an INSERT, SELECT INTO, or bulk copy statement completes, @@IDENTITY contains the last identity value generated by the statement. If the statement did not affect any tables with identity columns, @@IDENTITY returns NULL.
You are not doing any of the above, so you get NULL (but SQL really returns 0 instead)
You can do something like this:
CREATE PROCEDURE spu_test
@ID int
AS
declare @EmpID int
Select @EmpID = EmployeeID from Employees where EmployeeID = @ID
return @EmpID
or you can use an output parameter
CREATE PROCEDURE spu_test
@ID int,
@retIDVal int output
AS
Select @retIDVal = EmployeeID from Employees where EmployeeID = @ID
and to get the output parameter:
declare @retID int
execute spu_test 2, @retIDVal = @retID output
print "Return ID = " + cast(@retID as varchar(1))
HTH
Tom
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.