UrbanPro
true

Learn MS SQL Development from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

How To Insert The Result Sets Of A Stored Procedure/Functions Into A Temporary Table In SQL Server?

Amitava Majumder
30/05/2017 0 0

How to Insert the Result sets of a Stored Procedure/Functions into a Temporary Table in SQL Server

In some cases with SQL Server, there may be an instance where you wish to take the resulting data from a stored procedure and insert it into a temporary table for use in another query. Determining how to accomplish this task can be somewhat difficult, so we’ll briefly outline a couple options, depending on your specific needs and database configuration.

Using the Openrowset Statement:

One possibility is to use the openrowset statement, which allows you to access remote data from an OLE DB source and can be executed directly from within another SQL statement. Openrowset is a one - time connection and data retrieval method, so it should not be utilized for frequent connections (linking servers is preferable in that case).

Openrowset can be the target of any Insert, Delete or Update statement, which makes it ideal for our purposes of “executing” our stored procedure for us and extracting that data back out to our waiting temporary table. Also supports bulk operations through a built-in bulk provider that enables data from a file to be read and returned as a rowset.

Before using openrowset, it may be necessary to modify some configuration options, specifically by allowing ad hoc access. This can be configured using the following statements:

Now we can utilize Openrowset, which has a particular syntax that must be adhered to:

OPENROWSET (

  ,

  ,

 

)

Step 1: Enable Ad Hoc Distributed Queries:

sp_configure 'Show Advanced Options', 1

GO

RECONFIGURE

GO

sp_configure 'Ad Hoc Distributed Queries', 1

GO

RECONFIGURE

GO

Step 2: Insert Results of Stored Procedure/Functions into a Temporary Table:

-- Create a Stored Procedure using AdventureWorksLT2012 database

CREATE PROCEDURE GetShippedOrder

AS

SELECT a.[SalesOrderID], a.CustomerID, c.[Name],b.[OrderQty],a.[ShipDate]

    FROM [SalesLT].[SalesOrderHeader] a INNER JOIN [SalesLT].[SalesOrderDetail] b

        ON a.[SalesOrderID] = b.[SalesOrderID]

        INNER JOIN [SalesLT].[Product] c ON b.ProductID = c.ProductID

GO

-- Execute Stored Procedure

EXEC GetShippedOrder

GO

-- Create a Inline Function

CREATE FUNCTION GetShippedOrders()

RETURNS TABLE

AS

RETURN SELECT a.[SalesOrderID], a.CustomerID, c.[Name],b.[OrderQty],a.[ShipDate]

    FROM [SalesLT].[SalesOrderHeader] a INNER JOIN [SalesLT].[SalesOrderDetail] b

        ON a.[SalesOrderID] = b.[SalesOrderID]

        INNER JOIN [SalesLT].[Product] c ON b.ProductID = c.ProductID

    WHERE a.[ShipDate] IS NULL

GO

-- Run the Function

select * from  GetShippedOrders()

Step 3: Insert into Temp Table:

-- Insert into Temp Table from Stored Procedure

SELECT *

INTO #TempTable

FROM OPENROWSET('SQLNCLI', 'Server=Amitava-PC;Trusted_Connection=yes;','EXEC [AdventureWorksLT2012].[dbo].GetShippedOrder')

GO

 -- Select Data from Temp Table

SELECT *

FROM #TempTable

GO

-- Insert into Temp Table from Inline Function

SELECT *

INTO #TempTable

FROM OPENROWSET('SQLNCLI','Server=AMITAVA-PC;Trusted_Connection=yes;','select * from  [AdventureWorksLT2012].[dbo]. GetShippedOrders()')

GO

-- Select Data from Temp Table

SELECT *

FROM #TempTable

GO.

 

 

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

Write A Query To Get Nth Highest Salary
WITH CTE AS ( SELECT EmpID, EmpName, EmpSalary, RN = ROW_NUMBER() OVER (ORDER BY EmpSalary DESC) FROM dbo.Salary ) SELECT EmpID, EmpName, EmpSalary FROM CTE WHERE RN = @NthRowUse...

5 Tips For Improving Your Documentation Immediately.
Tip 1) Quit it with the Passive Voice The passive voice is a plague on effective documentation. It reduces its clarity, its consistency, and the efficiency and tightness of the writing. The passive voice...

What is Hyperion?
- Its an Business Intelligence tools. Like Brio which was an independent product bought over my Hyperion has converted this product name to Hyperion Intelligence. Is it an OLAP tool? - Yes. You can analyse...

WebSphere
WebSphere is a set of Java-based tools from IBM that allows customers to create and manage sophisticated business Web sites. The central WebSphere tool is theWebSphere Application Server (WAS), an application...

SQLSERVER Tips
Tips: 1. Treat SQLSERVER as you would any other programming language 2. Go with baby steps 3. Try to practice as much as you can with tables by writing queries 4. Do not get discouraged

Looking for MS SQL Development Training?

Learn from Best Tutors on UrbanPro.

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for MS SQL Development Classes?

The best tutors for MS SQL Development Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn MS SQL Development with the Best Tutors

The best Tutors for MS SQL Development Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more