Log what was just selected without temporary table?
In a stored procedure I am selec开发者_开发知识库ting from a table using variables passed in. Then, I want to log what is selected. Is there any way to do this without using a temporary table or table variable?
The select statement:
Select userId, firstName, lastName, address
from myTable
where userName = @userName and password = @password
Then, I want to log the userId, firstName, lastName, and address that were selected. So:
Insert into loginLog...[results gotten above]
Note: I only want to log it if there is only one result returned. If more than one result is returned, I still want to select all the results, but I do not want to log it.
Use your select
to populate local variables in the stored procedure. Log the results, then select
the variable to return them to the caller:
declare
@userID int ,
@firstName varchar(255) ,
@lastName varchar(255) ,
@address varchar(255) ,
@rows int
select @userID = userId ,
@firstName = firstName ,
@lastName = lastName ,
@address = address
from myTable
where ...
set @rows = @@rowcount
insert loginLog
select @userID , @firstName , @lastName , @address
where @rows > 0
select userID = @userID ,
firstName = @firstName ,
lastName = @lastName ,
address = @address
where @rows > 0
精彩评论