如何在存储过程中使用变量代替表名

(已解决)
提问者:lied 悬赏分:50分提问时间:2008/03/20
我想在存储过程中使用变量代替表名,以便对相同格式的不同的表进行数据更改,但我不知道该怎么做。

create procedure proc1
as
declare @tbname varchar(11),@cs int
update @tbname set zd1=zd1+@cs
结果显示:第4行有错误,必须声明变量 @tbname
请问应该怎样写?
最佳答案
回答者:老王 回答时间:2008/02/24
将 Update @tbname set zd1=zd1+@cs
该为:
update Table1 @tbname set zd1=zd1+@cs
或 Update Table1 as @tbname set zd1=zd1+@cs (注:table1 为原表名)

peiyan的意见:
CREATE PROCEDURE
Find_Dj @table_name varchar(20),@strsql varchar(20)

AS

declare @sql varchar(200)

set @sql=’’select * from ’’+@table_name+ ’’ where ’’+ @strsql
execute(@sql)

乐全云的意见:
create procedure proc1
as
declare @tbname varchar(11),@cs int
declare @SQLSTRING NVARCHAR(4000)
SET @SQLSTRING=N’’ update ’’+@tbname+’’ set zd1=zd1+’’+@cs
EXEC sp_executesql @SQLSTRING
我也来回答:您可以在回答中上传图片,如果您的回答是从其他地方引用,请注明出处。
注册且已登录的用户回答即可得5分,回答被采纳则获得悬赏分以及奖励20分。点击登录

站长QQ:28212441

太仓交友 张家界会议网 齐齐商务  直流电机维修 

Copyright © 2007 www.herecn.com! company. All rights reserved.

苏ICP备09011162号