select语句错误的子查询

时间:2023-01-14 00:09:39
DECLARE @colSelect TABLE(name VARCHAR(100));

insert into @colSelect values ('id')
insert into @colSelect values ('d')
insert into @colSelect values ('rpn')
insert into @colSelect values ('criticality')
insert into @colSelect values ('dateCreated')

DECLARE @tableName varchar(100), @startDate datetime, @endDate datetime

SET @tableName = 'tblFmeas'
SET @startDate  = '2016-04-05 00:00:00'
SET @endDate    = '2016-04-05 00:00:00'


DECLARE @SQL nvarchar(max)

SET @SQL = 'select ' + (select * from @colSelect) + ' from ' + @tableName + ' where dateCreated between ' + @startDate + ' and' + @endDate;

EXEC (@SQL)

Someone have a suggestion for this?

有人对此提出建议吗?

Cause in my app, the user choose the columns that he want to see in report and i need to pass them for my query.

在我的应用程序中,用户选择他想要在报告中看到的列,我需要将它们传递给我的查询。

1 个解决方案

#1


-1  

Here it returning multiple values since there is no join condition so that's the issue.

这里它返回多个值,因为没有连接条件,所以这就是问题。

Probably "Select Top 1 name from @colselect " will work. But not sure what you're trying to achieve.

可能“从@colselect中选择前1名”将起作用。但不确定你想要实现的目标。

#1


-1  

Here it returning multiple values since there is no join condition so that's the issue.

这里它返回多个值,因为没有连接条件,所以这就是问题。

Probably "Select Top 1 name from @colselect " will work. But not sure what you're trying to achieve.

可能“从@colselect中选择前1名”将起作用。但不确定你想要实现的目标。