日期:2014-05-16 浏览次数:20719 次
--因为在' + cast(@TimeId as varchar(255)) + '这里无法加 ' 报错
declare @sql varchar(255)
declare @id varchar(255),@TimeId varchar(255);
set @sql = 'SELECT TOP ' + cast(@id as varchar) + ' * FROM bbb where schoolid = ' + cast(@TimeId as varchar(255)) + ' ORDER BY TimeId'
exec(@sql)
--查询语句如果按照以下输入是正确的
SELECT TOP 11 * FROM bbb where TimeId = '301221' ORDER BY TimeId
declare @sql varchar(255)
declare @id varchar(255),@TimeId varchar(255);
SET @timeid ='301221'
SET @id=10
set @sql = 'SELECT TOP (' + cast(@id as varchar) + ') * FROM bbb where schoolid = ' + cast(@TimeId as varchar(255)) + ' ORDER BY TimeId'
PRINT(@sql)
set @sql = 'SELECT TOP (' + cast(@id as varchar) + ') * FROM bbb where schoolid = ''' + cast(@TimeId as varchar(255)) + ''' ORDER BY TimeId'
PRINT(@sql)
/*
SELECT TOP (10) * FROM bbb where schoolid = 301221 ORDER BY TimeId
SELECT TOP (10) * FROM bbb where schoolid = '301221' ORDER BY TimeId
*/