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.
| 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, bytesFROM @TableCharServer: Msg 137, Level 15, State 2, Line 22Must 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 |
 |
|
|
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 |
 |
|
|
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 |
 |
|
|
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. |
 |
|
|
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 ??? |
 |
|
|
raymondpeacock
Constraint Violating Yak Guru
367 Posts |
|
|
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 |
 |
|
|
|
|
|
|
|