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)
 How to pass variable and uses as table's field name.

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-05-01 : 08:36:34
fenying writes "EXAMPLE:

set @day = datepart(dw,getdate())

if @day='1'
declare cursor_shift_details cursor for select Day_01 from
Shift_Data where .....

if @day='2'
declare cursor_shift_details cursor for select Day_02 from
Shift_Data where .....

and so on.


QUESTION:

@day's value will grow up to 31, so it is not the best way to check each value using IF statement. How to make the field name(Day_01, Day_02,...) to be dynamic variable? In other word, I don't want to use IF statement. Is it possible concantenate the @day's value with "Day_". "

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2002-05-01 : 08:54:16
search this site for dynamic sql.

we like to make a passtime of helping people re-write their code so as to not use cursors (rdbm systems function much better in a set based approach) . . . If you want to post your ddl and dml, someone here will probably take a stab at getting rid of the (lame) cursor.

<O>
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-05-01 : 09:29:12
Not to mention that if your table is designed like this:

Day_01 Day_02 Day_03 Day_04 ...

You have a serious design flaw, and it's causing you to use a cursor in the first place.

Also, datepart(dw, getdate()) will return the number of the WEEKDAY (Sunday, Monday, etc.) which are numbered 1-7, NOT the date (1, 2, 3 etc.) You should use datepart(day, getdate()) to get that value.

Go to Top of Page
   

- Advertisement -