简体   繁体   English

替换语句:字符串或二进制数据将被截断 - NVARCHAR(最大)

[英]Replace Statement: String or binary data would be truncated - NVARCHAR(max)

I had a procedure that previously took the string from a stored procedure in my database and replaced certain tables and removed large chunks of the procedure via a long replace function. 我有一个过程,以前从我的数据库中的存储过程中取出字符串,并替换某些表并通过长替换函数删除过程的大块。 I would then create a new procedure using this manipulated string 然后,我将使用此操纵的字符串创建一个新过程

It worked for about 2 months but recently I'm getting the error message 它工作了大约2个月,但最近我收到了错误消息

Msg 8152, Level 16, State 10, Line 2 Msg 8152,Level 16,State 10,Line 2
String or binary data would be truncated 字符串或二进制数据将被截断

I've tried altering the data types and also removing every single replace statement but it errors out on the first Replace Statement which makes me think its an issue with the datatypes or my initial manipulation. 我已经尝试更改数据类型并删除每个替换语句,但它在第一个替换语句上出错,这使我认为它是数据类型或我的初始操作的问题。

Any help would be much appreciated. 任何帮助将非常感激。

DECLARE @PriorityProcedureString AS VARCHAR(MAX)

SET @PriorityProcedureString = 
(SELECT 
     /*1*/REPLACE(
     /*1*/OBJECT_DEFINITION(object_id(ROUTINE_NAME)),
    (SUBSTRING (OBJECT_DEFINITION(object_id(ROUTINE_NAME)), -- String 
                CHARINDEX('µ',OBJECT_DEFINITION(object_id(ROUTINE_NAME))), -- Start_Position
                (CHARINDEX('!',OBJECT_DEFINITION(object_id(ROUTINE_NAME))) - CHARINDEX('µ',OBJECT_DEFINITION(object_id(ROUTINE_NAME))))-- Length
                )
    )   
                                                                                    ,''
                                                                                    )
            FROM INFORMATION_SCHEMA.ROUTINES
            WHERE specific_name LIKE 'usp_Cs_Coll_NBA_Priority_And_Exclusions'
)

SELECT @PriorityProcedureString

-- EXEC @PriorityProcedureString

This is because the length of some of your code is longer than 8,000 characters. 这是因为某些代码的长度超过了8,000个字符。 CHARINDEX has an 8,000 character limit. CHARINDEX有8,000个字符的限制。

https://docs.microsoft.com/en-us/sql/t-sql/functions/charindex-transact-sql?view=sql-server-2017 https://docs.microsoft.com/en-us/sql/t-sql/functions/charindex-transact-sql?view=sql-server-2017

Apologies for the original question realize it wasn't very clear what the issue was. 对原始问题的道歉意识到问题是什么并不是很清楚。 I wasn't able to identify the problem but suspect that the REPLACE function potentially has a limit of characters and once the first REPALCE was resolved it was potentially truncating my string before moving onto the next REPLACE? 我无法识别问题,但怀疑REPLACE函数可能有字符限制,一旦第一个REPALCE被解析,它可能会在转移到下一个REPLACE之前截断我的字符串?

Instead I've put it into a variable and done the replace statement in steps, this does not give me any errors. 相反,我把它放入变量并按步骤完成替换语句,这不会给我任何错误。

The basic goal of this code was to just take an existing live procedure that populates business critical tables and replace all the tables in it with dummy or simulation tables so that I can mess around with different logic output results and not affect any lives processes. 此代码的基本目标是仅使用现有的实时过程来填充业务关键表,并用虚拟表或模拟表替换其中的所有表,以便我可以使用不同的逻辑输出结果,而不会影响任何生命过程。 I wanted to do it this way because the procedure is altered by different SQL users on a daily basis so manually scripting it out would leave the 'Simulation' procedure out of date. 我想这样做,因为每天都会由不同的SQL用户更改过程,因此手动编写脚本会使“模拟”过程过时。

If anyone has a more efficient way of doing this appreciate if you could share as the below solution is cumbersome and requires manual updating whenever new tables are added. 如果有人能够更有效地进行这种欣赏,如果你可以分享,因为下面的解决方案很麻烦,并且每当添加新表时都需要手动更新。

/******************************************************************************************
    * Put Procedure Text Into String *
    *******************************************************************************************/

SET @Priority_ExecutableString = (
                       SELECT OBJECT_DEFINITION(object_id(ROUTINE_NAME))
                       FROM INFORMATION_SCHEMA.ROUTINES
                       WHERE specific_name LIKE 'usp_Cs_Coll_NBA_Priority_And_Exclusions'
                       )

/******************************************************************************************
*   Use Substring To Remove Parts Of String *
* As defined by the µ start and ! End Points In The Original Procedure
*******************************************************************************************/

SET @StringIWantToRemove = 
(
SELECT SUBSTRING (@Priority_ExecutableString, -- String 
CHARINDEX('µ',@Priority_ExecutableString), -- Start_Position
(CHARINDEX('!',@Priority_ExecutableString) - CHARINDEX('µ',@Priority_ExecutableString)))-- Length
)



   SET @Priority_ExecutableString = (SELECT REPLACE(@Priority_ExecutableString, @StringIwantToRemove,''))

/**************************************************************************************

    ****
    *   Step 2 : Replace Defaulted 'Create Procedure' With 'Alter Procedure'
    *******************************************************************************************/

SET @Priority_ExecutableString = (SELECT REPLACE(@Priority_ExecutableString, 'Create Procedure', 'ALTER PROCEDURE'))

/******************************************************************************************
*   Step 3 : Replace Original Procedure Name With Simulation Procedure Name
*******************************************************************************************/

SET @Priority_ExecutableString = (SELECT REPLACE(@Priority_ExecutableString, '[dbo].[usp_Cs_Coll_NBA_Priority_And_Exclusions]','[dbo].[usp_Cs_Coll_NBA_Priority_And_Exclusions_Simulation]'))

/******************************************************************************************
*   Step 4 : Replace Offer_Stage Table With Offer_Stage Simulation Table
*******************************************************************************************/

SET @Priority_ExecutableString = (SELECT REPLACE(@Priority_ExecutableString, 'WORKDBOPS_NBA.dbo.t_CS_Coll_NBA_offer_stage','WORKDBOPS_NBA.dbo.t_CS_Coll_NBA_offer_stage_Simulation'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 nvarchar max给出“字符串或二进制数据将被截断” - nvarchar max gives 'String or binary data would be truncated' 字符串或二进制数据将在Varbinary(MAX)中被截断 - String or binary data would be truncated in Varbinary(MAX) “字符串或二进制数据将被截断。” 用于 NVARCHAR 但不是 LIKE 操作中的 VARCHAR - "String or binary data would be truncated." for NVARCHAR but not VARCHAR in LIKE operation SQL Server:字符串或二进制数据将被截断。 nvarchar - SQL SERVER: String or binary data would be truncated. nvarchar 字符串或二进制数据将在INSERT语句中被截断 - String or binary data would be truncated on INSERT statement 更新语句中的“字符串或二进制数据将被截断” - “String or binary data would be truncated” in update statement 字符串或二进制数据将被截断,但是所有内容都是varchar(max)吗? - String or binary data would be truncated, but everything is varchar(max)? 字符串或二进制数据将被截断 - String or binary data would be truncated 条件语句错误的条件排序:字符串或二进制数据将被截断 - Conditional order by case statement error : String or binary data would be truncated 字符串或二进制数据将被截断。 该语句已终止 - String or binary data would be truncated. The statement has been terminated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM