SQL如何把字符串赋值给变量,代替查询语句

例如这个查询语句,
(select Id from VideoType where tid in
(select Id from VideoType where tid in
(select Id from VideoType where tid in
(select Id from VideoType where tid in
(select id from VideoType where tid in(select id from VideoType where Title in('老人专区','幼儿专区')))))))
我能不能这样
string a="(select Id from VideoType where tid in";
然后查询语句就瘦身了变成
a
a
a
a
a "in(select id from VideoType where Title in('老人专区','幼儿专区')))))))"
请问该怎么实现?

declare @sql nvarchar(max);
set @sql = " select * from TableName";

exec ( @sql);追问

能详细点吗?怎么连接他们
这样吗?
declare @sql nvarchar(max);
set @sql = '(select Id from VideoType where tid in'
exec (@sql)+
exec(@sql)+(select id from VideoType where Title in('老人专区','幼儿专区')))))))))

能详细点吗?怎么连接他们
这样吗?
declare @sql nvarchar(max);
set @sql = '(select Id from VideoType where tid in'
exec (@sql)+
exec(@sql)+(select id from VideoType where Title in('老人专区','幼儿专区')))))))))

追答

1、先拼接完 @sql
2、最后调用  exec (@sql) ; 
--
不过,你得确保你写的sql没有问题

温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网