DLA*_*014 2 t-sql sql-server quoted-identifier sql-server-2008-r2 sql-server-2012
我目前正在使用2008-r2服务器上的旧数据库,该数据库使用大量使用引用标识符设置为关闭的对象.
我主要看这些类型:
CHECK_CONSTRAINT
DEFAULT_CONSTRAINT
RULE
SQL_SCALAR_FUNCTION
SQL_STORED_PROCEDURE
SQL_TRIGGER
VIEW
Run Code Online (Sandbox Code Playgroud)
我现在正试图改变引用标识符设置,当我发现我甚至无法改变约束时,我立刻就难倒了.
对于约束:我认为我必须以某种方式制作临时克隆/复制,删除原始,然后使用副本和Quoted_Identifier设置为ON重新创建它们,但我真的不知道如何做到这一点或如何因为我的SQL技能有限,所以自动执行此操作.有人能帮助我吗?或者有人知道更容易的替代方法吗?
小智 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3056 次 |
| 最近记录: |