以下是我的查询,我需要在选择子查询中输入列值,而不是获取列名 @Temp =临时表
insert into @Temp([dateTime],Reading)
values (@startDate,(select top(1) @trendId from TABLENAME where deviceTimestamp >= @startDate and deviceTimestamp < @tempdt order by deviceTimestamp desc))
ALTER PROCEDURE ProcName
#trendId as nvarchar(max),
#startDate as datetime,
#endDate as datetime
AS
BEGIN
declare #stt varchar(200) = 'select deviceTimestamp,' + #trendId + ' '+'as reading
from TableName
where deviceTimestamp >= '+#startDate+'and deviceTimestamp < '+#endDate+'
order by deviceTimestamp desc'
exec(#stt)END
我收到错误消息:
从字符串转换日期和/或时间时转换失败。
答案 0 :(得分:0)
DECLARE @sqlCommand varchar(1000)
DECLARE @columnList varchar(75)
DECLARE @city varchar(75)
SET @columnList = 'AddressID, AddressLine1, City'
SET @city = '''London'''
SET @sqlCommand = 'SELECT ' + @columnList + ' FROM Person.Address WHERE City = ' + @city
EXEC (@sqlCommand)
Check this link out.动态SQL似乎可以满足您的需要,您可以将表名和表列动态传递给查询。因此,您可以将列名作为参数变量传递。否则,您只需选择plainText作为记录。