首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

存储过程中一个表的某个字段值为另一个表的表名该如何写啊

2013-02-04 
存储过程中一个表的某个字段值为另一个表的表名该怎么写啊?(select tablename from novel_topic where top

存储过程中一个表的某个字段值为另一个表的表名该怎么写啊?
(select tablename from novel_topic where topicid=@topicid)

tablename的值为另一个表的表名
insert into 表名(topicid,chapter,path,length) values(@topicid,@chapter,@path,@length)


这个存储过程该怎么写啊?
   


declare @tb nvarchar(50),@sql nvarchar(2000)
select @tb=tablename from novel_topic where topicid=@topicid

set @sql='insert into '+@tb+'(topicid,chapter,path,length) values(@topicid,@chapter,@path,@length)'
exec sp_exeutesql @sql,N'@topicid int,@chapter nvarchar(20)....',@topicid,@chapter... 

[解决办法]
- -你看你写的与我写的有啥区别呢。。
有简单的写法当然给你写喽,表、列等对象不能用变量表示
需要用字符拼接的方式,用exec来执行

热点排行
Bad Request.