我正在将查询转换为我的第一个存储过程但是找不到一步的解决方案。
此处为完整程序
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 (g.UserID=@U and g.Password=@P and b.active='1' and g.Role='parent' and b.teacherID=@T and w.Time=@O and w.studentID=@S and b.[Day]=@D )
)
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 (g.UserID=@U and g.Password=@P and b.active='1' and g.Role='parent' and w.Time=@O and b.[Day]=@D );
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 (g.UserID=@U and g.Password=@P and b.active='1' and g.Role='parent' and b.teacherID=@T and w.time=@O and b.[Day]=@D 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 (g.UserID=@U and g.Password=@P and b.active='1' and g.Role='parent' and w.Time=@O and b.Day=@D 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 (g.UserID=@U and g.Password=@P and b.active='1' and g.Role='parent' and b.teacherID=@T and w.studentID=@S and b.[Day]=@D )
)
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 TeacherID=@T and [Day]=@D
)
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 Teacherid=@T
set @ID= (select ID from booking where teacherID=@T and [Day]=@D)
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 teacherID=@T and [Day]=@D)
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
中,但不知道如何操作
我搜索了很多但没有找到解决方案: - (
可以给我一些指示
谢谢!
塞尔吉奥
答案 0 :(得分:0)
声明表变量和OUTPUT INTO temp_Table
像这样......
SET NOCOUNT ON
DECLARE @ID int;
DECLARE @ID_TABLE TABLE (ID VARCHAR(100))
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 (g.UserID=@U and g.Password=@P and b.active='1' and g.Role='parent'
and b.teacherID=@T and w.Time=@O and w.studentID=@S and b.[Day]=@D )
)
begin
update w
set w.Active= ~ w.Active
output 'OK/'+cast(inserted.Active as nvarchar) INTO @ID_TABLE(ID)
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 (g.UserID=@U and g.Password=@P and b.active='1' and g.Role='parent'
and w.Time=@O and b.[Day]=@D );
-- select the ID into a variable or return a table execute one of the following
SELECT @ID = ID FROM @ID_TABLE --<-- If it will only return a single value
SELECT * FROM @ID_TABLE --<-- If it can update multiple rows at a time
end