[英]Create/Amend multiple objects (within a sproc) with different QUOTED_IDENTIFIER values
As a development aid, I am writing a stored procedure which creates/amends database objects from one database into another one.作为开发辅助,我正在编写一个存储过程,该过程将数据库对象从一个数据库创建/修改到另一个数据库。 (The sproc takes a CSV string of object names, which it splits into individual values using XML, and therefore the
QUOTED_IDENTIFIER
needs to be turned on for the sproc to run.) (存储过程采用 CSV 字符串 object 名称,它使用 XML 将其拆分为单独的值,因此需要为运行过程打开
QUOTED_IDENTIFIER
。)
However, the objects being created/amended include stored procedures where QUOTED_IDENTIFIER
could be turned either on or off.但是,正在创建/修改的对象包括可以打开或关闭
QUOTED_IDENTIFIER
的存储过程。
According to the answer to this very similar question (which talks specifically about creating a single stored procedure) if you create/amend a stored procedure within another stored procedure, it will always use the QUOTED_IDENTIFIER
values set in the "parent" sproc.根据这个非常相似的问题的答案(专门讨论创建单个存储过程),如果您在另一个存储过程中创建/修改存储过程,它将始终使用“父”存储过程中设置的
QUOTED_IDENTIFIER
值。
Does anybody know of a way to be able to set different QUOTED_IDENTIFIER
flag values when creating/amending multiple stored procedures?有人知道在创建/修改多个存储过程时能够设置不同的
QUOTED_IDENTIFIER
标志值的方法吗?
I've tried very simple code within the loop (such as the following) but as the above answer suggests, this has no effect on the result and the created/amended sproc always has QUOTED_IDENTIFIER
set to ON
...我在循环中尝试了非常简单的代码(如下所示),但正如上面的答案所暗示的,这对结果没有影响,并且创建/修改的 sproc 总是将
QUOTED_IDENTIFIER
设置为ON
...
IF @QUOTED = 1 -- from sys.sql_modules.uses_quoted_identifier
SET QUOTED_IDENTIFIER ON
ELSE
SET QUOTED_IDENTIFIER OFF
EXEC sp_executesql @DEFINITION -- from sys.sql_modules.definition
With many thanks to @Jeroen who sent me down the path to the solution, which is to create another development-aid stored procedure with QUOTED_IDENTIFIER
set to OFF
.非常感谢@Jeroen ,他让我找到了解决方案,即创建另一个开发辅助存储过程,并将
QUOTED_IDENTIFIER
设置为OFF
。
Within the main loop of the primary development-aid sproc it executes the definition through itself (if ON
is required) or through the secondary sproc (if OFF
is required).在主要开发辅助存储区的主循环中,它通过自身(如果需要
ON
)或通过辅助存储区(如果需要OFF
)执行定义。
This is a very simplified pseudo version of what I now have working...这是我现在工作的一个非常简化的伪版本......
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE DEV_AID_SUB
@DEFINITION NVARCHAR(MAX)
AS
EXEC sp_executesql @DEFINITION
---
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE DEV_AID
AS
BEGIN
WHILE @LOOP = 1
IF @QUOTED = 1
EXEC sp_executesql @DEFINITION
ELSE
EXEC DEV_AID_SUB @DEFINITION
END
END
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.