Avatar of raghava_dg
raghava_dg

asked on 

Ctreating temp table inside Exec command.

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
Sybase Database

Avatar of undefined
Last Comment
iziki
Avatar of bret
bret
Flag of United States of America image

That is right, an EXEC is essentially a new connection to ASE and the temp table only exists within that scope (nor can the EXEC reference temp tables created outside of it's own scope).

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

Avatar of raghava_dg
raghava_dg

ASKER

thanks bret ,

but my worry in the above suggestion is .... I need to have a separate routine to purge the tmp table.
Because i will be passing "spid " after generating it by randaom genarator from my client. So each time it will be diffrent and the delete statement in my proc will not be of much help , I mean since each time new "spid " will be passed so i may not be able to delete it.

For my another technical reason due to the Java client I can not delete those records after i finesh my proceesing inside my proc coz last statement in my proc should return a resultset (i,e a select statement).
Avatar of namasi_navaretnam
Please maintain open questions.

Regards-
ASKER CERTIFIED SOLUTION
Avatar of bret
bret
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of raghava_dg
raghava_dg

ASKER

Thanks bret .
One more solution is to create the temp table out side ASE and use that #temp table inside ASE. Once sp is finished temp table will be deleted automatically.

Thanks for the help
Avatar of iziki
iziki

Hi,
Another way to do it is by chainning the commands, I used it  before several times and it works great :-)

declare @sql_text varchar(255)

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

select @sql_text = "select * from #output1"    

exec(@sql + @sql_text)

good luck.
iziki.
Sybase Database
Sybase Database

Sybase, a subsidiary of SAP, builds a client/server relational database management system. Products include Adaptive Server Enterprise (ASE), Adaptive Server Anywhere (ASA), Sybase Unwired Platform (SUP) for mobile applications, Afaria for enterprise mobile device management and IQ for data warehouse and big data applications.

5K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo