日期:2014-05-18  浏览次数:20790 次

DECLARE怎么调用
DECLARE @s NVARCHAR(max)
SELECT @s=ISNULL(@s+',','select ')+a.Name+' as '+REPLACE(a.Name,b.NormContent,b.NormName) 
FROM syscolumns AS a 
  inner JOIN NormInfoTb AS b ON a.Name LIKE b.NormContent+'_%'
WHERE ID=OBJECT_ID('gsm_alarm')


EXEC(@s+' from gsm_alarm')


在SQL2008查询分析器里执行的时候,系统提示【'(' 附近有语法错误。】好像是要给‘s’赋值之类的。怎么调呢。

我想把它放成存储过程方式调用,也不知道咋写。谢谢,在线等。

------解决方案--------------------
DECLARE @s NVARCHAR(max)
set @s = '';
SELECT @s=ISNULL(@s+',','select ')+a.Name+' as '+REPLACE(a.Name,b.NormContent,b.NormName)
FROM syscolumns AS a
inner JOIN NormInfoTb AS b ON a.Name LIKE b.NormContent+'_%'
WHERE ID=OBJECT_ID('gsm_alarm')

set @s = @s+' from gsm_alarm';
EXEC(@s);


------解决方案--------------------
木有看出来语法错误


你print一下看看你拼接的语句有问题没


DECLARE @s NVARCHAR(max)
SELECT @s=ISNULL(@s+',','select ')+a.Name+' as '+REPLACE(a.Name,b.NormContent,b.NormName)
FROM syscolumns AS a
inner JOIN NormInfoTb AS b ON a.Name LIKE b.NormContent+'_%'
WHERE ID=OBJECT_ID('gsm_alarm')

print @s+' from gsm_alarm';