2014-02-06 22 views
-1

請人幫我SQL查詢中插入從用戶接收到的參數,並從現有表中獲取記錄,並插入到另一個現有的表

這裏是SQL Server我的存儲過程:

ALTER procedure ExistingBook 
    @title nvarchar(50), 
    @Author nvarchar(50), 
    @Number nvarchar(50) 
as 
    declare @Sno as int 

    begin 
     if exists(select * from Students where Title = @title) 
     begin 
     select @Sno = count(*) 
     from Teacher 

     if (@Author='proof') 
      if (@Number>'20') 
       insert into Teacher(@Sno+1, Team, @title, getdate(), 
            @Number, PMName, Comments, ISBN, null, null) 

     select 
      Team, PMName, Comments, ISBN 
     from Students 
     where Title = @title 
     end 
    end 

當我執行上面的查詢我得到錯誤

在此上下文中不允許使用名稱「Team」。有效表達式是常量,常量表達式和(在某些情況下)變量。列名不被允許。

我知道以上查詢

我之所以在不同的方式我的查詢執行的嘗試,但記錄複製

請有人幫我

回答

0

插入查詢格式不正確。

我想你想要像

INSERT INTO Teacher 
      (
      col1, 
      col2, 
      col3, 
      col4, 
      col5, 
      col6, 
      col7, 
      col8, 
      col9 
      ) 
      select 
      @Sno+1, 
      Team, 
      @title, 
      @Number, 
      PMName, 
      Comments, 
      ISBN, 
      null, 
      null 
      from Students 
where [email protected] 
+0

....因爲這些都是要麼文本,常數或SQL變量,你可以** **也使用'INSERT INTO dbo.Teacher(COL1。 ..,colN)VALUES(.......)' –

+0

非常感謝Kale,這是我尋找的 – appu

相關問題