我目前正在使用SQL Server 2008 R2
DECLARE @synName VARCHAR(100), @synTarget VARCHAR(150), @synSchemaName VARCHAR (110)
DECLARE Syns CURSOR LOCAL STATIC FOR
SELECT
s.name, base_object_name, sc.name AS synSchemaName
FROM
sys.synonyms s
INNER JOIN
sys.schemas sc ON s.schema_id = sc.schema_id
WHERE
s.name in ('bunt')
OPEN Syns
FETCH NEXT FROM Syns INTO @synName, @synTarget, @synSchemaName
WHILE @@FETCH_STATUS <> -1
BEGIN
PRINT 'DROP SYNONYM guest.' + @synName
EXEC ('DROP SYNONYM guest.' + @synName)
SET @synTarget = CASE
WHEN @synTarget LIKE '%ChardCake2%'
THEN REPLACE(@synTarget, 'ChardCake2', 'ChardCake3')
WHEN @synTarget LIKE '%ChardCake3%'
THEN REPLACE(@synTarget, 'ChardCake3', 'ChardCake4')
WHEN @synTarget LIKE '%ChardCake4%'
THEN REPLACE(@synTarget, 'ChardCake4', 'ChardCake2')
END
PRINT 'CREATE SYNONYM guest.' + @synName + ' FOR ' + @synTarget
EXEC ('CREATE SYNONYM guest.' + @synName + ' FOR ' + @synTarget)
PRINT 'TRUNCATE TABLE'+@syntarget
EXEC ('TRUNCATE TABLE'+@syntarget)
FETCH NEXT FROM Syns INTO @synName, @synTarget, @synSchemaName
INSERT INTO ChardCake3
SELECT *
FROM ChardCake2
END
CLOSE Syns
DEALLOCATE Syns
以上查询用于在我的数据库中自动删除和创建同义词。我遇到的一个问题是我已经将insert插入到声明中,我试图避免这样做。
我尝试过使用
INSERT INTO @synTarget
SELECT * FROM ChardCake''
并没有表明发生任何事情,而且''语法不正确。
我也尝试过添加
SET @newSynTarget = CASE
WHEN @newSynTarget LIKE '%ChardCake2%'
THEN REPLACE(@curSynTarget, 'ChardCake2', 'ChardCake4')
WHEN @newSynTarget LIKE '%ChardCake3%'
THEN REPLACE(@curSynTarget, 'ChardCake3', 'ChardCake2')
WHEN @newSynTarget LIKE '%ChardCake4%'
THEN REPLACE(@curSynTarget, 'ChardCake4', 'ChardCake3')
END
和
SELECT @sql = 'INSERT INTO ' + quotename(@newSynTarget) +
' SELECT * FROM ' + quotename(@curSynTarget)`
当我在表中截断后也在查询中运行它时,它显示好像什么都没有。
我无法弄清楚如何获得
SELECT @sql = 'INSERT INTO ' + quotename(@newSynTarget) +
' SELECT * FROM ' + quotename(@curSynTarget)
实际上可以在查询和函数中看到。还有什么我需要做才能让它发挥作用。
原创^
use TestingT1
go
DECLARE @synName VARCHAR(100), @synTarget VARCHAR(150), @synSchemaName VARCHAR (110)
DECLARE Syns CURSOR LOCAL STATIC FOR
SELECT s.name, base_object_name, sc.name AS synSchemaName
FROM sys.synonyms s
INNER JOIN sys.schemas sc
ON s.schema_id = sc.schema_id
where s.name in ('bunt')
OPEN Syns
FETCH NEXT FROM Syns INTO @synName, @synTarget, @synSchemaName
WHILE @@FETCH_STATUS <> -1
BEGIN
PRINT 'DROP SYNONYM guest.' + @synName
EXEC ('DROP SYNONYM guest.' + @synName)
SET @synTarget = CASE
WHEN @synTarget LIKE '%ChardCake2%' THEN REPLACE(@synTarget,'ChardCake2','ChardCake3')
WHEN @synTarget LIKE '%ChardCake3%' THEN REPLACE(@synTarget,'ChardCake3','ChardCake4')
WHEN @synTarget LIKE '%ChardCake4%' THEN REPLACE(@synTarget,'ChardCake4','ChardCake2')
END
PRINT 'CREATE SYNONYM guest.' + @synName + ' FOR ' +@synTarget
EXEC ('CREATE SYNONYM guest.' + @synName + ' FOR ' +@synTarget)
PRINT 'TRUNCATE TABLE'+@syntarget
EXEC ('TRUNCATE TABLE'+@syntarget)
FETCH NEXT FROM Syns INTO @synName, @synTarget, @synSchemaName
DECLARE @sql varchar(2000), @newSynTarget varchar(20), @curSynTarget varchar(20) select @sql = '', @newSynTarget = 'newSynTarget', @curSynTarget = 'currentSynTarget'
SET @newSynTarget = CASE
WHEN @newSynTarget LIKE '%ChardCake2%' THEN REPLACE(@curSynTarget, 'ChardCake2', 'ChardCake4')
WHEN @newSynTarget LIKE '%ChardCake3%' THEN REPLACE(@curSynTarget, 'ChardCake3', 'ChardCake2')
WHEN @newSynTarget LIKE '%ChardCake4%' THEN REPLACE(@curSynTarget, 'ChardCake4', 'ChardCake3')
END
SELECT @sql = 'INSERT INTO ' + quotename(@newSynTarget) + ' SELECT * FROM ' + quotename(@curSynTarget) EXEC (@sql)
END
CLOSE Syns
DEALLOCATE Syns
我已将附加信息添加到查询中,但结果仍然与之前运行命令时没有任何反应相同,并且前一个表信息未插入到同义词所指向的当前表中。
例如,我当前的同义词指向ChardCake2,一旦我运行Query,它现在指向ChardCake3并清除表。
目前,在将同义词更改为指向3
之前,该表中包含信息testcolumn testcolumns testing
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
一旦命令运行,这就是表格的样子
testcolumn testcolumns测试
上一个表中包含信息,没有任何内容移到当前表中。