Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

cfset using tempory table

Posted on 2011-03-08
6
Medium Priority
?
454 Views
Last Modified: 2012-05-11
i have a report generating from dynamic sql (500 lines) in coldfusion that is taking long time to run, in this process of fine tuning ,i have created temporary table for a view  as this view is used in about 5 times in the query , after making this change and the query executed in less time.To implement this in coldfusion  I need to know how to declare  below query using temp table using  cfset in coldfusion

SELECT * INTO #temp
FROM VW_sample MAIN
INNER JOIN table_A

Note: In database, after we reran query with temporary table We will get the following  message

'There is already an object named '#temp' in the database'.
So we need to make sure in coldfusion not to repeat this error.

0
Comment
Question by:dsk1234
[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
  • 3
  • 2
6 Comments
 
LVL 52

Expert Comment

by:_agx_
ID: 35070713
This may be due to connection pooling.  Temp tables exist for the life of the db session / connection.  If you're using connection pooling, the session (and temp table) will persist beyond a single http request.  You need to drop the #temp table at the end of your query.

0
 

Author Comment

by:dsk1234
ID: 35070806
my question is how to  write above query using cfset  in coldfusion (particularly temp table (#temp))
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 1000 total points
ID: 35070881
Actually it looks like there were 2 questions:

1. I need to know how to declare  below query using temp table using  cfset in coldfusion

It's just like setting any other string. The one difference is you need to escape the # sign so CF doesn't think it's a variable. Use two ## signs instead

<cfset x = "SELECT * INTO ##temp FROM VW_sample MAIN INNER JOIN table_A ">

Btw: The disadvantage to using dynamic sql is you can't use cfqueryparam to help protect against sql injection. So be sure to scrub your input.

2. 'There is already an object named '#temp' in the database'.
So we need to make sure in coldfusion not to repeat this error.


Like I said, you have to drop the temp table at the end of the query. So it doesn't live on past the current request.
0
Amazon Web Services EC2 Cheat Sheet

AWS EC2 is a core part of AWS’s cloud platform, allowing users to spin up virtual machines for a variety of tasks; however, EC2’s offerings can be overwhelming. Learn the basics with our new AWS cheat sheet – this time on EC2!

 
LVL 39

Expert Comment

by:gdemaria
ID: 35072370
Hmmm, I'm a bit confused by the question.  agx, you may have it right, but I am thinking he is not trying to do dynamic sql, but perhaps store values into temp using codlfusion?

dsk1234, if agx hasn't resolved the issue, could you please expand more on what you're trying to accomplish.  My feeling is that CFSET is not what you want.

I think you want to write a cfquery and use multiple SQL statements within it using BEGIN or END.  Alternatively, create a SQL Procedure to do the entire thing...

... but i could be missing the obejective


<cfquery name="myQuery" datasource="#request.datasource#">
 BEGIN
  SELECT * INTO #temp 
  FROM VW_sample MAIN
    INNER JOIN table_A
    
  do your other queries  
 END;
</cfquery>

Open in new window

0
 
LVL 52

Expert Comment

by:_agx_
ID: 35072549
Hm.. sounded like they were already running a bunch of dynamic sql statements w/cfquery's. The only difference in adding temp tables would be the need to escape the pound # sign in the table name.  And of course drop the temp table at the end...

     ie Use  ##temp instead of #temp

But ... you could be right ;-) Though really .. this sounds like a job for a stored proc.
0
 

Author Closing Comment

by:dsk1234
ID: 35097184
Thanks
0

Featured Post

PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

Question has a verified solution.

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

PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

705 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