我有一点动态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