在现有约束/规则/过程上更改Quoted_Identifier

时间:2014-01-22 07:41:58

标签: sql-server tsql sql-server-2008-r2 sql-server-2012 quoted-identifier

我目前正在使用2008-r2服务器上的旧数据库,该服务器使用大量使用引用标识符设置为关闭的对象。

我主要关注这些类型:

CHECK_CONSTRAINT
DEFAULT_CONSTRAINT
RULE
SQL_SCALAR_FUNCTION
SQL_STORED_PROCEDURE
SQL_TRIGGER
VIEW

我现在正在尝试更改引用标识符设置,当我发现我甚至无法改变约束时,我立刻就难倒了。

对于约束:我认为我必须以某种方式制作临时克隆/复制,删除原始,然后使用副本和Quoted_Identifier设置为ON重新创建它们,但我真的不知道如何做到这一点或者如何自动化这一点,因为我的SQL技能有限。有人能帮助我吗?或者有人知道更容易的替代方法吗?

3 个答案:

答案 0 :(得分:5)

我在安装过程中遇到一个错误,导致QUOTED_IDENTIFIER在大量对象上随机打开/关闭(问题跨越了过程,函数,触发器和视图......)。

由于使用过滤索引和查询通知之类的东西需要QUOTED_IDENTIFIER ON,我想找到一个方法来查找它关闭的所有内容并将其打开。

在研究这个网站上的问题时,我发现了这个(以及其他一些)帖子,但是我找不到任何一个没有重新生成所有SQL脚本的好方法(我确实有数千个需要的对象)固定)或编写C#代码。

所以我开发了一种基于SQL的方法来处理它。这会重新编译所有的proc,并生成一个由于某种原因无法编译的列表。我知道这不能处理不同的模式(dbo与销售与其他方面),但你可以根据需要调整它。在我的情况下,我不需要担心。

    SET NOCOUNT ON

    -- MAKE SURE THIS IS ON!!
    SET QUOTED_IDENTIFIER ON

    --- Used in try/catch below
    DECLARE @ErrorMessage nvarchar(4000);
    DECLARE @ErrorSeverity int;
    DECLARE @ErrorState int;

    DECLARE @name sysname
    DECLARE @type char(2)
    DECLARE @objType nvarchar(50)
    DECLARE @createCommand nvarchar(max)
    DECLARE @dropCommand nvarchar(max)
    DECLARE @success bit

    IF OBJECT_ID(N'tempdb..#ProcList', N'U') IS NOT NULL DROP TABLE #ProcList

    CREATE TABLE #ProcList
    (
        name            sysname         NOT NULL PRIMARY KEY,
        id              int             NOT NULL,
        type            char(2)         NOT NULL,
        definition      nvarchar(max)   NULL,
        alterstmt       nvarchar(max)   NULL,
        processed       bit             NOT NULL,
        successful      bit             NOT NULL
    )

    --- Build the list of objects that have quoted_identifier off
    INSERT INTO #ProcList
    SELECT 
        so.name, 
        so.object_id, 
        so.type, 
        sm.definition, 
        NULL, 
        0, 
        0
    FROM sys.objects so
        INNER JOIN sys.sql_modules sm
            ON so.object_id = sm.object_id
    WHERE 
        LEFT(so.name, 3) NOT IN ('sp_', 'xp_', 'ms_')
        AND sm.uses_quoted_identifier = 0
    ORDER BY 
        name

    -- Get the first object
    SELECT @name = MIN(name) FROM #ProcList WHERE processed = 0

    --- As long as we have one, keep going
    WHILE (@name IS NOT NULL)
    BEGIN

        SELECT
            @createCommand = definition,
            @type = type
        FROM #ProcList 
        WHERE name = @name

        --- Determine what type of object it is
        SET @objType = CASE @type
                WHEN 'P'  THEN 'PROCEDURE' 
                WHEN 'TF' THEN 'FUNCTION'
                WHEN 'IF' THEN 'FUNCTION'
                WHEN 'FN' THEN 'FUNCTION'
                WHEN 'V'  THEN 'VIEW'
                WHEN 'TR' THEN 'TRIGGER'
            END

        --- Create the drop command
        SET @dropCommand = 'DROP ' + @objType + ' ' + @name

        --- record the drop statement that we are going to execute
        UPDATE #ProcList 
        SET 
            processed = 1, 
            alterstmt = @dropCommand 
        WHERE name = @name

        --- Assume we will not succeed
        SET @success = 0

        BEGIN TRANSACTION

        --- Drop the current proc
        EXEC sp_executesql @dropCommand

        BEGIN TRY

            --- Execute the create statement from the definition
            EXEC sp_executesql @createCommand

            --- If we reached this point, it all worked
            SET @success = 1

            COMMIT

        END TRY
        BEGIN CATCH

            --- oops something went wrong
            SELECT
                @ErrorMessage = ERROR_MESSAGE(),
                @ErrorSeverity = ERROR_SEVERITY(),
                @ErrorState = ERROR_STATE();

            PRINT 'Error processing ' + @name
            RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState, @name)

            --- Undo the transaction, which undoes the drop above
            ROLLBACK

        END CATCH

        --- At this point, there should be no open transactions
        IF @@TRANCOUNT > 0 
        BEGIN
            PRINT 'ERROR... transaction count not right!!'
            ROLLBACK
            RETURN
        END

        --- check to make sure the object still exists after executing the alter statement, and that we didn't detect an earlier error
        --- If it's all good, then mark the proc as having been successful
        IF (
            @success = 1
            AND EXISTS (
                SELECT name 
                FROM sys.objects so 
                    INNER JOIN sys.sql_modules sm 
                        ON so.object_id = sm.object_id 
                WHERE name = @name
            )
        )
        BEGIN
            UPDATE #ProcList SET successful = 1 WHERE name = @name
        END      

        -- Get the next one... if none are left the result will be NULL
        SELECT @name = MIN(name) FROM #ProcList where processed = 0

    END

    -- What wasn't successful??
    SELECT * 
    FROM #ProcList 
    WHERE successful = 0 
    ORDER BY name 

答案 1 :(得分:0)

编写此数据库的脚本,删除所有Quoted_Identifier OFF,从脚本重新创建数据库并重新导入数据(该向导包含在MS SS中)。

这种解决方案可能有很多种风味。用动态sql / smo技术解决了类似的问题: Change the ANSI_NULLS setting for all Stored Procedures in the Database

答案 2 :(得分:0)

我已经更改了@Earl 编写的脚本(见上文),以便它执行 CREATE OR ALTER,并且也适用于 SQL Server 2016 及更高版本的架构。

仍然不完美,因为找到“CREATE”部分并不简单。

*