在 SQL 服务器中执行超过长度的 sp_executesql
execute sp_executesql over length in SQL Server
我的SQL服务器版本是SQLServer 2008 R2。
in
语句中大约有 100 行。
我用这个SQL做一个动态查询:
DECLARE @DynamicDate nvarchar(100)
DECLARE @SQLString_1 nvarchar(max)
SET @DynamicDate='20181021'
SET @SQLString_1 =
N'select *
from T1_'+@DynamicDate+' w
left join
T2 c
on w.ASSET_ID=c.DEVICE_ID
left join
T3 cm
on c.DEVICE_ID=cm.ASSET_ID
left join
T4 gm
on cm.GROUP_ID=gm.GROUP_ID
left join
T5 em
on w.EVENT_ID =em.EVENT_ID
and w.EVENT_CATEGORY_ID=em.EVENT_CATEGORY_ID
left join T6 sa
on w.PROCESS_NAME=sa.PROCESS_NAME
where
w.ASSET_ID in (''{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}'',
''{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}'',
''{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}'',
''{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}'',
……
''{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}'',
''{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}'',
''{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}'',
''{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}'',
''{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}''
)
'
print(@SQLString_1)
--EXECUTE sp_executesql @SQLString_1
当我打印 sql 时。
输出如下:
select
*
from T1_20181021 w
left join
T2 c
on w.ASSET_ID=c.DEVICE_ID
left join
T3 cm
on c.DEVICE_ID=cm.ASSET_ID
left join
T4 gm
on cm.GROUP_ID=gm.GROUP_ID
left join
T5 em
on w.EVENT_ID =em.EVENT_ID
and w.EVENT_CATEGORY_ID=em.EVENT_CATEGORY_ID
left join T6 sa
on w.PROCESS_NAME=sa.PROCESS_NAME
where
w.ASSET_ID in (
'{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}',
'{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}',
……
'{XXXXXXXX-XXXX
只打印了一部分。
当我使用sp_executesql @SQLString_1
错误是:
Message 105, level 15, state 1, and line 96 The quote is not closed after the string '{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}'.
Message 102, level 15, state 1, line 96 There is an incorrect syntax in the vicinity of '{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}'.
我在@SQLString_1
,
中使用了关键字max
所以我不能用execute sp_executesql`,好像不行。
我该如何解决?
关于您问题的打印部分,print
限制为 4000 个符号。如果你想在控制台中转储更长的文本,你可以像这样在块上打印它:
print substring(@SQLString_1, 1, 4000)
print substring(@SQLString_1, 4001, 4000)
print substring(@SQLString_1, 8001, 4000)
根据需要添加尽可能多的 4k 块。每个命令都会在一个新行上打印它的块,所以你的查询会有点混乱,但这些额外的换行符很容易找到和修复:
left join T6 sa
on w.PROCES <-- Find this place and join the lines together, to get the original query
S_NAME=sa.PROCESS_NAME
where
关于查询错误,您的查询字符串看起来被转换为非最大类型,因此被截断为 4000 个字符。检查你的串联。如有必要,将非最大部分转换为 nvarchar(max)。
我的SQL服务器版本是SQLServer 2008 R2。
in
语句中大约有 100 行。
我用这个SQL做一个动态查询:
DECLARE @DynamicDate nvarchar(100)
DECLARE @SQLString_1 nvarchar(max)
SET @DynamicDate='20181021'
SET @SQLString_1 =
N'select *
from T1_'+@DynamicDate+' w
left join
T2 c
on w.ASSET_ID=c.DEVICE_ID
left join
T3 cm
on c.DEVICE_ID=cm.ASSET_ID
left join
T4 gm
on cm.GROUP_ID=gm.GROUP_ID
left join
T5 em
on w.EVENT_ID =em.EVENT_ID
and w.EVENT_CATEGORY_ID=em.EVENT_CATEGORY_ID
left join T6 sa
on w.PROCESS_NAME=sa.PROCESS_NAME
where
w.ASSET_ID in (''{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}'',
''{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}'',
''{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}'',
''{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}'',
……
''{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}'',
''{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}'',
''{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}'',
''{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}'',
''{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}''
)
'
print(@SQLString_1)
--EXECUTE sp_executesql @SQLString_1
当我打印 sql 时。 输出如下:
select
*
from T1_20181021 w
left join
T2 c
on w.ASSET_ID=c.DEVICE_ID
left join
T3 cm
on c.DEVICE_ID=cm.ASSET_ID
left join
T4 gm
on cm.GROUP_ID=gm.GROUP_ID
left join
T5 em
on w.EVENT_ID =em.EVENT_ID
and w.EVENT_CATEGORY_ID=em.EVENT_CATEGORY_ID
left join T6 sa
on w.PROCESS_NAME=sa.PROCESS_NAME
where
w.ASSET_ID in (
'{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}',
'{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}',
……
'{XXXXXXXX-XXXX
只打印了一部分。
当我使用sp_executesql @SQLString_1
错误是:
Message 105, level 15, state 1, and line 96 The quote is not closed after the string '{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}'. Message 102, level 15, state 1, line 96 There is an incorrect syntax in the vicinity of '{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}'.
我在@SQLString_1
,
max
所以我不能用execute sp_executesql`,好像不行。
我该如何解决?
关于您问题的打印部分,print
限制为 4000 个符号。如果你想在控制台中转储更长的文本,你可以像这样在块上打印它:
print substring(@SQLString_1, 1, 4000)
print substring(@SQLString_1, 4001, 4000)
print substring(@SQLString_1, 8001, 4000)
根据需要添加尽可能多的 4k 块。每个命令都会在一个新行上打印它的块,所以你的查询会有点混乱,但这些额外的换行符很容易找到和修复:
left join T6 sa
on w.PROCES <-- Find this place and join the lines together, to get the original queryS_NAME=sa.PROCESS_NAME
where
关于查询错误,您的查询字符串看起来被转换为非最大类型,因此被截断为 4000 个字符。检查你的串联。如有必要,将非最大部分转换为 nvarchar(max)。