Skip to content

Commit ecdb796

Browse files
Merge pull request #15438 from MicrosoftDocs/MashaMSFT-patch-2
Update execute-transact-sql.md
2 parents 01f5fbb + b7d63fa commit ecdb796

1 file changed

Lines changed: 161 additions & 30 deletions

File tree

docs/t-sql/language-elements/execute-transact-sql.md

Lines changed: 161 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,77 @@ Executes a command string or character string within a [!INCLUDE[tsql](../../inc
4444
![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)
4545

4646
## Syntax
47+
48+
::: moniker range=">=sql-server-ver15||=sqlallproducts-allversions"
49+
The following code block shows the syntax in SQL Server 2019. Alternatively, see [syntax in SQL Server 2017 and earlier](execute-transact-sql.md?view=sql-server-2017) instead.
50+
51+
```syntaxsql
52+
-- Syntax for SQL Server 2019
4753
54+
Execute a stored procedure or function
55+
[ { EXEC | EXECUTE } ]
56+
{
57+
[ @return_status = ]
58+
{ module_name [ ;number ] | @module_name_var }
59+
[ [ @parameter = ] { value
60+
| @variable [ OUTPUT ]
61+
| [ DEFAULT ]
62+
}
63+
]
64+
[ ,...n ]
65+
[ WITH <execute_option> [ ,...n ] ]
66+
}
67+
[;]
68+
69+
Execute a character string
70+
{ EXEC | EXECUTE }
71+
( { @string_variable | [ N ]'tsql_string' } [ + ...n ] )
72+
[ AS { LOGIN | USER } = ' name ' ]
73+
[;]
74+
75+
Execute a pass-through command against a linked server
76+
{ EXEC | EXECUTE }
77+
( { @string_variable | [ N ] 'command_string [ ? ]' } [ + ...n ]
78+
[ { , { value | @variable [ OUTPUT ] } } [ ...n ] ]
79+
)
80+
[ AS { LOGIN | USER } = ' name ' ]
81+
[ AT linked_server_name ]
82+
[ AT DATA_SOURCE data_source_name ]
83+
[;]
84+
85+
<execute_option>::=
86+
{
87+
RECOMPILE
88+
| { RESULT SETS UNDEFINED }
89+
| { RESULT SETS NONE }
90+
| { RESULT SETS ( <result_sets_definition> [,...n ] ) }
91+
}
92+
93+
<result_sets_definition> ::=
94+
{
95+
(
96+
{ column_name
97+
data_type
98+
[ COLLATE collation_name ]
99+
[ NULL | NOT NULL ] }
100+
[,...n ]
101+
)
102+
| AS OBJECT
103+
[ db_name . [ schema_name ] . | schema_name . ]
104+
{table_name | view_name | table_valued_function_name }
105+
| AS TYPE [ schema_name.]table_type_name
106+
| AS FOR XML
107+
}
108+
```
109+
::: moniker-end
110+
111+
::: monikerRange=">=sql-server-2016 ||=sqlallproducts-allversions"
112+
113+
The following code block shows the syntax in SQL Server 2017 and earlier. Alternatively, see [syntax in SQL Server 2019](execute-transact-sql.md?view=sql-server-ver15) instead.
114+
115+
48116
```syntaxsql
49-
-- Syntax for SQL Server
117+
-- Syntax for SQL Server 2017 and earleir
50118
51119
Execute a stored procedure or function
52120
[ { EXEC | EXECUTE } ]
@@ -102,6 +170,8 @@ Execute a pass-through command against a linked server
102170
| AS FOR XML
103171
}
104172
```
173+
::: moniker-end
174+
105175

106176
```syntaxsql
107177
-- In-Memory OLTP
@@ -174,7 +244,8 @@ Execute a character string
174244
| AS TYPE [ schema_name.]table_type_name
175245
| AS FOR XML
176246
177-
```
247+
```
248+
178249

179250
```syntaxsql
180251
-- Syntax for Azure SQL Data Warehouse and Parallel Data Warehouse
@@ -190,6 +261,8 @@ Execute a character string
190261
( { @string_variable | [ N ] 'tsql_string' } [ +...n ] )
191262
[;]
192263
```
264+
265+
193266

194267
## Arguments
195268
@*return_status*
@@ -292,6 +365,14 @@ If you pass a single word that does not begin with `@` and that's not enclosed i
292365

293366
WITH \<execute_option>
294367
Possible execute options. The RESULT SETS options cannot be specified in an INSERT...EXEC statement.
368+
369+
AT DATA_SOURCE data_source_name
370+
**Applies to**: [!INCLUDE[sssqlv15](../../includes/sssqlv15-md.md)] and later
371+
372+
Specifies that *command_string* is executed against *data_source_name* and results, if any, are returned to the client. *data_source_name* must refer to an existing EXTERNAL DATA SOURCE definition in the database. Only data sources that point to SQL Server are supported. Additionally, for SQL Server big data cluster data sources that point to compute pool, data pool or storage pool are supported. Data sources are defined by using [CREATE EXTERNAL DATA SOURCE](../statements/create-external-data-source-transact-sql.md).
373+
374+
WITH \<execute_option>
375+
Possible execute options. The RESULT SETS options cannot be specified in an INSERT...EXEC statement.
295376

296377
|Term|Definition|
297378
|----------|----------------|
@@ -385,26 +466,26 @@ USE master; EXEC ('USE AdventureWorks2012; SELECT BusinessEntityID, JobTitle FRO
385466
### Context Switching Permissions
386467
To specify EXECUTE AS on a login, the caller must have IMPERSONATE permissions on the specified login name. To specify EXECUTE AS on a database user, the caller must have IMPERSONATE permissions on the specified user name. When no execution context is specified, or EXECUTE AS CALLER is specified, IMPERSONATE permissions are not required.
387468

388-
## Examples
469+
## Examples: SQL Server
389470

390471
### A. Using EXECUTE to pass a single parameter
391472
The `uspGetEmployeeManagers` stored procedure in the [!INCLUDE[ssSampleDBnormal](../../includes/sssampledbnormal-md.md)] database expects one parameter (`@EmployeeID`). The following examples execute the `uspGetEmployeeManagers` stored procedure with `Employee ID 6` as its parameter value.
392473

393-
```
474+
```sql
394475
EXEC dbo.uspGetEmployeeManagers 6;
395476
GO
396477
```
397478

398479
The variable can be explicitly named in the execution:
399480

400-
```
481+
```sql
401482
EXEC dbo.uspGetEmployeeManagers @EmployeeID = 6;
402483
GO
403484
```
404485

405486
If the following is the first statement in a batch or an **osql** or **sqlcmd** script, EXEC is not required.
406487

407-
```
488+
```sql
408489
dbo.uspGetEmployeeManagers 6;
409490
GO
410491
--Or
@@ -415,7 +496,7 @@ GO
415496
### B. Using multiple parameters
416497
The following example executes the `spGetWhereUsedProductID` stored procedure in the [!INCLUDE[ssSampleDBnormal](../../includes/sssampledbnormal-md.md)] database. It passes two parameters: the first parameter is a product ID (`819`) and the second parameter, `@CheckDate,` is a `datetime` value.
417498

418-
```
499+
```sql
419500
DECLARE @CheckDate datetime;
420501
SET @CheckDate = GETDATE();
421502
EXEC dbo.uspGetWhereUsedProductID 819, @CheckDate;
@@ -425,7 +506,7 @@ GO
425506
### C. Using EXECUTE 'tsql_string' with a variable
426507
The following example shows how `EXECUTE` handles dynamically built strings that contain variables. This example creates the `tables_cursor` cursor to hold a list of all user-defined tables in the [!INCLUDE[ssSampleDBobject](../../includes/sssampledbobject-md.md)] database, and then uses that list to rebuild all indexes on the tables.
427508

428-
```
509+
```sql
429510
DECLARE tables_cursor CURSOR
430511
FOR
431512
SELECT s.name, t.name
@@ -453,7 +534,7 @@ GO
453534

454535
**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] and later
455536

456-
```
537+
```sql
457538
DECLARE @retstat int;
458539
EXECUTE @retstat = SQLSERVER1.AdventureWorks2012.dbo.uspGetEmployeeManagers @BusinessEntityID = 6;
459540
```
@@ -471,7 +552,7 @@ EXEC @proc_name;
471552
### F. Using EXECUTE with DEFAULT
472553
The following example creates a stored procedure with default values for the first and third parameters. When the procedure is run, these defaults are inserted for the first and third parameters when no value is passed in the call or when the default is specified. Note the various ways the `DEFAULT` keyword can be used.
473554

474-
```
555+
```sql
475556
IF OBJECT_ID(N'dbo.ProcTestDefaults', N'P')IS NOT NULL
476557
DROP PROCEDURE dbo.ProcTestDefaults;
477558
GO
@@ -490,7 +571,7 @@ GO
490571

491572
The `Proc_Test_Defaults` stored procedure can be executed in many combinations.
492573

493-
```
574+
```sql
494575
-- Specifying a value only for one parameter (@p2).
495576
EXECUTE dbo.ProcTestDefaults @p2 = 'A';
496577
-- Specifying a value for the first two parameters.
@@ -512,7 +593,7 @@ EXECUTE dbo.ProcTestDefaults DEFAULT, 'I', @p3 = DEFAULT;
512593

513594
**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] and later
514595

515-
```
596+
```sql
516597
EXEC sp_addlinkedserver 'SeattleSales', 'SQL Server'
517598
GO
518599
EXECUTE ( 'CREATE TABLE AdventureWorks2012.dbo.SalesTbl
@@ -523,15 +604,15 @@ GO
523604
### H. Using EXECUTE WITH RECOMPILE
524605
The following example executes the `Proc_Test_Defaults` stored procedure and forces a new query plan to be compiled, used, and discarded after the module is executed.
525606

526-
```
607+
```sql
527608
EXECUTE dbo.Proc_Test_Defaults @p2 = 'A' WITH RECOMPILE;
528609
GO
529610
```
530611

531612
### I. Using EXECUTE with a user-defined function
532613
The following example executes the `ufnGetSalesOrderStatusText` scalar user-defined function in the [!INCLUDE[ssSampleDBnormal](../../includes/sssampledbnormal-md.md)] database. It uses the variable `@returnstatus` to store the value returned by the function. The function expects one input parameter, `@Status`. This is defined as a **tinyint** data type.
533614

534-
```
615+
```sql
535616
DECLARE @returnstatus nvarchar(15);
536617
SET @returnstatus = NULL;
537618
EXEC @returnstatus = dbo.ufnGetSalesOrderStatusText @Status = 2;
@@ -544,7 +625,7 @@ GO
544625

545626
**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] and later
546627

547-
```
628+
```sql
548629
-- Setup the linked server.
549630
EXEC sp_addlinkedserver
550631
@server='ORACLE',
@@ -576,7 +657,7 @@ GO
576657
### K. Using EXECUTE AS USER to switch context to another user
577658
The following example executes a [!INCLUDE[tsql](../../includes/tsql-md.md)] string that creates a table and specifies the `AS USER` clause to switch the execution context of the statement from the caller to `User1`. The [!INCLUDE[ssDE](../../includes/ssde-md.md)] will check the permissions of `User1` when the statement is run. `User1` must exist as a user in the database and must have permission to create tables in the `Sales` schema, or the statement fails.
578659

579-
```
660+
```sql
580661
EXECUTE ('CREATE TABLE Sales.SalesTable (SalesID int, SalesName varchar(10));')
581662
AS USER = 'User1';
582663
GO
@@ -587,7 +668,7 @@ GO
587668

588669
**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] and later
589670

590-
```
671+
```sql
591672
-- Setup the linked server.
592673
EXEC sp_addlinkedserver 'SeattleSales', 'SQL Server'
593674
GO
@@ -603,7 +684,7 @@ GO
603684

604685
**Applies to**: [!INCLUDE[ssSQL11](../../includes/sssql11-md.md)] and later, [!INCLUDE[ssSDSfull](../../includes/sssdsfull-md.md)]
605686

606-
```
687+
```sql
607688
EXEC uspGetEmployeeManagers 16
608689
WITH RESULT SETS
609690
(
@@ -623,7 +704,7 @@ WITH RESULT SETS
623704

624705
**Applies to**: [!INCLUDE[ssSQL11](../../includes/sssql11-md.md)] and later, [!INCLUDE[ssSDSfull](../../includes/sssdsfull-md.md)]
625706

626-
```
707+
```sql
627708
--Create the procedure
628709
CREATE PROC Production.ProductList @ProdName nvarchar(50)
629710
AS
@@ -653,53 +734,103 @@ WITH RESULT SETS
653734
);
654735

655736
```
737+
### O. Using EXECUTE with AT DATA_SOURCE data_source_name to query a remote SQL Server
656738

657-
## Examples: [!INCLUDE[ssSDWfull](../../includes/sssdwfull-md.md)] and [!INCLUDE[ssPDW](../../includes/sspdw-md.md)]
739+
The following example passes a command string to an external data source pointing to a SQL Server instance.
658740

659-
### Example O: Basic Procedure Execution
660-
Executing a stored procedure:
741+
**Applies to**: [!INCLUDE[sssqlv15](../../includes/sssqlv15-md.md)] and later
661742

743+
```sql
744+
EXECUTE ( 'SELECT @@SERVERNAME' ) AT DATA_SOURCE my_sql_server;
745+
GO
662746
```
747+
748+
### P. Using EXECUTE with AT DATA_SOURCE data_source_name to query compute pool in SQL Server Big Data Cluster
749+
750+
The following example passes a command string to an external data source pointing to a compute pool in SQL Server Big Data Cluster. The example creates a data source `SqlComputePool` against a compute pool in SQL Server Big Data Cluster and executes a `SELECT` statement against the data source.
751+
752+
**Applies to**: [!INCLUDE[sssqlv15](../../includes/sssqlv15-md.md)] and later
753+
754+
```sql
755+
CREATE EXTERNAL DATA SOURCE SqlComputePool
756+
WITH (LOCATION = 'sqlcomputepool://controller-svc/default');
757+
EXECUTE ( 'SELECT @@SERVERNAME' ) AT DATA_SOURCE SqlComputePool;
758+
GO
759+
```
760+
761+
### Q. Using EXECUTE with AT DATA_SOURCE data_source_name to query data pool in SQL Server Big Data Cluster
762+
The following example passes a command string to an external data source pointing to compute pool in SQL Server big data cluster. The example creates a data source `SqlDataPool` against a data pool in SQL Server big data cluster and executes a `SELECT` statement against the data source.
763+
764+
**Applies to**: [!INCLUDE[sssqlv15](../../includes/sssqlv15-md.md)] and later
765+
766+
```sql
767+
CREATE EXTERNAL DATA SOURCE SqlDataPool
768+
WITH (LOCATION = 'sqldatapool://controller-svc/default');
769+
EXECUTE ( 'SELECT @@SERVERNAME' ) AT DATA_SOURCE SqlDataPool;
770+
GO
771+
```
772+
773+
### R. Using EXECUTE with AT DATA_SOURCE data_source_name to query storage pool in SQL Server Big Data Cluster
774+
775+
The following example passes a command string to an external data source pointing to compute pool in SQL Server Big Data Cluster. The example creates a data source `SqlStoragePool` against a data pool in SQL Server Big Data Cluster and executes a `SELECT` statement against the data source.
776+
777+
**Applies to**: [!INCLUDE[sssqlv15](../../includes/sssqlv15-md.md)] and later
778+
779+
```sql
780+
CREATE EXTERNAL DATA SOURCE SqlStoragePool
781+
WITH (LOCATION = 'sqlhdfs://controller-svc/default');
782+
EXECUTE ( 'SELECT @@SERVERNAME' ) AT DATA_SOURCE SqlStoragePool;
783+
GO
784+
```
785+
786+
787+
## Examples: Azure Synapse Analytics
788+
789+
### A: Basic Procedure Execution
790+
Executing a stored procedure:
791+
792+
```sql
663793
EXEC proc1;
664794
```
665795

666796
Calling a stored procedure with name determined at runtime:
667797

668-
```
798+
```sql
669799
EXEC ('EXEC ' + @var);
670800
```
671801

672802
Calling a stored procedure from within a stored procedure:
673803

674-
```
804+
```sql
675805
CREATE sp_first AS EXEC sp_second; EXEC sp_third;
676806
```
677807

678-
### Example P: Executing Strings
808+
### B: Executing Strings
679809
Executing a SQL string:
680810

681-
```
811+
```sql
682812
EXEC ('SELECT * FROM sys.types');
683813
```
684814

685815
Executing a nested string:
686816

687-
```
817+
```sql
688818
EXEC ('EXEC (''SELECT * FROM sys.types'')');
689819
```
690820

691821
Executing a string variable:
692822

693-
```
823+
```sql
694824
DECLARE @stringVar nvarchar(100);
695825
SET @stringVar = N'SELECT name FROM' + ' sys.sql_logins';
696826
EXEC (@stringVar);
697827
```
698828

699-
### Example Q: Procedures with Parameters
829+
### C: Procedures with Parameters
830+
700831
The following example creates a procedure with parameters and demonstrates 3 ways to execute the procedure:
701832

702-
```
833+
```sql
703834
-- Uses AdventureWorks
704835

705836
CREATE PROC ProcWithParameters

0 commit comments

Comments
 (0)