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)
 Syntax T-SQL Pb

Author  Topic 

squab
Starting Member

15 Posts

Posted - 2004-05-21 : 09:51:34
Hello,

I'm new in T-SQL and I have a syntax pb. Look :


DECLARE
@TableChar varchar(50)

SET @TableChar = 'BDDCTSRVNATNV..biStats_' + CAST(@YrDate AS varchar) + '_' + CAST(@WkDate AS varchar)
--PRINT @TableChar

SELECT applId, intervalID, deviceAId, deviceBId, bytes
FROM @TableChar


Server: Msg 137, Level 15, State 2, Line 22
Must declare the variable '@TableChar'.

Why I can't do this ???
Thanks in advance.

Regards,

SqUaB

raymondpeacock
Constraint Violating Yak Guru

367 Posts

Posted - 2004-05-21 : 09:52:34
Bexcause you have declared the variable as a varchar string and you can't select from a string. You can only select from a table.


Raymond
Go to Top of Page

squab
Starting Member

15 Posts

Posted - 2004-05-21 : 10:19:27
Hello Raymond,

Thanks for your answer, but can I do to solve my problem ? I can't specify a fix table name because my table name are composed by variables.

Best Regards,

SqUaB
Go to Top of Page

raymondpeacock
Constraint Violating Yak Guru

367 Posts

Posted - 2004-05-21 : 10:23:46
Look to build a string that contains your SELECT statement. Then use EXEC to perform it.

EXEC ('SELECT applId, intervalID, deviceAId, deviceBId, bytes FROM ' + @TableChar)




Raymond
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2004-05-21 : 10:47:32
this is a "dynamic sql" problem....search here for many examples.
Go to Top of Page

squab
Starting Member

15 Posts

Posted - 2004-05-21 : 10:55:07
quote:
Originally posted by AndrewMurphy

this is a "dynamic sql" problem....search here for many examples.



search where exactly ???
Go to Top of Page

raymondpeacock
Constraint Violating Yak Guru

367 Posts

Posted - 2004-05-21 : 11:14:37
Use the forum search:

http://www.sqlteam.com/forums/search.asp


Raymond
Go to Top of Page

squab
Starting Member

15 Posts

Posted - 2004-05-21 : 12:17:39
Yeah !

Thanks to all, it works fine !

Thanks a lot.

Best Regards,

SqUaB
Go to Top of Page
   

- Advertisement -