| Author |
Topic |
|
bpgupta
Yak Posting Veteran
75 Posts |
Posted - 2008-02-05 : 02:03:46
|
| HiI wanted to use the table variable in Stored proc , for that i have create the table variable in the main SP which will be used by again called sp(child SPs)now when i am trying to use the same table variable in the child SP, at the time of compliation it is showing error Msg 1087, Level 15, State 2, Procedure fwd_price_cons, Line 149Must declare the table variable "@tmp_get_imu_retn".Can any body give me the idea how to complile the child SP with the same table variable used in the main SP.Thanks,BPG |
|
|
sunil
Constraint Violating Yak Guru
282 Posts |
Posted - 2008-02-05 : 02:12:03
|
| This is an excerpt from Inside Microsoft SQL Server 2005: T-SQL Programming:Scope and VisibilityThe scope of a table variable is well defined. It is defined as the current level, and within it the current batch only, just as with any other variable. That is, a table variable is not accessible to inner levels, and not even to other batches within the same level. In short, you can use it only within the same batch it was created. This scope is much more limited than that of a local temporary table and is typically an important factor in choosing a temporary object typeHope it gives you reason for error. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-05 : 02:34:18
|
| I think you need to use a global temp table (##table) for your purpose. |
 |
|
|
bpgupta
Yak Posting Veteran
75 Posts |
Posted - 2008-02-05 : 02:53:45
|
| But as per our standard We can't use the temp table , Is there any way to use table variable in this issue.Please advice.Thanks,BPG |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-05 : 02:55:43
|
| Can you explain your scenario in a bit more detail? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-02-05 : 03:00:02
|
Visakh, if you create a temp table #t in SP a and then make a call to SP b, temp table #t is visible in SP b.CREATE PROC aASCREATE TABLE #t (i INT)INSERT #tSELECT 1EXEC bGOCREATE PROC bASSELECT * FROM #tGOEXEC aGODROP PROC aDROP PROC bGO E 12°55'05.25"N 56°04'39.16" |
 |
|
|
bpgupta
Yak Posting Veteran
75 Posts |
Posted - 2008-02-05 : 03:09:13
|
| Currently i am using the Temp table in all the SP .the temp table is created in the main SP , which can be used in Child SP also for the calculation for other things.Now we have to change the Temp table to Variable Table , but while changing to Variable Table , Child SP is not compling showing the error message Msg 1087, Level 15, State 2, Procedure fwd_price_cons, Line 149Must declare the table variable "@tmp_get_imu_retn".Now i have to use the Table variable in all the SP , which is used in the main SP.For example Main SP1 - A is used for declaration of table variable @table1Now the @table1 should be used in all the child SPs say B,C and D.Hope i am clear now.thanks,BPG |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-02-05 : 03:20:50
|
Sunil told you why. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-05 : 03:30:46
|
quote: Originally posted by Peso Visakh, if you create a temp table #t in SP a and then make a call to SP b, temp table #t is visible in SP b.CREATE PROC aASCREATE TABLE #t (i INT)INSERT #tSELECT 1EXEC bGOCREATE PROC bASSELECT * FROM #tGOEXEC aGODROP PROC aDROP PROC bGO E 12°55'05.25"N 56°04'39.16"
Yeah, i see here now. Since they are in same connection. Second SP will be able to use it.Sorry i overlook this a bit. |
 |
|
|
bpgupta
Yak Posting Veteran
75 Posts |
Posted - 2008-02-05 : 05:07:09
|
| Can u do the same things using the variable Table. |
 |
|
|
sunil
Constraint Violating Yak Guru
282 Posts |
Posted - 2008-02-05 : 05:23:53
|
| No, you cannot. See what I posted earlier. Its a limitation of table variable. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-02-05 : 06:03:36
|
No, and Sunil told you why. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|