Synonyms in SQL Server 2005

http://nayyeri.net/archive/2007/08/27/synonyms-in-sql-server-2005.aspx

Naming conventions are always a big concern for developers whether they're an application developer or database developers!  Naming tables, columns, views, stored procedures and other objects in a database sometimes gets harder than what we think, especially when we're working on multiple objects with same name from two different databases, tables or ...

SQL Server 2005 introduced a new feature for synonyms that come handy to assign alternative names for a database object in order to work with it easier.  CREATE SYNONYM is a new statement that lets you assign these alternative names.

You can define a synonym on a two-part, three-part or four-part object name.  The scope of synonyms is limited to the database where they're defined in!  Synonyms can be defined for some objects like:

  • Table
  • View
  • Stored Procedure
  • Function
  • Replication filter procedure
  • Extended stored procedure

Let me give an example.  Suppose that I have a database named MyDB with a table named MyTable.  This table contains three columns: ID, Name and Age.  I create some sample data in this table.

Now I can assign an alternative name to MyTable like AltMyTable as you see.

CREATE SYNONYM dbo.AltMyTable FOR MyTable
SELECT * FROM AltMyTable

This simply returns data from MyTable.

Output

As another example, I also create a simple stored procedure to select data from MyTable and name it SelectData.

IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'SelectData')
BEGIN
DROP Procedure Stored_Procedure_Name
END
GO
CREATE Procedure SelectData
AS
SELECT * FROM MyTable
GO

Now I assign a new name like SelectMyData to this stored procedure.

CREATE SYNONYM dbo.SelectMyData FOR SelectData
EXECUTE SelectMyData

Output

You can also check your synonyms from the explorer easily.

Snap3

转载于:https://www.cnblogs.com/Glen/archive/2007/08/28/872420.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值