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)
 shared variables between contexts

Author  Topic 

heze
Posting Yak Master

192 Posts

Posted - 2006-04-17 : 15:35:12
Hi
Does anybody know if if is possible to reference a variable from a string inside an exec statement to one outside the exec?

ie
declare @var as int
exec ('set @var=2')

the output is :
Server: Msg 137, Level 15, State 1, Line 1
Must declare the variable '@var'.


thannk you

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-04-17 : 15:59:47
That is not possible. Could you show us an actual example of what you are trying to do or explain what you want?

Tara Kizer
aka tduggan
Go to Top of Page

heze
Posting Yak Master

192 Posts

Posted - 2006-04-17 : 16:15:45
create proc somename
@t int
as
exec(select * from ...where myfield=@t)
exec(select * from ...where myfield=@t)
.
.
.
exec(select * from ...where myfield=@t)
--

Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2006-04-17 : 16:15:53
You can do that using sp_executesql

declare @var as int
exec sp_executesql N'set @var=2', N'@var int out', @var out



==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-04-17 : 16:19:10
exec('select * from ...where myfield=' + @t)

Tara Kizer
aka tduggan
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-04-17 : 16:20:49
I was only going to offer sp_executesql if there was no workaround for what he was trying to do. My reply shouldn't really have said that it's not possible, but more along the lines of: "it's possible, but before we show you the keys to the kingdom, show us why you want to do this".

Tara Kizer
aka tduggan
Go to Top of Page

heze
Posting Yak Master

192 Posts

Posted - 2006-04-17 : 16:25:12
sorry I omitted a detail...

create proc somename
@t int
as
exec('select * from MyTableRootName'+@t+ ' where myfield='+@t)
exec('select * from MyTableRootName'+@t+ ' where myfield='+@t)

.
.
.
exec('select * from MyTableRootName'+@t+ 'where myfield='+@t)
--
the queries are actually select-into statements, about 100 temporary tables will be produced.
So according to what yuoguys are writting, I have to include all the statement into the exec string.

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-04-17 : 16:29:34
Or you could fix your database design so that you don't have Table1, Table2, Table3...

Tara Kizer
aka tduggan
Go to Top of Page

heze
Posting Yak Master

192 Posts

Posted - 2006-04-17 : 16:32:18
ok

I started with cursors..
First.

I want to flaten a foreign key table field and feed it to the primary key
say we have pk table:
Term IdStudent
2002 234
and we have fk Table
Term IdStudent CourseTaken
2002 234 aerodynamics
2002 234 english
2002 234 chemistry

I want to add a new field in pk Table
that shows:
Term IdStudent FlatCoursesTaken
2002 234 aerodynamics:english:chemistry

I already developed an algorithm using cursors, testing some samples however it was going to take me 15 hours to complete the process. Forget cursors, I tried temp tables, went down to forecasted 10 hours, also forget it, too much. Then I will do very small tables and only do operations in them not on the large ones, for example the joining of pk and fk tables yields one million rows.

thanks
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-04-17 : 16:34:13
That is a horrible design if you want that row in the pk table.

Tara Kizer
aka tduggan
Go to Top of Page

heze
Posting Yak Master

192 Posts

Posted - 2006-04-17 : 16:39:27
I know t, but its for a report they want. They want to know all the courses taken and they want it fast, I proposed this and they loved it. They have everything in the same place and can analyze the patterns of a given group of IDs with bare eye. I will further separate each course taken into a respective row given that only a maximum of 7 courses can be taken. But the procedures are similar using my current apporoach.
Go to Top of Page

heze
Posting Yak Master

192 Posts

Posted - 2006-04-17 : 16:40:20
Any suggestions?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-04-17 : 16:44:09
You just query the FK table for the information and display it the way you want it via the application. Do not store this data in SQL Server this way.

Tara Kizer
aka tduggan
Go to Top of Page

heze
Posting Yak Master

192 Posts

Posted - 2006-04-17 : 16:54:47
t,
I will have to do that if this last try yields the same kind of performance. However, please take into consideraton that I am building a pivot table source. The analysts will extract the data from sql server into excel and bui;d a pivot table.
The pivot table has demonstrated superior to any application we currently have for the specific analysis they are doing. Since the pivot table goes by fields, I am just creating hew fields with appropriate values ofr display. In a way I am building a very archaic analysis services tool. I went this way and not used analysis services because I was just not succesfull. I was also thinking about implementing an extended stored procedure using c but then I thought... I just want to get over this and move on in life.
Go to Top of Page

heze
Posting Yak Master

192 Posts

Posted - 2006-04-17 : 23:23:47
Hi,

I finally was succesful with my last approach. What I bascally did was to create a table for pk and another for fk for each point in time. I had 30 points in time therefore I have 60 individual pk and fk tables. In the same process of table creation, I inner joined both tables. I then use a variable assigment select (this is just a descirpiotn mnot sure of the formal name) ie select @myfield1=@myfield1+coursecodefield from... I then updated the column of the partial pk table.
At the end I am left with 30 updated pk tables(in the flatgrade field).I then generate a string that "unions" the 30 partial tables , then exec(@string) then I just materialize the table into my database and delete all the temporary tables that were produced.
Again I tried with cursors: best case ~15hrs
while cycles using entire tables~10 hrs
this approach 1hr:59 mins
Go to Top of Page
   

- Advertisement -