Hai frnds,
I have used the temp table inside my Exec statement. And If I tried to re use that temp table after Exec,it gave error as #output1 (this is name of temp table) not found .
What is the solution for this.
I need to use Exec coz I need to create a dynamic query depending on the input.
Please help me on this.
My proc is as below
create procedure ParentTest1
@whereClause varchar(100)
AS
DECLARE
@sql varchar(1000)
select @sql = "SELECT od.orderID orderDetailID,o.parentOrderID tmpParentOrderID ,o.orderID,
od.acctMnc,od.securityMnc
INTO #output1
FROM tmpOrders o, tmpOrderDetail od
WHERE o.orderID *= od.orderID"+@whereClause
exec(@sql)
select * from #output1
I think life of #output1 will end as soon as we complete Exec . Is this rt? Do u guys have any other solutions?
Thanks a lot for ur suggestion.
Thanks
Raghava
The general solution is to create a permanent table to pass info through. You can provide a key value in the string you pass to exec (such as the caller's @@spid value) that is inserted as a column value in the permanent table so the caller can identify their own rows.
General flow:
create table results (spid int, result int)
go
-- get rid of any old data in the table
delete results where spid = @@spid
-- call the exec that stores values
-- in the results table
exec (
"insert results values ("
+ str(@@spid,10,0)
+ ", 42)"
)
select * from results where spid = @@spid