[英]Return value in SQL Server stored procedure
I have a stored procedure that has an if statement in it. 我有一个存储过程,其中包含if语句。 If the number of rows counted is greater than 0 then it should set the only output parameter
@UserId
to 0 如果计算的行数大于0,则应将唯一的输出参数
@UserId
为0
However it only returns a value in the second part of the query. 但是它只返回查询第二部分中的值。
@EmailAddress varchar(200),
@NickName varchar(100),
@Password varchar(150),
@Sex varchar(50),
@Age int,
@EmailUpdates int,
@UserId int OUTPUT
IF
(SELECT COUNT(UserId) FROM RegUsers WHERE EmailAddress = @EmailAddress) > 0
BEGIN
SET @UserId = 0
END
ELSE
BEGIN
INSERT INTO RegUsers (EmailAddress,NickName,PassWord,Sex,Age,EmailUpdates) VALUES (@EmailAddress,@NickName,@Password,@Sex,@Age,@EmailUpdates)
SELECT SCOPE_IDENTITY()
END
END
You can either do 1 of the following: 您可以执行以下任一操作:
Change: 更改:
SET @UserId
= 0 to SELECT @UserId
SET @UserId
= 0设置为SELECT @UserId
This will return the value in the same way your 2nd part of the IF statement is. 这将以与IF语句的第二部分相同的方式返回值。
Or, seeing as @UserId is set as an Output, change: 或者,将@UserId设置为输出,更改:
SELECT SCOPE_IDENTITY()
to SET @UserId = SCOPE_IDENTITY()
SELECT SCOPE_IDENTITY()
SET @UserId = SCOPE_IDENTITY()
It depends on how you want to access the data afterwards. 这取决于您之后如何访问数据。 If you want the value to be in your result set, use
SELECT
. 如果希望值在结果集中,请使用
SELECT
。 If you want to access the new value of the @UserId parameter afterwards, then use SET @UserId
如果您想在之后访问@UserId参数的新值,则使用
SET @UserId
Seeing as you're accepting the 2nd condition as correct, the query you could write (without having to change anything outside of this query) is: 看到您接受第二个条件是正确的,您可以编写的查询(无需更改此查询之外的任何内容)是:
@EmailAddress varchar(200),
@NickName varchar(100),
@Password varchar(150),
@Sex varchar(50),
@Age int,
@EmailUpdates int,
@UserId int OUTPUT
IF
(SELECT COUNT(UserId) FROM RegUsers WHERE EmailAddress = @EmailAddress) > 0
BEGIN
SELECT 0
END
ELSE
BEGIN
INSERT INTO RegUsers (EmailAddress,NickName,PassWord,Sex,Age,EmailUpdates) VALUES (@EmailAddress,@NickName,@Password,@Sex,@Age,@EmailUpdates)
SELECT SCOPE_IDENTITY()
END
END
I can recommend make pre-init of future index value, this is very usefull in a lot of case like multi work, some export etc 我可以推荐使用未来索引值的pre-init,这在很多情况下非常有用,如多工作,一些导出等
just create additional User_Seq
table: with two fields: id Uniq index
and SeqVal nvarchar(1)
只需创建额外的
User_Seq
表:有两个字段: id Uniq index
和SeqVal nvarchar(1)
and create next SP, and generated ID value from this SP and put to new User row! 并创建下一个SP,并从此SP生成ID值并将其放入新的用户行!
CREATE procedure [dbo].[User_NextValue]
as
begin
set NOCOUNT ON
declare @existingId int = (select isnull(max(UserId)+1, 0) from dbo.User)
insert into User_Seq (SeqVal) values ('a')
declare @NewSeqValue int = scope_identity()
if @existingId > @NewSeqValue
begin
set identity_insert User_Seq on
insert into User_Seq (SeqID) values (@existingId)
set @NewSeqValue = scope_identity()
end
delete from User_Seq WITH (READPAST)
return @NewSeqValue
end
Try to call your proc in this way: 尝试以这种方式调用您的proc:
DECLARE @UserIDout int
EXEC YOURPROC @EmailAddress = 'sdfds', @NickName = 'sdfdsfs', ..., @UserId = @UserIDout OUTPUT
SELECT @UserIDout
@EmailAddress varchar(200),
@NickName varchar(100),
@Password varchar(150),
@Sex varchar(50),
@Age int,
@EmailUpdates int,
@UserId int OUTPUT
DECLARE @AA INT
SET @AA=(SELECT COUNT(UserId) FROM RegUsers WHERE EmailAddress = @EmailAddress)
IF @AA> 0
BEGIN
SET @UserId = 0
END
ELSE
BEGIN
INSERT INTO RegUsers (EmailAddress,NickName,PassWord,Sex,Age,EmailUpdates) VALUES (@EmailAddress,@NickName,@Password,@Sex,@Age,@EmailUpdates)
SELECT SCOPE_IDENTITY()
END
END
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.