sqlserver存储过程返回值
存储过程中我想判断用户名是否存在 info是表名 然后return的值在c#里用ParameterDirection.ReturnValue获得 但是为什么无论如何返回值都是0 但是结果倒好像是对的 不存在的话插入 存在的话不插入了 纠结那个返回值 新手。。
create proc two
@username nvarchar(50),
@password nvacchar(50)
as
begin
if exists (select username from info where username=@username)
return 0
else
insert into info (username,password) values (@username,@password)
return 1
end
create proc two
@username nvarchar(50),
@password nvacchar(50),
@result int output
as
begin
if exists (select username from info where username=@username)
set @reslut=0
else
insert into info (username,password) values (@username,@password)
set @reslut=1
return result
end
大概类似是这样,好久没写了。你看看
create proc two
@username nvarchar(50),
@password nvacchar(50)
as
begin
if exists (select username from info where username=@username)
select 0
else
insert into info (username,password) values (@username,@password)
select 1
end
声明返回值变量,并赋值, 查询完return 值,这样是不行的~
在else后面要添加begin end,在没有账号要进行插入并返回值,两行语句
create proc two
@username nvarchar(50),
@password nvacchar(50)
as
begin
if exists (select username from info where username=@username)
select 0
else
begin
insert into info (username,password) values (@username,@password)
select 1
end
end
CREATE proc two
@username NVARCHAR(50),
@password nvacchar(50),
@result INT output
AS
BEGIN
IF EXISTS (SELECT username FROM info WHERE username=@username)
SET @reslut=0;
ELSE
BEGIN
INSERT INTO info (username,PASSWORD) VALUES (@username,@password);
SET @reslut=1;
END;
RETURN result;
END