Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql Server 2008 - Dropping a synonym

I have a utility database (customers) on my db server where I store all of the routines used to modify data on the other databases. We recently found out that using synonyms will greatly benefit us.

use Customers   
IF EXISTS (SELECT * FROM employees.sys.synonyms WHERE name = 'tblPerson2') begin
drop synonym [dbo].tblPerson2
end

This doesn't work because I am using the Customers database but need to drop the synonym from my employees database.

SQL Server 2008 doesn't support this syntax -

drop synonym [employees].[dbo].tblPerson2

Anyone have any ideas on how to modify synonyms accross databases. My solution involves having to add an identical stored procedure to every database, which seems prone to error.

like image 952
duckmike Avatar asked Oct 19 '11 12:10

duckmike


People also ask

How do you drop a synonym in SQL Server?

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 .

What is the syntax to drop synonym?

The syntax to drop a synonym in Oracle is: DROP [PUBLIC] SYNONYM [schema .] synonym_name [force];

How do you modify synonyms in SQL Server?

Use the ALTER SYNONYM statement to modify an existing synonym. To modify a private synonym in another user's schema, you must have the CREATE ANY SYNONYM and DROP ANY SYNONYM system privileges. To modify a PUBLIC synonym, you must have the CREATE PUBLIC SYNONYM and DROP PUBLIC SYNONYM system privileges.

What is the syntax for synonym in SQL?

The syntax for creating a Synonym in SQL Server Database is:CREATE SYNONYM [ name_of_schema. ]


1 Answers

EXEC('USE employees;
DROP SYNONYM [dbo].tblPerson2;')
like image 119
Martin Smith Avatar answered Sep 20 '22 20:09

Martin Smith