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 |
micnie_2020
Posting Yak Master
232 Posts |
Posted - 2014-03-15 : 08:02:58
|
Hi All,I can't run this query:- declare @tableName nvarchar(max) declare @tbl nvarchar(max) set @tableName='tblCurrencyRate.Description'SELECT @tbl=SUBSTRING(@tableName, 0, PATINDEX('%.%',@tableName))select * from @tblI already declare the @tbl. It's still raised me error:Must declare the table variable "@tbl".Please advise. Regards,Micheale |
|
micnie_2020
Posting Yak Master
232 Posts |
Posted - 2014-03-15 : 08:09:29
|
Thank you.I manage to solved the issue with:- declare @tableName nvarchar(max) declare @tbl nvarchar(max) declare @sSQL nvarchar(max) set @tableName='tblCurrencyRate.Description'SELECT @tbl=SUBSTRING(@tableName, 0, PATINDEX('%.%',@tableName))set @sSQL=N'select * from ' +@tbl EXEC sp_executesql @sSQL Regards,Micheale |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2014-03-15 : 08:11:36
|
quote: select * from @tbl
it would expect @tbl as a table variable. What you have is a string variable.Looking at your query, looks like you will need dynamic sql http://www.sommarskog.se/dynamic_sql.htmldeclare @tableName nvarchar(max)declare @tbl nvarchar(max)declare @sql nvarchar(max)set @tableName='tblCurrencyRate.Description'SELECT @tbl=SUBSTRING(@tableName, 0, PATINDEX('%.%',@tableName))select @sql = N'select * from ' + @tblexec (@sql) KH[spoiler]Time is always against us[/spoiler] |
|
|
|
|
|
|
|