Solved

Ctreating temp table inside Exec command.

Posted on 2004-03-29
6
1,707 Views
Last Modified: 2007-12-19
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
0
Comment
Question by:raghava_dg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 10

Expert Comment

by:bret
ID: 10704699
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

0
 

Author Comment

by:raghava_dg
ID: 10705246
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).
0
 
LVL 15

Expert Comment

by:namasi_navaretnam
ID: 10710303
Please maintain open questions.

Regards-
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 10

Accepted Solution

by:
bret earned 50 total points
ID: 10733606
Well, that is one reason why I use the actual spid of the user process; it is easy to tell going in that if there is existing data with my own spid that it can be deleted.  (For that matter, any rows with a spid that is not currently in sysprocesses could also be deleted).

Perhaps your sproc could do this, which would both delete the records and allow the last statement to be the result set.  The temp table will automatically be cleaned up by ASE when the procedure exits.

select * into #temp from results where spid = @@spid
delete results where spid = @@spid
select * from #temp
0
 

Author Comment

by:raghava_dg
ID: 10805683
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
0
 
LVL 1

Expert Comment

by:iziki
ID: 10911503
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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Pivot and Unpivot in Sybase 2 1,726
install ASE 16 side by side with ASE 15.7 15 875
performance measure of  running SSIS package 4 227
Sybase and replication server 13 58
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
In order to fulfill our mission of inspiring learning in the technology community, Experts Exchange is launching a Course of the Month program. Premium and Team Account members will have access to one course per month as a part of their membership, …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question