2014-12-31 27 views
0

我正在将查询转换为我的第一个存储过程,但没有找到解决方案的一个步骤。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了很多,但没有找到解决办法:-(

可以给我一些指示

谢谢!

塞尔吉奥

+0

如果有什么更多的是一个行被更新 –

+0

@NoDisplayName那么,人们希望列'dbo.Users.UserID'是主键,并且只有一个匹配标量参数('UserID = @ U')。 –

+0

@AaronBertrand thats one theory,但如果单个'userid'在'booking table'中多次出现,会怎样呢? –

回答

0

声明表变量并将输出转换成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 ([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) 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 ([email protected] and [email protected] and b.active='1' and g.Role='parent' 
        and [email protected] and b.[Day][email protected]); 

-- 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 
+0

嗨! 好,因为它会修改仅1记录时无需使用一个表 但所提出的解决方案确实需要的最后一个选择的参数必须具有相同的参数以前的更新.. 但我一直在寻找一个简单的解决方案,因为我们已经有更新的价值..应该有办法导出这样的价值,而不再查询.. 现在我添加了完整的SP也许更清晰 谢谢! – Joe

+0

确保在数据库中定义了足够的唯一约束来执行此操作。 –

+0

@Joe输出语法只能将记录插入到表中,因此无论您是更新一个还是多个记录,您都需要一个表变量来从output子句中获取值。 –