sql查询从用户插入接收的参数,从现有表中获取记录并插入到另一个现有表中

时间:2014-02-06 10:31:04

标签: sql-server

任何人都请帮助我

这是我在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”这个名称。有效表达式是常量,常量表达式和(在某些上下文中)变量。不允许使用列名。

我知道上述查询的原因

我尝试以不同的方式执行查询,但记录重复

请有人帮助我

1 个答案:

答案 0 :(得分: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 Title=@title 
相关问题