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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Local Variables

Author  Topic 

purnima.m
Starting Member

3 Posts

Posted - 2006-03-04 : 01:50:27
Hi ,

I am new to SQL Server.

1.Can anyone please tell me
the scope of local variables used in SQL Scripts?

2.If a local variable is being used in a Cursor's
while loop do we need to Reintialise the local variables
to prevent old values being retained?

Thank You

Punima

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2006-03-04 : 14:19:15
Local variables are limited in scope to the procedure in which they are declared. They cannot be referenced by either a calling procedure, or within a sub-procedure, or withing a dynamic sql statement.

The variable's scope is continuous within a cursor's loop, or any loop within the variable's procedure.
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2006-03-04 : 15:28:27
Since you're new to sql server you may be interested to hear that about 99 times out of a hundred, new sql developers use some type of loop/cursor solution when they should be using a set based solution. We'll be happy to show you a specific example if you want to post your existing cursor code. The performance difference can be like a thousand times faster as well as better for concurrent operations.

Be One with the Optimizer
TG
Go to Top of Page

purnima.m
Starting Member

3 Posts

Posted - 2006-03-05 : 05:16:13
Well i have a table with around 30 fields
from which i fetch only 21 fields and
using the codes in the field populate
my warehouse dimension with
corresponding description from other tables...

I use the cursor to populate local variables
with data from each record and then insert
the proper description into Dimension.

Eg. Select @District=Dist_name
from District where Dist_code=@dist

Purnima
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-03-05 : 05:25:10
Post your table's DDL and your existing cursor script.

----------------------------------
'KH'


Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2006-03-05 : 09:50:51
Generally the idea in this case would be to join your <30colulmn> table to your "corresponding..other" tables in a select statement and use the result as the basis of your insert into your Dimension table. You control the row selection with your JOIN and WHERE criteria. You control the column selection with your SELECT <colList>. If you want help with it, post the info KH suggested. (DDL just means the create table scripts that you can generate from sql server)

Be One with the Optimizer
TG
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2006-03-05 : 10:18:32
quote:
Originally posted by purnima.m

Well i have a table with around 30 fields
from which i fetch only 21 fields and
using the codes in the field populate
my warehouse dimension with
corresponding description from other tables...

I use the cursor to populate local variables
with data from each record and then insert
the proper description into Dimension.

Eg. Select @District=Dist_name
from District where Dist_code=@dist

Purnima

Exactly the situation TG was talking about. Post your code and we can show you a better way to do this.
Go to Top of Page
   

- Advertisement -