2017-02-11 150 views
0
create procedure dbo.move_pos 
(
@id int, 
@tbl varchar(50) 
)  
as  
begin  
declare @pos int  
exec('select '[email protected]+'=POS from '[email protected]+' where id='[email protected])  
exec('update '[email protected]+' set POS=POS+1 where POS<'[email protected])  
end 

在上述过程中,列POS的类型为int。但是,当我米执行该过程它显示以下错误:SQL存储过程中的动态表名称和参数

消息102,级别15,状态1,行1

附近有语法错误 '='。

我使用SQL SERVER 2012.需要帮助。提前致谢 !!!

+0

选择 '+ @ POS +'=从'+ @ TBL +的POS其中id ='+ @ ID西隧ü要做到与@ pos –

+0

试试这个 declare query varchar(100); (查询) add @before查询,id,pos –

+0

@YashveerSingh I m试图移动一个项目顶部,所以我使用pos。 – SUPU

回答

0

我会推荐重新思考所有这些动态SQL存储过程。
但是,如果你真的必须使用动态SQL,试试这个:

create procedure dbo.move_pos 
(
    @id int, 
    @tbl varchar(50) 
)  
as  
begin  
declare @sql nvarchar(max); 
set @sql = 'update ' + QUOTENAME(@tbl) + ' 
      set POS = POS + 1 
      where POS < (
       select POS 
       from ' + QUOTENAME(@tbl) + ' 
       where id = '+ cast(@id as nvarchar(10) 
      )' 

exec(@sql)  
end 
+0

谢谢@Zohar它工作正常。非常感谢你。 – SUPU