Skip to content Skip to sidebar Skip to footer

Tsql Writing Into A Temporary Table From Dynamic Sql

Consider the following code: SET @SQL1 = 'SELECT * INTO #temp WHERE ...' exec(@SQL1) SELECT * from #temp (this line throws an error that #temp doesn't exist) Apparently this is b

Solution 1:

Did you try to create your template table explicitly?

CreateTable#temp (..)

Solution 2:

You can create temp before exec and use exec to populate the temp table.

Solution 3:

Didn't find a workable solution that did everything I needed so I switched to using ##global temp tables instead.

Solution 4:

An example, look at "into"

SELECT o.OrderID, o.OrderDate, od.UnitPrice, od.Quantity,
       c.CustomerID, c.CompanyName, c.Address, c.City, c.Region,
       c.PostalCode, c.Country, c.Phone, p.ProductID,
       p.ProductName, p.UnitsInStock, p.UnitsOnOrder
INTO   #temp
FROM   Orders o
JOIN   [Order Details] od ON o.OrderID = od.OrderID
JOIN   Customers c ON o.CustomerID = c.CustomerID
JOIN   Products p ON p.ProductID = od.ProductID

Solution 5:

Can you not put your select after the insert into with a ; delimeter and run the two statements together?

Post a Comment for "Tsql Writing Into A Temporary Table From Dynamic Sql"