1
在T-SQL中從遊標讀取表名的代碼如下。在SQL語句中動態傳遞表名的T-SQL
但我在WITH
語句內的範圍表名稱變量有問題。
我可以運行此代碼時,我明確地將[email protected]_name
設置爲同義詞名稱,如dbo.mysysnonym
,但當我把它作爲變量名稱如[email protected]_name
它不起作用。
-- drop duplicates records from synonyms
DECLARE @syn_name varchar(50)
DECLARE s_cursor CURSOR FOR
SELECT name
FROM sys.synonyms
WHERE base_object_name LIKE 'xyz%'
OPEN s_cursor;
FETCH NEXT FROM s_cursor INTO @syn_name;
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM s_cursor INTO @syn_name;
WITH dedupTable AS
(
SELECT
sys_id,
row_number() OVER (PARTITION BY sys_id ORDER BY sys_id) AS nr
FROM
[email protected]_name
)
DELETE FROM dedupTable
WHERE nr > 1
END;
CLOSE s_cursor
DEALLOCATE s_cursor