0

我现在正在使用动态查询,我需要表中的值。

到目前为止我的查询:

DECLARE @query varchar(1500)
SET @query = 
'SELECT Id,'
+ 'SUM(CASE WHEN ['+convert(varchar(50),CAST(myTable.myDate AS date),120)+'] = ['+convert(varchar(50), CAST(GETDATE() AS date), 120)+'] then [Counter] else 0 end) ''0'','
+ 'SUM(CASE WHEN ['+convert(varchar(50),CAST(DATEADD(day, -1, myTable.myDate)), 120)+'] = ['+convert(varchar(50),CAST(DATEADD(day, -1, GETDATE()) AS date), 120)+'] then [counter] else 0 end) ''1'','
+ 'SUM(CASE WHEN ['+convert(varchar(50),CAST(DATEADD(day, -2, myTable.myDate)), 120)+'] = ['+convert(varchar(50),CAST(DATEADD(day, -2, GETDATE()) AS date), 120)+'] then [counter] else 0 end) ''2'','
+ 'SUM(CASE WHEN ['+convert(varchar(50), CAST(DATEADD(day, -3, myTable.myDate)), 120)+'] = ['+convert(varchar(50),CAST(DATEADD(day, -3, GETDATE()) AS date),120)+'] then [counter] else 0 end) ''3'','
+ 'SUM(CASE WHEN ['+convert(varchar(50),CAST(DATEADD(day, -4, myTable.myDate)),120)+'] = ['+convert(varchar(50),CAST(DATEADD(day, -4, GETDATE()) AS date),120)+'] then [counter] else 0 end) ''4'''
+ 'FROM [myTable] GROUP BY Id'
PRINT(@query)
EXEC(@query)

这不起作用,因为它说“无法绑定多部分识别器“myTable”。” 在第 4-8 行。

我见过有人推荐这样的东西:

CREATE TABLE #tmp
(
    id varchar(50),
    counter int,
    myDate smalldatetime
)
Insert Into #tmp (id, counter, myDate)
SELECT * FROM myTable
--GO                       --doesn't work either with or without 'go'
DECLARE @query varchar(1500)
SET @query = 
'SELECT Id,'
+ 'SUM(CASE WHEN ['+convert(varchar(50),CAST(#tmp.myDate AS date),120)+'] = ['+convert(varchar(50), CAST(GETDATE() AS date), 120)+'] then [counter] else 0 end) ''0'','
+ 'SUM(CASE WHEN ['+convert(varchar(50),CAST(DATEADD(day, -1, #tmp.myDate)), 120)+'] = ['+convert(varchar(50),CAST(DATEADD(day, -1, GETDATE()) AS date), 120)+'] then [counter] else 0 end) ''1'','
+ 'SUM(CASE WHEN ['+convert(varchar(50),CAST(DATEADD(day, -2, #tmp.myDate)), 120)+'] = ['+convert(varchar(50),CAST(DATEADD(day, -2, GETDATE()) AS date), 120)+'] then [counter] else 0 end) ''2'','
+ 'SUM(CASE WHEN ['+convert(varchar(50),CAST(DATEADD(day, -3, #tmp.myDate)), 120)+'] = ['+convert(varchar(50),CAST(DATEADD(day, -3, GETDATE()) AS date),120)+'] then [counter] else 0 end) ''3'','
+ 'SUM(CASE WHEN [+'convert(varchar(50),CAST(DATEADD(day, -4, #tmp.myDate)), 120)+'] = ['+convert(varchar(50),CAST(DATEADD(day, -4, GETDATE()) AS date),120)+'] then [aounter] else 0 end) ''4'''
+ 'FROM [myTable] GROUP BY Id'
PRINT(@query)
EXEC(@query)

还是不行。请帮忙!

我的问题的简化版本:

DECLARE @query varchar(1500)
SET @query = 'SELECT ['+myTable.value+'] FROM [myTable]'
EXEC(@query)

不工作

4

1 回答 1

0

尝试这样的事情:

CREATE TABLE #tmp
(
    id varchar(50),
    counter int,
    myDate smalldatetime
)
Insert Into #tmp (id, counter, myDate)
SELECT * FROM myTable

DECLARE @query varchar(1500)
Select @query = 
'SELECT Id,'
+ 'SUM(CASE WHEN ''' + convert(varchar(50), #tmp.myDate,120) + ''' = '''+convert(varchar(50), GETDATE(), 120)+''' then ''counter'' else 0 end) ''0'','
+ 'SUM(CASE WHEN '''+convert(varchar(50), DATEADD(day, -1, #tmp.myDate), 120)+''' = '''+convert(varchar(50),DATEADD(day, -1, GETDATE()), 120)+''' then ''counter'' else 0 end) ''1'','
+ 'SUM(CASE WHEN '''+convert(varchar(50), DATEADD(day, -2, #tmp.myDate), 120)+''' = '''+convert(varchar(50),DATEADD(day, -2, GETDATE()), 120)+''' then ''counter'' else 0 end) ''2'','
+ 'SUM(CASE WHEN '''+convert(varchar(50), DATEADD(day, -3, #tmp.myDate), 120)+''' = '''+convert(varchar(50),DATEADD(day, -3, GETDATE()), 120)+''' then ''counter'' else 0 end) ''3'','
+ 'SUM(CASE WHEN '''+convert(varchar(50), DATEADD(day, -4, #tmp.myDate), 120)+''' = '''+convert(varchar(50),DATEADD(day, -4, GETDATE()), 120)+''' then ''counter'' else 0 end) ''4'''
+ ' FROM ' from #tmp 
Set @query = @query + ' #tmp GROUP BY Id'
PRINT(@query)
EXEC(@query)
于 2013-06-18T07:27:26.277 回答