【发布时间】:2016-09-29 13:16:41
【问题描述】:
总结:EXEC sp_executesql @code 对于@code 中超过 4000 的内容会失败,但 @code 不会被截断为 4000 个 unicode 字符。
我在 SQL Server 2014 Developer Edition 上观察到问题。
更多细节:我的 SQL 安装脚本动态定义了一些代码,因为它应该修改代码以反映环境(仅一次,在安装期间)。让以下@datasource 变量捕获特定环境的结果:
DECLARE @datasource nvarchar(100) = N'testdb.dbo.source_table'
@code变量被声明为nvarchar(max)类型,REPLACE函数用于根据需要修改字符串(即用@datasource内容替换占位符)——见sn-p 下面。
在 Management Studio 中使用@code 执行sp_executesql 时,会显示以下错误:
消息 156,级别 15,状态 1,过程 my_sp,第 86 行
关键字“AS”附近的语法不正确。
消息 102,级别 15,状态 1,过程 my_sp,第 88 行
'WHERE' 附近的语法不正确。
下面的 sn-p 是上述方式失败的代码的精确副本(待复制)。功能可能并不重要——可能只是代码的长度。 @code 内容显然被sp_executesql 截断;但是,它不应该是(见下文):
-- ... repeated from above
DECLARE @datasource nvarchar(100) = N'testdb.dbo.source_table'
DECLARE @code nvarchar(MAX) = REPLACE(N'
-- Comment comment comment comment comment comment comment comment comment.
-- Comment comment comment comment comment comment comment comment comment.
-- Comment comment comment comment comment comment comment comment comment.
CREATE PROCEDURE dbo.my_sp
AS
BEGIN
SET NOCOUNT ON
DECLARE @result int = -555 -- Comment comment comment comment comment.
-- Comment comment comment comment comment comment comment comment comment.
-- Comment comment comment comment comment comment comment comment comment.
DECLARE @info_table TABLE (
action nvarchar(10), -- Comment comment comment comment comment
firmaID int, -- Comment comment comment comment comment
kod numeric(8, 0), -- Comment comment comment comment comment
oz1 nvarchar(40), -- Comment comment comment comment comment
oz2 nvarchar(40), -- Comment comment comment comment comment
oz3 nvarchar(40),
oz4 nvarchar(40)
)
-- Comment comment comment comment comment comment comment comment comment.
BEGIN TRANSACTION tran_firmy
BEGIN TRY
MERGE dbo.firmy AS target
USING (SELECT kod, ico, dic, nazev,
oz1, oz2, oz3, oz4,
jeaktivni,
ulice, mesto, psc
FROM @datasource) AS source
ON target.kod = source.kod
WHEN MATCHED AND (COALESCE(target.ico, '''') != COALESCE(source.ico, '''')
OR COALESCE(target.dic, '''') != COALESCE(source.dic, '''')
OR COALESCE(target.nazev, '''') != COALESCE(source.nazev, '''')
OR COALESCE(target.nepouzivat_oz1, '''') != COALESCE(source.oz1, '''')
OR COALESCE(target.nepouzivat_oz2, '''') != COALESCE(source.oz2, '''')
OR COALESCE(target.nepouzivat_oz3, '''') != COALESCE(source.oz3, '''')
OR COALESCE(target.nepouzivat_oz4, '''') != COALESCE(source.oz4, '''')
OR COALESCE(target.jeaktivni, 0) != COALESCE(source.jeaktivni, 0)
OR COALESCE(target.ulice, '''') != COALESCE(source.ulice, '''')
OR COALESCE(target.mesto, '''') != COALESCE(source.mesto, '''')
OR COALESCE(target.psc, '''') != COALESCE(source.psc, '''')
) THEN
UPDATE
SET target.ico = source.ico,
target.dic = source.dic,
target.nazev = source.nazev,
target.nepouzivat_oz1 = source.oz1,
target.nepouzivat_oz2 = source.oz2,
target.nepouzivat_oz3 = source.oz3,
target.nepouzivat_oz4 = source.oz4,
target.jeaktivni = source.jeaktivni,
target.ulice = source.ulice,
target.mesto = source.mesto,
target.psc = source.psc,
target.changed = GETDATE(),
target.changedby = ''dialog''
WHEN NOT MATCHED THEN
INSERT (kod, ico, dic, nazev,
nepouzivat_oz1, nepouzivat_oz2, nepouzivat_oz3, nepouzivat_oz4,
jeaktivni,
ulice, mesto, psc,
created, createdby)
VALUES (source.kod, source.ico, source.dic, source.nazev,
source.oz1, source.oz2, source.oz3, source.oz4,
source.jeaktivni,
source.ulice, source.mesto, source.psc,
GETDATE(), ''dialog'')
OUTPUT
$action AS action, -- INSERT or UPDATE
inserted.ID AS firmaID,
inserted.kod AS kod,
inserted.nepouzivat_oz1 AS oz1,
inserted.nepouzivat_oz2 AS oz2,
inserted.nepouzivat_oz3 AS oz3,
inserted.nepouzivat_oz4 AS oz4
INTO @info_table;
-- Comment comment comment comment comment comment comment comment comment.
-- Comment comment comment comment comment comment comment comment comment.
SET @result = @@ROWCOUNT
-- Comment comment comment comment comment comment comment comment comment.
-- Comment comment comment comment comment comment comment comment comment.
DELETE FROM obchodni_zastupci AS ozt
WHERE ozt.kod IN (
SELECT kod FROM @info_table AS it WHER it.action = ''UPDATE''
)
-- Comment comment comment comment comment comment comment comment comment.
-- Comment comment comment comment comment comment comment comment comment.
UPDATE dodaci_adresy
SET custID = f.ID
FROM firmy AS f, dodaci_adresy AS da
WHERE da.custID IS NULL AND f.kod = da.kod_firmy
COMMIT TRANSACTION tran_firmy
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION tran_firmy
SET @result = -1 -- Comment comment comment comment comment comment comment comment comment.
END CATCH
RETURN @result -- Comment comment comment comment comment comment comment comment comment.
END', N'@datasource', N'testdb.dbo.source_table')
-- The following prints only show that the full-length string is there
PRINT SUBSTRING(@code, 0, 4000)
PRINT '-----------------------------------------------------------'
PRINT SUBSTRING(@code, 4000, 10000)
EXEC sp_executesql @code
-- The following command also does not work (uncomment it).
-- EXEC(@code)
-- Even splitting to two variables and passing the concatenation
-- does not work.
-- DECLARE @code1 nvarchar(MAX) = SUBSTRING(@code, 0, 4000)
-- DECLARE @code2 nvarchar(MAX) = SUBSTRING(@code, 4000, 10000)
-- EXEC(@code1 + @code2)
注意两个PRINT 命令。第一个打印前 4000 个字符,第二个打印其余字符。它在行的中间被剪切,但它仅用于表明@code 确实包含完整的字符串。
sp_executesql (Transact-SQL) 的文档说:
[@stmt=] 声明
[...] 字符串的大小仅受可用数据库服务器的限制 记忆。在 64 位服务器上,字符串的大小限制为 2 GB, nvarchar(max) 的最大大小。
我在其他地方找到了使用EXEC(@code) 的提示,它没有sp_executesql 的限制。但是,它与文档的上述引用部分相矛盾。而且EXEC(@code)也不起作用。
当替换后的相同内容复制/粘贴到SQL控制台时,它可以工作(即创建过程)。
如何破案?
【问题讨论】:
-
您是在寻求解决方案来解决问题,还是在询问(如您的标题中所暗示的)
sp_ExecuteSQL的内部工作原理以及它是否真正使用nvarchar (max)?您的问题似乎是问后者,但您的结束问题How to solve the case?暗示前者。你问的是哪个? -
首先,我需要找到任何解决方案。其次,我需要澄清。医生说它应该可以工作,但它不起作用。我已经更新了 sn-p 的结尾——替代解决方案也不起作用。请尝试复制/粘贴查看。
标签: sql sql-server sql-server-2014 nvarchar limits