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)
 Need help on combine table name using +

Author  Topic 

Delinda
Constraint Violating Yak Guru

315 Posts

Posted - 2009-10-27 : 08:27:10
I've list of table as follow,

DERTimer_200910
DERTimer_200909
DERTimer_200908
DERTimer_200907

I want to make my sql statement dynamically as follow
declare @currDteInYYYYMM as varchar(10)
set @currDteInYYYYMM='200910'
select ECout from TRoutePrice, DERTimer_+ @currDteInYYYYMM +

But i got an error as follow,
Incorrect syntax near '+'.

Can somebody help me what the syntax problem?

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-10-27 : 08:39:39
www.sommarskog.se/dynamic_sql.html

Madhivanan

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

Delinda
Constraint Violating Yak Guru

315 Posts

Posted - 2009-10-27 : 09:38:49
quote:
Originally posted by madhivanan

www.sommarskog.se/dynamic_sql.html

Madhivanan

Failing to plan is Planning to fail



My SDate is DateTime

after using @sql, i got
select ECout,Desn from TRoutePrice a, DERTimer_200907 b, TCounter c where b.SDate=Jul 18 2009

how to make it sql above to
select ECout,Desn from TRoutePrice a, DERTimer_200907 b, TCounter c where b.SDate='7/18/2009'

Currently, i using
SELECT @sql = ' select ECout,Desn' +
' from TRoutePrice a, DERTimer_'+@currDteInYYYYMM+' b,' +
' TCounter c' +
' where b.SDate='+cast(@SalesDte as varchar(12))+''
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-10-27 : 09:53:10
SELECT @sql = ' select ECout,Desn' +
' from TRoutePrice a, DERTimer_'+@currDteInYYYYMM+' b,' +
' TCounter c' +
' where b.SDate='''+cast(@SalesDte as varchar(12))+''''

Madhivanan

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

Delinda
Constraint Violating Yak Guru

315 Posts

Posted - 2009-10-27 : 09:59:29
quote:
Originally posted by madhivanan

SELECT @sql = ' select ECout,Desn' +
' from TRoutePrice a, DERTimer_'+@currDteInYYYYMM+' b,' +
' TCounter c' +
' where b.SDate='''+cast(@SalesDte as varchar(12))+''''

Madhivanan

Failing to plan is Planning to fail



Tq very much
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-10-27 : 10:08:30
quote:
Originally posted by Delinda

quote:
Originally posted by madhivanan

SELECT @sql = ' select ECout,Desn' +
' from TRoutePrice a, DERTimer_'+@currDteInYYYYMM+' b,' +
' TCounter c' +
' where b.SDate='''+cast(@SalesDte as varchar(12))+''''

Madhivanan

Failing to plan is Planning to fail



Tq very much


You are welcome

Madhivanan

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

- Advertisement -