0
SELECT @sql='

select 
    *
from (
    select ''Ongoing'' AS Ongoing,
    Coalesce(COUNT(project),0) AS project, Coalesce(COUNT(year(u.PlannedStartDate)),0) as [y]
    from Projects u  WHERE 
    u.actualstartdate IS  NULL 
    AND u.Startdate < ''+GETDATE()+''
    AND ID ='''+@ID+'''  

  ) Data
PIVOT (
  COUNT(project)
  FOR [y]
  IN (
    ' + @Years + '
  )
) PivotTable
'

在这里我想传递当前日期但它不起作用..这是因为 GETDATE() 的值没有出现在字符串中

4

3 回答 3

2

更改AND u.Startdate < ''+GETDATE()+''为 ANDu.Startdate < GETDATE()

于 2012-07-11T14:37:54.273 回答
1

只需删除它周围的引号我会认为

SELECT @sql='

select * from ( select ''Ongoing'' AS Ongoing, Coalesce(COUNT(project),0) AS project,
Coalesce(COUNT(year(u.PlannedStartDate)),0) as [y] from Projects u 
WHERE u.actualstartdate IS   NULL AND u.Startdate < GETDATE() AND ID ='''+@ID+'''

) Data PIVOT ( COUNT(project) FOR [y] IN ( ' + @Years + ' ) ) PivotTable '
于 2012-07-11T14:39:28.130 回答
0

尝试将 GETDATE() 转换为这样的 varchar:CAST(GETDATE() AS varchar)

于 2012-07-11T14:37:50.460 回答