我正在将查询转换为我的第一个存储过程,但没有找到解决方案的一个步骤。sql服务器存储过程插入值作为输出参数
这里全部程序
UPDATE Procedure dbo.sp_BOOK
@U nvarchar(50),
@P nvarchar(50),
@T nvarchar(50),
@D datetime ,
@O nvarchar(10),
@S nvarchar(50),
@R nvarchar(50) OUTPUT
AS
SET NOCOUNT ON
DECLARE @ID int;
IF EXISTS
(
select d.name
from users s
join users g on CHARINDEX(s.UserID,g.ParentOf)>0
join booking b on charindex(s.ClassRoom,b.class)>0
join interview w on b.id=w.bookingID
join users d on d.userid=b.teacherID
where ([email protected] and [email protected] and b.active='1' and g.Role='parent' and [email protected] and [email protected] and [email protected] and b.[Day][email protected])
)
begin
update w set w.Active= ~ w.Active output 'OK/'+cast(inserted.Active as nvarchar) rspn
from users s
join users g on CHARINDEX(s.UserID,g.ParentOf)>0
join booking b on charindex(s.ClassRoom,b.class)>0
join interview w on b.id=w.bookingID
join users d on d.userid=b.teacherID
where ([email protected] and [email protected] and b.active='1' and g.Role='parent' and [email protected] and b.[Day][email protected]);
end
ELSE
begin
IF EXISTS
(
select d.name
from users s
join users g on CHARINDEX(s.UserID,g.ParentOf)>0
join booking b on charindex(s.ClassRoom,b.class)>0
join interview w on b.id=w.bookingID
join users d on d.userid=b.teacherID
where ([email protected] and [email protected] and b.active='1' and g.Role='parent' and [email protected] and [email protected] and b.[Day][email protected] and w.active='1')
)
begin
SET @R='KO/booked already'
--select 'KO/booked already from other' rspn
end
ELSE
begin
IF EXISTS
(
select 1
from interview w
join users s on s.userID=w.StudentID
join users g on charindex(w.studentID,g.ParentOf)>0
join booking b on charindex(s.classroom,b.class)>0
join users d on d.UserID=b.TeacherID
where ([email protected] and [email protected] and b.active='1' and g.Role='parent' and [email protected] and [email protected] and w.active='1')
)
begin
--select 'KB/you are busy at this Time!' rspn
SET @R='KB/you are busy'
end
ELSE
begin
IF EXISTS
(
select d.name
from users s
join users g on CHARINDEX(s.UserID,g.ParentOf)>0
join booking b on charindex(s.ClassRoom,b.class)>0
join interview w on b.id=w.bookingID
join users d on d.userid=b.teacherID
where ([email protected] and [email protected] and b.active='1' and g.Role='parent' and [email protected] and [email protected] and b.[Day][email protected])
)
begin
--select 'KZ/You already booked with teacher fro this child' rspn
SET @R='KZ/You already booked'
end
ELSE
begin
IF NOT EXISTS
(
select 1 from booking where [email protected] and [Day][email protected]
)
begin
begin Transaction
Insert into booking (TeacherID,Materie,Class,[day],TimeFrame,Duration, DateFrom, DateTo) select @T, r.Materie, r.classi, @D, '0900-1000' , durata, getdate(), dateadd(d,anticipo*-1,@D) from ricevimentoD r join users u on r.TeacherID=u.UserID where [email protected]
set @ID= (select ID from booking where [email protected] and [Day][email protected])
insert into Interview (BookingID, Time, studentID) VALUES (@ID,@O,@S)
SET @R='OK/Registered New'
commit
end
ELSE
begin
set @ID= (select ID from booking where [email protected] and [Day][email protected])
insert into Interview (BookingID, Time, studentID) VALUES (@ID,@O,@S)
SET @R='OK/Registered Old'
end
end
end
end
end
SET NOCOUNT OFF
...........
问题是改造
OUTPUT 'OK/'+CAST(insterted.Active as nvarchar) rspn
到的东西,把价值@R
,但有不知道该怎么办
我GOOGLE了很多,但没有找到解决办法:-(
可以给我一些指示
谢谢!
塞尔吉奥
如果有什么更多的是一个行被更新 –
@NoDisplayName那么,人们希望列'dbo.Users.UserID'是主键,并且只有一个匹配标量参数('UserID = @ U')。 –
@AaronBertrand thats one theory,但如果单个'userid'在'booking table'中多次出现,会怎样呢? –