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
 Transact-SQL (2000)
 getting every month a new table

Author  Topic 

rtr1900
Starting Member

48 Posts

Posted - 2008-05-08 : 07:52:38
Hi,

i have
SELECT COD_ART, DESCRIP, U1
FROM MAE_ART
WHERE (COD_ART = 2500)
ORDER BY COD_ART

There is U1,U2,....U12

I need to know if I can add a kind of way so that U1 changes in U2 in the month of february.

I thought something like

SELECT COD_ART, DESCRIP, U&month(getdate()) AS Expr1
FROM MAE_ART
WHERE (COD_ART = 2500)
ORDER BY COD_ART

But it tells me that the column U doesn´t exists (which is correct). How can I do that?

Thx in advance

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-05-08 : 08:47:47



declare @sql varchar(8000)
set @sql='SELECT COD_ART, DESCRIP, U'+cast(month(getdate()) as varchar(2))+' AS Expr1
FROM MAE_ART
WHERE (COD_ART = 2500)
ORDER BY COD_ART'
EXEC(@sql)

Also read about Normalisation

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

rtr1900
Starting Member

48 Posts

Posted - 2008-05-08 : 09:15:12
Hi madhivanan,

After executing the code, it tells me that the syntax is incorrect near the keyword 'AS'

But checking the sql code it tells me it is ok.

What could it be? The column name is correct an thtype is "Float 8". Should be alright, no?


Go to Top of Page

RyanRandall
Master Smack Fu Yak Hacker

1074 Posts

Posted - 2008-05-08 : 09:51:20
Here's an alternative...

SELECT COD_ART, DESCRIP,
CASE MONTH(GETDATE())
WHEN 1 THEN U1
WHEN 2 THEN U2
WHEN 3 THEN U3
WHEN 4 THEN U4
WHEN 5 THEN U5
WHEN 6 THEN U6
WHEN 7 THEN U7
WHEN 8 THEN U8
WHEN 9 THEN U9
WHEN 10 THEN U10
WHEN 11 THEN U11
WHEN 12 THEN U12
END AS Expr1
FROM MAE_ART
WHERE (COD_ART = 2500)
ORDER BY COD_ART
But definitely read about normalisation.


Ryan Randall
Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page
   

- Advertisement -