Populates a local table with results returned from a procedure and makes it easier to import data from remote systems to the local server.
For additional syntax information for the INSERT statement, see the Microsoft SQL Server Transact-SQL Reference.
INSERT [INTO]
{table_name | view_name} [(column_list)]
EXECute { procedure_name | @procedure_name_var}
[[@parameter_name=] {value | @variable [OUTPUT] | DEFAULT}
[, [@parameter_name =] {value | @variable [OUTPUT] | DEFAULT}]...]
where
The column_list is necessary only when some, but not all, columns in the table are to receive data. You can leave out items in the column_list as long as the omitted columns permit null values or the columns are associated with a default.
If column_list is not specified, all columns in the receiving table (in CREATE TABLE order) are assumed to be included.
This statement allows you to run procedures on local or remote systems and store the returned information in a local table. The procedures can be system procedures, user-defined procedures, or extended procedures.
Specify the system and procedure you want to run by specifying the procedure name. To specify a remote system, the procedure name will begin with the server name followed by the database name, the owner of the procedure, and the procedure name. Each element of the procedure name is followed by a period except for the procedure name itself. For example, the remote procedure get_latest_sales owned by jake on the accounts server would look like this:
accounts.acct_db.jake.get_latest_sales
The local table is loaded with data returned from SELECT statements. The local table must already exist. Data returned from the PRINT statement, RAISERROR statement, FETCH statement, or other output is not stored in the table.
A procedure that returns multiple results sets appends the data to the table.
Data returned from the SELECT statement must conform to the datatypes in the existing local table's columns. For example, inserting varchar data into a tinyint data column is not permitted.
All INSERT statement rules apply to this operation. That is, table constraints, rules and triggers, and implicit datatype conversion rules are all enforced during table population. For more information about INSERT statement rules, see INSERT Statement.
For more information about writing procedures, see CREATE PROCEDURE Statement.
Errors are reported if there is a datatype mismatch between local table input columns and values returned from the procedure. Also, if the number of columns does not match the number of items returned in a row, an error is returned.
For more information about executing a procedure, see EXECUTE Statement in this document and in the Microsoft SQL Server Transact-SQL Reference.
This example retrieves information from a remote database for administration purposes. The human_resource.hr.dbo.fetch_employee_data and marketing.mis.dbo.fetch_sales_figures procedures pull data from remote sites into local tables.
INSERT INTO employee_info EXECUTE human_resource.hr.dbo.fetch_employee_data INSERT INTO sales_info EXECUTE marketing.mis.dbo.fetch_sales_figures
This example shows how to use the new INSERT EXECUTE feature to enable Transact-SQL code to assign the current value of a SET option to a variable.
SET NOCOUNT on CREATE TABLE #tb_setopts (SetOptName varchar(35) NOT NULL ,SetOptValue varchar(35) null) INSERT INTO #tb_setopts (SetOptName,SetOptValue) EXEC('dbcc useroptions') DECLARE @CurrentValue varchar(35) SELECT @CurrentValue=case min(SetOptValue) WHEN 'SET' THEN 'on' else 'off' END FROM #tb_setopts WHERE SetOptName='ansi_padding' IF @CurrentValue='on' RAISERROR('The SET opt ''ansi_padding'' is''on''.' ,1,1) ELSE RAISERROR('The SET opt ''ansi_padding'' is''off''.',1,1) IF object_id('tempdb..#tb_setopts') IS NOT NULL DROP TABLE #tb_setopts /******** Actual output.... DBCC execution completed. If DBCC printed error messages, see your System Administrator. The SET opt 'ansi_padding' is 'off'. ********/