2014-12-31 28 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子句中獲取值。 –