T-SQL – How to select data form stored procedure into a table

If you have a stored procedure that returns a dataset, you can insert that data directly into a table.

First, you need to create target table and define columns. Columns need to match the dataset your stored procedure is returning. So for example if your stored procedure returns a dataset that contains integer field followed by two nvarchar(50) fields, target table would look like:

CREATE TABLE #MyTable
(
   Col1 int,
   Col2 nvarchar(50),
   Col3 nvarchar(50)
)

When you’ve defined the table, insert the data by calling your stored procedure:

INSERT INTO #MyTable
EXEC spxMySP @Params
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s