我有一點動態SQL我運行的工作正常,當我手動寫入變量,但只要我改變它們從手動寫入到實際變量,我得到上面的錯誤。從字符串動態轉換日期和/或時間時轉換失敗sql
的代碼是:
set @query = 'SELECT eng, ' + @colsNull + '
from
(
select eng, [count], cast(weekof as date) weekof
from dbo.RPT_ENG_WEEK (''1 jan 2013'', ''9 apr 2013'', ''1 jan 2013'', ''9 apr 2013'')
) x
pivot
(
sum([count])
for weekof in (' + @cols + ')
) p '
,工作正常,但只要我將其更改爲
set @query = 'SELECT eng, ' + @colsNull + '
from
(
select eng, [count], cast(weekof as date) weekof
from dbo.RPT_ENG_WEEK ('[email protected]+', '[email protected]+', '[email protected]+', '[email protected]+')
) x
pivot
(
sum([count])
for weekof in (' + @cols + ')
) p '
我得到的錯誤。我也試過
set @query = 'SELECT eng, ' + @colsNull + '
from
(
select eng, [count], cast(weekof as date) weekof
from dbo.RPT_ENG_WEEK ((select CONVERT(DATE'[email protected]+',105)), (select CONVERT(DATE'[email protected]+',105)), (select CONVERT(DATE'[email protected]+',105)), (select CONVERT(DATE'[email protected]+',105)))
) x
pivot
(
sum([count])
for weekof in (' + @cols + ')
) p '
但無濟於事!
也曾嘗試
from dbo.RPT_ENG_WEEK ((select CONVERT(DATE'''[email protected]+''',105)), (select CONVERT(DATE'''[email protected]+''',105)), (select CONVERT(DATE'''[email protected]+''',105)), (select CONVERT(DATE'''[email protected]+''',105)))
和
from dbo.RPT_ENG_WEEK ('''[email protected]+''', '''[email protected]+''', '''[email protected]+''', '''[email protected]+''')
不這個位上有足夠多的單引號'from dbo.RPT_ENG_WEEK('+ @ from +','+ @ to +','+ @ start +','+ @ end +')'我想。 – 2013-04-09 15:40:03
@Dommer是不是我放在中間代碼塊? – franglais 2013-04-09 15:42:08
我現在也試過 from dbo.RPT_ENG_WEEK('''+ @ from +''','''+ @ to +''','''+ @ start +''','''+ @ end +' '') 但沒有快樂! – franglais 2013-04-09 15:43:41