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 2008 Forums
 Transact-SQL (2008)
 set dynamic variables

Author  Topic 

sunnytrader
Starting Member

3 Posts

Posted - 2011-06-20 : 08:16:00
Hello

i have around twenty types of currency names. For example @aud, @usd ..etc

I am trying to set up the variable dynamically as follows

set @sql = 'set @' + @buycurrency + ' = @' + @buycurrency + ' + 1' ;
exec(@sql);
where buycurrency holds "aud", "usd" etc...
The problem is that variables are not being set . I have done debugging in SSMS and the @sql holds @aud = @aud + 1 etc..
what is wrong here?
thanx guys

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-06-20 : 08:22:14
You should get an error as the variables are defined in the scope of the statement.
Should also get an error as the where clause isn't part of a statement.

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

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-06-20 : 08:29:12
My opinion: stop trying to do that.
Maybe you can explain where you need this and we can give another solution...


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

sunnytrader
Starting Member

3 Posts

Posted - 2011-06-20 : 08:46:30
Thanx for the offer Nigel. I will try to explain as neatly as possible.

So i have a table with 23 columns. Out of which one column is primary key, one is date time and rest are values of currency pairs.

So the structure is like this
TABLE 1
id datetime AUD_USD USD_CAD USD_JPY .....(total 23 columns)
2345 1/1/2010 -0.1 0.1 -0.1
........

I need to use the data in this table to populate another table which is of type

TABLE 2
id datetime AUD(int) USD(int) EUR(int)


The criteria is this...
if in a row of table 1 a value is positive ...then the buy currency will be added with 1 and sell currency reduced by 1 (in AUD_USD AUD is buy currency and USD is sell currency)

so in my example of table 1 , it will cause the table 2 to be filled with this row


id datetime AUD(int) USD(int) CAD JPY
2345 1/1/2010 -1 2 -1 1
(aud_usd is -ve)(aud_usd is -ve)
+
(usd cad is +ve)
-
(usd_jpy is negaitive)

i hope that was clear enough.

Thanx guys


Go to Top of Page
   

- Advertisement -