You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -44,9 +44,77 @@ Executes a command string or character string within a [!INCLUDE[tsql](../../inc
44
44
[Transact-SQL Syntax Conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
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.
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
+
48
116
```syntaxsql
49
-
-- Syntax for SQL Server
117
+
-- Syntax for SQL Server 2017 and earleir
50
118
51
119
Execute a stored procedure or function
52
120
[ { EXEC | EXECUTE } ]
@@ -102,6 +170,8 @@ Execute a pass-through command against a linked server
102
170
| AS FOR XML
103
171
}
104
172
```
173
+
::: moniker-end
174
+
105
175
106
176
```syntaxsql
107
177
-- In-Memory OLTP
@@ -174,7 +244,8 @@ Execute a character string
174
244
| AS TYPE [ schema_name.]table_type_name
175
245
| AS FOR XML
176
246
177
-
```
247
+
```
248
+
178
249
179
250
```syntaxsql
180
251
-- Syntax for Azure SQL Data Warehouse and Parallel Data Warehouse
@@ -292,6 +365,14 @@ If you pass a single word that does not begin with `@` and that's not enclosed i
292
365
293
366
WITH \<execute_option>
294
367
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.
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.
387
468
388
-
## Examples
469
+
## Examples: SQL Server
389
470
390
471
### A. Using EXECUTE to pass a single parameter
391
472
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.
392
473
393
-
```
474
+
```sql
394
475
EXEC dbo.uspGetEmployeeManagers6;
395
476
GO
396
477
```
397
478
398
479
The variable can be explicitly named in the execution:
399
480
400
-
```
481
+
```sql
401
482
EXEC dbo.uspGetEmployeeManagers @EmployeeID =6;
402
483
GO
403
484
```
404
485
405
486
If the following is the first statement in a batch or an **osql** or **sqlcmd** script, EXEC is not required.
406
487
407
-
```
488
+
```sql
408
489
dbo.uspGetEmployeeManagers6;
409
490
GO
410
491
--Or
@@ -415,7 +496,7 @@ GO
415
496
### B. Using multiple parameters
416
497
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.
417
498
418
-
```
499
+
```sql
419
500
DECLARE @CheckDate datetime;
420
501
SET @CheckDate = GETDATE();
421
502
EXEC dbo.uspGetWhereUsedProductID819, @CheckDate;
@@ -425,7 +506,7 @@ GO
425
506
### C. Using EXECUTE 'tsql_string' with a variable
426
507
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.
427
508
428
-
```
509
+
```sql
429
510
DECLARE tables_cursor CURSOR
430
511
FOR
431
512
SELECTs.name, t.name
@@ -453,7 +534,7 @@ GO
453
534
454
535
**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] and later
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.
473
554
474
-
```
555
+
```sql
475
556
IF OBJECT_ID(N'dbo.ProcTestDefaults', N'P')IS NOT NULL
476
557
DROP PROCEDURE dbo.ProcTestDefaults;
477
558
GO
@@ -490,7 +571,7 @@ GO
490
571
491
572
The `Proc_Test_Defaults` stored procedure can be executed in many combinations.
492
573
493
-
```
574
+
```sql
494
575
-- Specifying a value only for one parameter (@p2).
495
576
EXECUTE dbo.ProcTestDefaults @p2 ='A';
496
577
-- Specifying a value for the first two parameters.
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.
525
606
526
-
```
607
+
```sql
527
608
EXECUTE dbo.Proc_Test_Defaults @p2 ='A' WITH RECOMPILE;
528
609
GO
529
610
```
530
611
531
612
### I. Using EXECUTE with a user-defined function
532
613
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.
**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] and later
546
627
547
-
```
628
+
```sql
548
629
-- Setup the linked server.
549
630
EXEC sp_addlinkedserver
550
631
@server='ORACLE',
@@ -576,7 +657,7 @@ GO
576
657
### K. Using EXECUTE AS USER to switch context to another user
577
658
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.
### O. Using EXECUTE with AT DATA_SOURCE data_source_name to query a remote SQL Server
656
738
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.
658
740
659
-
### Example O: Basic Procedure Execution
660
-
Executing a stored procedure:
741
+
**Applies to**: [!INCLUDE[sssqlv15](../../includes/sssqlv15-md.md)] and later
661
742
743
+
```sql
744
+
EXECUTE ( 'SELECT @@SERVERNAME' ) AT DATA_SOURCE my_sql_server;
745
+
GO
662
746
```
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
663
793
EXEC proc1;
664
794
```
665
795
666
796
Calling a stored procedure with name determined at runtime:
667
797
668
-
```
798
+
```sql
669
799
EXEC ('EXEC '+ @var);
670
800
```
671
801
672
802
Calling a stored procedure from within a stored procedure:
673
803
674
-
```
804
+
```sql
675
805
CREATE sp_first AS EXEC sp_second; EXEC sp_third;
676
806
```
677
807
678
-
### Example P: Executing Strings
808
+
### B: Executing Strings
679
809
Executing a SQL string:
680
810
681
-
```
811
+
```sql
682
812
EXEC ('SELECT * FROM sys.types');
683
813
```
684
814
685
815
Executing a nested string:
686
816
687
-
```
817
+
```sql
688
818
EXEC ('EXEC (''SELECT * FROM sys.types'')');
689
819
```
690
820
691
821
Executing a string variable:
692
822
693
-
```
823
+
```sql
694
824
DECLARE @stringVar nvarchar(100);
695
825
SET @stringVar = N'SELECT name FROM'+' sys.sql_logins';
696
826
EXEC (@stringVar);
697
827
```
698
828
699
-
### Example Q: Procedures with Parameters
829
+
### C: Procedures with Parameters
830
+
700
831
The following example creates a procedure with parameters and demonstrates 3 ways to execute the procedure:
0 commit comments