我正在將查詢轉換爲我的第一個存儲過程,但沒有找到解決方案的一個步驟。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'中多次出現,會怎樣呢? –