Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Local Temporary table

Author  Topic 

AAAV
Posting Yak Master

152 Posts

Posted - 2010-03-09 : 09:11:59
I want to know the scope of a local temporary table (prefixed with #) created in a SP.

If there are three stored procedures A,B,C and
A calls B and B calls C
If i create a table #Results in A can i insert rows into #Results in C?

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-03-09 : 09:17:44
No. The temporary table created inside a procedure wont available in other procedure

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

AAAV
Posting Yak Master

152 Posts

Posted - 2010-03-09 : 09:25:29
http://www.sqlteam.com/article/temporary-tables i read here it is valid... but want to double check

so how do I do this if it is not valid? do i have to creat a table variable and pass it on to the SPs?
Go to Top of Page

haroon2k9
Constraint Violating Yak Guru

328 Posts

Posted - 2010-03-09 : 09:29:42
quote:
Originally posted by AAAV

so how do I do this? do i have to creat a table variable and pass it on to the SPs?



Nope.
quote:

If there are three stored procedures A,B,C and
A calls B and B calls C



See,Could you please post your exact requirement over here..That would help you much better way to acheive your Req.post some sample data with expected output..
Go to Top of Page

AAAV
Posting Yak Master

152 Posts

Posted - 2010-03-09 : 09:33:20
ok...

In UP_Ownership_Total i create
CREATE TABLE [#Results_Total](
[vc_trimid] [int] NULL,
[cost_type] [nvarchar](255) NULL,
[year] [smallint] NULL,
[year_total] [decimal](10, 4) NULL,
[month1] [decimal](10, 4) NULL,
[month2] [decimal](10, 4) NULL,
[month3] [decimal](10, 4) NULL,
[month4] [decimal](10, 4) NULL,
[month5] [decimal](10, 4) NULL,
[month6] [decimal](10, 4) NULL,
[month7] [decimal](10, 4) NULL,
[month8] [decimal](10, 4) NULL,
[month9] [decimal](10, 4) NULL,
[month10] [decimal](10, 4) NULL,
[month11] [decimal](10, 4) NULL,
[month12] [decimal](10, 4) NULL,
[datetime] [smalldatetime] NULL
)

This calls UP_Fuel_cost, UP_Maintenance etc...
these calls one common procedure UP_Insert_Results which referes to #Results_Total and insert the rows for each cost

Finally comming back to UP_Ownership_Total i want to return all the rows in a select to the caller.

Now I want to know if i can refer to #results_total in UP_Insert_Results though it is created in the caller up the line UP_Ownership_total
The code is too large to post here. will this help?

Go to Top of Page

haroon2k9
Constraint Violating Yak Guru

328 Posts

Posted - 2010-03-09 : 09:36:45
quote:
Originally posted by AAAV

ok...

In UP_Ownership_Total i create
CREATE TABLE [#Results_Total](
[vc_trimid] [int] NULL,
[cost_type] [nvarchar](255) NULL,
[year] [smallint] NULL,
[year_total] [decimal](10, 4) NULL,
[month1] [decimal](10, 4) NULL,
[month2] [decimal](10, 4) NULL,
[month3] [decimal](10, 4) NULL,
[month4] [decimal](10, 4) NULL,
[month5] [decimal](10, 4) NULL,
[month6] [decimal](10, 4) NULL,
[month7] [decimal](10, 4) NULL,
[month8] [decimal](10, 4) NULL,
[month9] [decimal](10, 4) NULL,
[month10] [decimal](10, 4) NULL,
[month11] [decimal](10, 4) NULL,
[month12] [decimal](10, 4) NULL,
[datetime] [smalldatetime] NULL
)

This calls UP_Fuel_cost, UP_Maintenance etc...
these calls one common procedure UP_Insert_Results which referes to #Results_Total and insert the rows for each cost

Finally comming back to UP_Ownership_Total i want to return all the rows in a select to the caller.

Now I want to know if i can refer to #results_total in UP_Insert_Results though it is created in the caller up the line UP_Ownership_total
The code is too large to post here. will this help?





as for as i can understand..

could you refer this thread as quickly as possible please
http://beyondrelational.com/blogs/madhivanan/archive/2007/11/24/select-columns-from-exec-procedure-name-is-this-possible.aspx
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-03-09 : 10:03:39
quote:
Originally posted by madhivanan

No. The temporary table created inside a procedure wont available in other procedure

Madhivanan

Failing to plan is Planning to fail



Hi Madhivanan,

I think you may have misread the question.

AAAV -- if you create a temp table in a procedure then it will be available to any procedure called from it. However if you were to call the 'inner' procedure directly without going from your outer procedure it will likely fail.


DROP PROC myProcB
GO

CREATE PROC myProcB AS BEGIN

INSERT #foo ([a]) VALUES (10)

END
GO

DROP PROC myProcA
GO

CREATE PROC myProcA AS BEGIN

CREATE TABLE #foo (
[a] INT
)

EXEC dbo.myProcB

SELECT * FROM #foo
END
GO

EXEC dbo.myProcA



Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-03-09 : 10:12:43
<<
However if you were to call the 'inner' procedure directly without going from your outer procedure it will likely fail.
>>

Yes. I assumed that OP needed that way and thats why my first reply

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

AAAV
Posting Yak Master

152 Posts

Posted - 2010-03-09 : 10:48:59
ok... i solved the problem... There was a ref to the table Results hard coded in another procedure. once i changed it to #Results it worked.

and yes A calling B calling C
if you create a temp table in A you can use it in C. It is still in the same context.
Thanks all for your time and help.
Go to Top of Page
   

- Advertisement -