Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 98a43d9

Browse files
authored
Merge pull request #1192 from BYHAM/patch-48
CREATE SYNONYM, formatting
2 parents 6838d0e + a87f2d9 commit 98a43d9

1 file changed

Lines changed: 171 additions & 181 deletions

File tree

Lines changed: 171 additions & 181 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "CREATE SYNONYM (Transact-SQL) | Microsoft Docs"
33
ms.custom: ""
4-
ms.date: "03/14/2017"
4+
ms.date: "04/11/2017"
55
ms.prod: "sql-non-specified"
66
ms.reviewer: ""
77
ms.suite: ""
@@ -30,183 +30,173 @@ manager: "jhubbard"
3030
# CREATE SYNONYM (Transact-SQL)
3131
[!INCLUDE[tsql-appliesto-ss2008-asdb-xxxx-xxx_md](../../includes/tsql-appliesto-ss2008-asdb-xxxx-xxx-md.md)]
3232

33-
Creates a new synonym.
34-
35-
![Topic link icon](../../database-engine/configure-windows/media/topic-link.gif "Topic link icon") [Transact-SQL Syntax Conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
36-
37-
## Syntax
38-
39-
```
40-
41-
-- SQL Server Syntax
42-
43-
CREATE SYNONYM [ schema_name_1. ] synonym_name FOR <object>
44-
45-
<object> :: =
46-
{
47-
[ server_name.[ database_name ] . [ schema_name_2 ]. object_name
48-
| database_name . [ schema_name_2 ].| schema_name_2. ] object_name
49-
}
50-
```
51-
52-
```
53-
54-
-- Windows Azure SQL Database Syntax
55-
56-
CREATE SYNONYM [ schema_name_1. ] synonym_name FOR < object >
57-
58-
< object > :: =
59-
{
60-
[database_name. [ schema_name_2 ].| schema_name_2. ] object_name
61-
}
62-
```
63-
64-
## Arguments
65-
*schema_name_1*
66-
Specifies the schema in which the synonym is created. If *schema* is not specified, [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] uses the default schema of the current user.
67-
68-
*synonym_name*
69-
Is the name of the new synonym.
70-
71-
*server_name*
72-
||
73-
|-|
74-
|**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] through [!INCLUDE[ssCurrent](../../includes/sscurrent-md.md)].|
75-
76-
Is the name of the server on which base object is located.
77-
78-
*database_name*
79-
Is the name of the database in which the base object is located. If *database_name* is not specified, the name of the current database is used.
80-
81-
*schema_name_2*
82-
Is the name of the schema of the base object. If *schema_name* is not specified the default schema of the current user is used.
83-
84-
*object_name*
85-
Is the name of the base object that the synonym references.
86-
87-
Windows Azure SQL Database supports the three-part name format database_name.[schema_name].object_name when the database_name is the current database or the database_name is tempdb and the object_name starts with #.
88-
89-
## Remarks
90-
The base object need not exist at synonym create time. [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] checks for the existence of the base object at run time.
91-
92-
Synonyms can be created for the following types of objects:
93-
94-
|||
95-
|-|-|
96-
|Assembly (CLR) Stored Procedure|Assembly (CLR) Table-valued Function|
97-
|Assembly (CLR) Scalar Function|Assembly Aggregate (CLR) Aggregate Functions|
98-
|Replication-filter-procedure|Extended Stored Procedure|
99-
|SQL Scalar Function|SQL Table-valued Function|
100-
|SQL Inline-table-valued Function|SQL Stored Procedure|
101-
|View|Table<sup>1</sup> (User-defined)|
102-
103-
<sup>1 Includes local and global temporary tables</sup>
104-
105-
Four-part names for function base objects are not supported.
106-
107-
Synonyms can be created, dropped and referenced in dynamic SQL.
108-
109-
## Permissions
110-
To create a synonym in a given schema, a user must have CREATE SYNONYM permission and either own the schema or have ALTER SCHEMA permission.
111-
112-
The CREATE SYNONYM permission is a grantable permission.
113-
114-
> [!NOTE]
115-
> You do not need permission on the base object to successfully compile the CREATE SYNONYM statement, because all permission checking on the base object is deferred until run time.
116-
117-
## Examples
118-
119-
### A. Creating a synonym for a local object
120-
The following example first creates a synonym for the base object, `Product` in the `AdventureWorks2012` database, and then queries the synonym.
121-
122-
```
123-
USE tempdb;
124-
GO
125-
-- Create a synonym for the Product table in AdventureWorks2012.
126-
CREATE SYNONYM MyProduct
127-
FOR AdventureWorks2012.Production.Product;
128-
GO
129-
130-
-- Query the Product table by using the synonym.
131-
USE tempdb;
132-
GO
133-
SELECT ProductID, Name
134-
FROM MyProduct
135-
WHERE ProductID < 5;
136-
GO
137-
```
138-
139-
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
140-
141-
`-----------------------`
142-
143-
`ProductID Name`
144-
145-
`----------- --------------------------`
146-
147-
`1 Adjustable Race`
148-
149-
`2 Bearing Ball`
150-
151-
`3 BB Ball Bearing`
152-
153-
`4 Headset Ball Bearings`
154-
155-
`(4 row(s) affected)`
156-
157-
### B. Creating a synonym to remote object
158-
In the following example, the base object, `Contact`, resides on a remote server named `Server_Remote`.
159-
160-
||
161-
|-|
162-
|**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] through [!INCLUDE[ssCurrent](../../includes/sscurrent-md.md)].|
163-
164-
```
165-
EXEC sp_addlinkedserver Server_Remote;
166-
GO
167-
USE tempdb;
168-
GO
169-
CREATE SYNONYM MyEmployee FOR Server_Remote.AdventureWorks2012.HumanResources.Employee;
170-
GO
171-
```
172-
173-
### C. Creating a synonym for a user-defined function
174-
The following example creates a function named `dbo.OrderDozen` that increases order amounts to an even dozen units. The example then creates the synonym `dbo.CorrectOrder` for the `dbo.OrderDozen` function.
175-
176-
```
177-
-- Creating the dbo.OrderDozen function
178-
CREATE FUNCTION dbo.OrderDozen (@OrderAmt int)
179-
RETURNS int
180-
WITH EXECUTE AS CALLER
181-
AS
182-
BEGIN
183-
IF @OrderAmt % 12 <> 0
184-
BEGIN
185-
SET @OrderAmt += 12 - (@OrderAmt % 12)
186-
END
187-
RETURN(@OrderAmt);
188-
END;
189-
GO
190-
191-
-- Using the dbo.OrderDozen function
192-
DECLARE @Amt int;
193-
SET @Amt = 15;
194-
SELECT @Amt AS OriginalOrder, dbo.OrderDozen(@Amt) AS ModifiedOrder;
195-
196-
-- Create a synonym dbo.CorrectOrder for the dbo.OrderDozen function.
197-
CREATE SYNONYM dbo.CorrectOrder
198-
FOR dbo.OrderDozen;
199-
GO
200-
201-
-- Using the dbo.CorrectOrder synonym.
202-
DECLARE @Amt int;
203-
SET @Amt = 15;
204-
SELECT @Amt AS OriginalOrder, dbo.CorrectOrder(@Amt) AS ModifiedOrder;
205-
```
206-
207-
## See Also
208-
[DROP SYNONYM &#40;Transact-SQL&#41;](../../t-sql/statements/drop-synonym-transact-sql.md)
209-
[GRANT &#40;Transact-SQL&#41;](../../t-sql/statements/grant-transact-sql.md)
210-
[EVENTDATA &#40;Transact-SQL&#41;](../../t-sql/functions/eventdata-transact-sql.md)
211-
212-
33+
Creates a new synonym.
34+
35+
![Topic link icon](../../database-engine/configure-windows/media/topic-link.gif "Topic link icon") [Transact-SQL Syntax Conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
36+
37+
## Syntax
38+
39+
```
40+
-- SQL Server Syntax
41+
42+
CREATE SYNONYM [ schema_name_1. ] synonym_name FOR <object>
43+
44+
<object> :: =
45+
{
46+
[ server_name.[ database_name ] . [ schema_name_2 ]. object_name
47+
| database_name . [ schema_name_2 ].| schema_name_2. ] object_name
48+
}
49+
```
50+
51+
```
52+
-- Windows Azure SQL Database Syntax
53+
54+
CREATE SYNONYM [ schema_name_1. ] synonym_name FOR < object >
55+
56+
< object > :: =
57+
{
58+
[database_name. [ schema_name_2 ].| schema_name_2. ] object_name
59+
}
60+
```
61+
62+
## Arguments
63+
*schema_name_1*
64+
Specifies the schema in which the synonym is created. If *schema* is not specified, [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] uses the default schema of the current user.
65+
66+
*synonym_name*
67+
Is the name of the new synonym.
68+
69+
*server_name*
70+
**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] through [!INCLUDE[ssCurrent](../../includes/sscurrent-md.md)].
71+
72+
Is the name of the server on which base object is located.
73+
74+
*database_name*
75+
Is the name of the database in which the base object is located. If *database_name* is not specified, the name of the current database is used.
76+
77+
*schema_name_2*
78+
Is the name of the schema of the base object. If *schema_name* is not specified the default schema of the current user is used.
79+
80+
*object_name*
81+
Is the name of the base object that the synonym references.
82+
83+
Windows Azure SQL Database supports the three-part name format database_name.[schema_name].object_name when the database_name is the current database or the database_name is tempdb and the object_name starts with #.
84+
85+
## Remarks
86+
The base object need not exist at synonym create time. [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] checks for the existence of the base object at run time.
87+
88+
Synonyms can be created for the following types of objects:
89+
90+
|||
91+
|-|-|
92+
|Assembly (CLR) Stored Procedure|Assembly (CLR) Table-valued Function|
93+
|Assembly (CLR) Scalar Function|Assembly Aggregate (CLR) Aggregate Functions|
94+
|Replication-filter-procedure|Extended Stored Procedure|
95+
|SQL Scalar Function|SQL Table-valued Function|
96+
|SQL Inline-table-valued Function|SQL Stored Procedure|
97+
|View|Table<sup>1</sup> (User-defined)|
98+
99+
<sup>1 Includes local and global temporary tables</sup>
100+
101+
Four-part names for function base objects are not supported.
102+
103+
Synonyms can be created, dropped and referenced in dynamic SQL.
104+
105+
## Permissions
106+
To create a synonym in a given schema, a user must have CREATE SYNONYM permission and either own the schema or have ALTER SCHEMA permission.
107+
108+
The CREATE SYNONYM permission is a grantable permission.
109+
110+
> [!NOTE]
111+
> You do not need permission on the base object to successfully compile the CREATE SYNONYM statement, because all permission checking on the base object is deferred until run time.
112+
113+
## Examples
114+
115+
### A. Creating a synonym for a local object
116+
The following example first creates a synonym for the base object, `Product` in the `AdventureWorks2012` database, and then queries the synonym.
117+
118+
```
119+
-- Create a synonym for the Product table in AdventureWorks2012.
120+
CREATE SYNONYM MyProduct
121+
FOR AdventureWorks2012.Production.Product;
122+
GO
123+
124+
-- Query the Product table by using the synonym.
125+
SELECT ProductID, Name
126+
FROM MyProduct
127+
WHERE ProductID < 5;
128+
GO
129+
```
130+
131+
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
132+
133+
`-----------------------`
134+
135+
`ProductID Name`
136+
137+
`----------- --------------------------`
138+
139+
`1 Adjustable Race`
140+
141+
`2 Bearing Ball`
142+
143+
`3 BB Ball Bearing`
144+
145+
`4 Headset Ball Bearings`
146+
147+
`(4 row(s) affected)`
148+
149+
### B. Creating a synonym to remote object
150+
In the following example, the base object, `Contact`, resides on a remote server named `Server_Remote`.
151+
152+
**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] through [!INCLUDE[ssCurrent](../../includes/sscurrent-md.md)].
153+
154+
```
155+
EXEC sp_addlinkedserver Server_Remote;
156+
GO
157+
USE tempdb;
158+
GO
159+
CREATE SYNONYM MyEmployee FOR Server_Remote.AdventureWorks2012.HumanResources.Employee;
160+
GO
161+
```
162+
163+
### C. Creating a synonym for a user-defined function
164+
The following example creates a function named `dbo.OrderDozen` that increases order amounts to an even dozen units. The example then creates the synonym `dbo.CorrectOrder` for the `dbo.OrderDozen` function.
165+
166+
```
167+
-- Creating the dbo.OrderDozen function
168+
CREATE FUNCTION dbo.OrderDozen (@OrderAmt int)
169+
RETURNS int
170+
WITH EXECUTE AS CALLER
171+
AS
172+
BEGIN
173+
IF @OrderAmt % 12 <> 0
174+
BEGIN
175+
SET @OrderAmt += 12 - (@OrderAmt % 12)
176+
END
177+
RETURN(@OrderAmt);
178+
END;
179+
GO
180+
181+
-- Using the dbo.OrderDozen function
182+
DECLARE @Amt int;
183+
SET @Amt = 15;
184+
SELECT @Amt AS OriginalOrder, dbo.OrderDozen(@Amt) AS ModifiedOrder;
185+
186+
-- Create a synonym dbo.CorrectOrder for the dbo.OrderDozen function.
187+
CREATE SYNONYM dbo.CorrectOrder
188+
FOR dbo.OrderDozen;
189+
GO
190+
191+
-- Using the dbo.CorrectOrder synonym.
192+
DECLARE @Amt int;
193+
SET @Amt = 15;
194+
SELECT @Amt AS OriginalOrder, dbo.CorrectOrder(@Amt) AS ModifiedOrder;
195+
```
196+
197+
## See Also
198+
[DROP SYNONYM &#40;Transact-SQL&#41;](../../t-sql/statements/drop-synonym-transact-sql.md)
199+
[GRANT &#40;Transact-SQL&#41;](../../t-sql/statements/grant-transact-sql.md)
200+
[EVENTDATA &#40;Transact-SQL&#41;](../../t-sql/functions/eventdata-transact-sql.md)
201+
202+

0 commit comments

Comments
 (0)