Solved

Global Temp Vs Temp - help...

Posted on 2008-06-13
6
221 Views
Last Modified: 2010-04-21
I have the below - and when I use a global temp table it works but when I create a normal temp table right above the dynamic sql - it does not insert into - is it out of scope or something:

My global temp table is: ##tblMXPROFIT_CASH_CON1TEST

When I try and simply create a #tblMXPROFIT_CASH_CON1TEST temp table, and replace in the below, it does not work?
EXEC

    ( 'SELECT a.DEPT_CODE, a.PROC_CODE, aa.INSURANCE, aa.FSC, aa.PATIENT_TP, aa.PTP, c.UNIT_PRICE, a.QTY, a.REV 

			INTO ##tblMXPROFIT_CASH_CON1TEST

			FROM ' + @DB + '_vwMX_TRANS a INNER JOIN ' + @DB

      + '_vwMX_HEAD aa 

				ON a.ACCT_NO = aa.ACCT_NO 

				LEFT JOIN #tmp c 

				ON a.DEPT_CODE = c.DEPT_CODE AND a.PROC_CODE = c.PROC_CODE 

			ORDER BY aa.INSURANCE, aa.FSC, aa.PATIENT_TP, aa.PTP ' )

Open in new window

0
Comment
Question by:tbaseflug
  • 3
  • 3
6 Comments
 

Author Comment

by:tbaseflug
Comment Utility
Could I do something like:


SELECT * INTO #tblMXPROFIT_CASH_CON1TEST

EXEC

    ( 'SELECT a.DEPT_CODE, a.PROC_CODE, aa.INSURANCE, aa.FSC, aa.PATIENT_TP, aa.PTP, c.UNIT_PRICE, a.QTY, a.REV 

			FROM ' + @DB + '_vwMX_TRANS a INNER JOIN ' + @DB

      + '_vwMX_HEAD aa 

				ON a.ACCT_NO = aa.ACCT_NO 

				LEFT JOIN #tmp c 

				ON a.DEPT_CODE = c.DEPT_CODE AND a.PROC_CODE = c.PROC_CODE 

			ORDER BY aa.INSURANCE, aa.FSC, aa.PATIENT_TP, aa.PTP ' )

Open in new window

0
 
LVL 60

Expert Comment

by:chapmandew
Comment Utility
Yep, it goes out to scope....
0
 
LVL 60

Expert Comment

by:chapmandew
Comment Utility
No, you can't. However, if you declare the definition of the temp table and then try to insert into it, it shoudl work.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:tbaseflug
Comment Utility
Something like the below?  
CREATE TABLE #tblMXPROFIT_CASH_CON1TEST  (

DEPT_CODE nvarchar(10),

PROC_CODE nvarchar(10),

INSURANCE NVARCHAR(10),

FSC NVARCHAR(10),

PATIENT_TP NVARCHAR(10),

PTP NVARCHAR(10),

UNIT_PRICE MONEY,

QTY INT,

REV INT

)
 

INSERT INTO #tblMXPROFIT_CASH_CON1TEST 

EXEC

    ( 'SELECT a.DEPT_CODE, a.PROC_CODE, aa.INSURANCE, aa.FSC, aa.PATIENT_TP, aa.PTP, c.UNIT_PRICE, a.QTY, a.REV 

			FROM ' + @DB + '_vwMX_TRANS a INNER JOIN ' + @DB

      + '_vwMX_HEAD aa 

				ON a.ACCT_NO = aa.ACCT_NO 

				LEFT JOIN #tmp c 

				ON a.DEPT_CODE = c.DEPT_CODE AND a.PROC_CODE = c.PROC_CODE 

			ORDER BY aa.INSURANCE, aa.FSC, aa.PATIENT_TP, aa.PTP ' )

Open in new window

0
 
LVL 60

Accepted Solution

by:
chapmandew earned 500 total points
Comment Utility
Kinda...something like this...you have to assign your script to a var first....make sure your @db variable has a value also.


CREATE TABLE #tblMXPROFIT_CASH_CON1TEST  (
DEPT_CODE nvarchar(10),
PROC_CODE nvarchar(10),
INSURANCE NVARCHAR(10),
FSC NVARCHAR(10),
PATIENT_TP NVARCHAR(10),
PTP NVARCHAR(10),
UNIT_PRICE MONEY,
QTY INT,
REV INT
)
DECLARE @SQL NVARCHAR(2000)
SET @SQL = 'SELECT a.DEPT_CODE, a.PROC_CODE, aa.INSURANCE, aa.FSC, aa.PATIENT_TP, aa.PTP, c.UNIT_PRICE, a.QTY, a.REV
                        FROM ' + @DB + '_vwMX_TRANS a INNER JOIN ' + @DB
      + '_vwMX_HEAD aa
                                ON a.ACCT_NO = aa.ACCT_NO
                                LEFT JOIN #tmp c
                                ON a.DEPT_CODE = c.DEPT_CODE AND a.PROC_CODE = c.PROC_CODE
                        ORDER BY aa.INSURANCE, aa.FSC, aa.PATIENT_TP, aa.PTP '
 
INSERT INTO #tblMXPROFIT_CASH_CON1TEST
EXEC sp_executesql @SQL
0
 

Author Closing Comment

by:tbaseflug
Comment Utility
Perfect - thanks!
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

771 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now