Thanks, simonet.
I added a FROM clause to @sql but I'm still seeing the same error:
use mydb;
declare @sql nvarchar(1000)
declare @Table nvarchar(19)
set @sql = 'select top 1 ''dbo.MyTable_''
+ substring(CONVERT(varchar(
+ ''_'' + substring(CONVERT(varchar(
+ 'FROM dbo.MyTable_01_2008'
exec sp_executesql @sql, @Table
declare @mySQL nvarchar(1000)
SET @mySQL = 'SELECT top 10 * FROM ' + @Table
EXEC (@mySQL)
Any other suggestions?
Main Topics
Browse All Topics





by: simonetPosted on 2008-09-17 at 09:54:07ID: 22500898
There is no FROM clause in the query held in @sql. Thus SQL Server thinks that "dbo.MyTable" is a string constant, and that's all it returns. Rewrite the statement so that there is a FROM clause pointing to your table, like this:
30), cast(''1-2-08'' as smalldatetime), 102),6,2) 30), cast(''1-2-08'' as smalldatetime), 102),1,4) from dbo.MyTable_'
set @sql = 'select substring(CONVERT(varchar(
+ ''_'' + substring(CONVERT(varchar(