I wanted to drop all the synonyms of a database (sql server 2008 r2) using cursor. environment-database name- 'mydatabase', schema name- 'dbo'.. Can you please guide me as i did try but the statement of while .. end, is not able to drop the synonym. what logic should be apply w.r.t cursor?
To drop a private synonym, either the synonym must be in your own schema or you must have the DROP ANY SYNONYM system privilege. To drop a PUBLIC synonym, you must have the DROP PUBLIC SYNONYM system privilege. You must specify PUBLIC to drop a public synonym. You cannot specify schema if you have specified PUBLIC .
The syntax to drop a synonym in Oracle is: DROP [PUBLIC] SYNONYM [schema .] synonym_name [force];
No need to use a cursor. Do it as set:
declare @n char(1)
set @n = char(10)
declare @stmt nvarchar(max)
select @stmt = isnull( @stmt + @n, '' ) +
'drop synonym [' + SCHEMA_NAME(schema_id) + '].[' + name + ']'
from sys.synonyms
exec sp_executesql @stmt
Similar to Jason's answer with some improvements
Totally agree this is not something where you need a cursor.
DECLARE @SQL NVARCHAR(MAX) = N''
SELECT @SQL += N'DROP SYNONYM ' + QUOTENAME(SCHEMA_NAME([schema_id])) + N'.' + QUOTENAME(name) + N';' + Char(13) + Char(10)
FROM sys.synonyms
WHERE SCHEMA_NAME([schema_id]) = N'dbo'
EXEC sys.sp_executesql @SQL
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With