Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Mahesh writes "Hi,I have two test tables: Test1 and Test2.I want to know the count of one of the tables dynamically. The table number will be passed to me at runtime.I have this simple code:declare @table_number intdeclare @count intset @table_number = 1exec('select @count = count(*) from Test'+convert(varchar, @table_number))print @countCan you please tell me what's wrong with the above code?When I execute it, I get the follwing error:syntax error at convertAny help is greatly appreciated"
khtan
In (Som, Ni, Yak)
17689 Posts
Posted - 2006-06-08 : 08:55:52
if you need to pass in / out parameters, you will need to use sp_executesql
declare @table_number intdeclare @count intdeclare @sql nvarchar(4000)set @table_number = 1-- you can't use convert() by itselfselect @sql = 'select @count = count(*) from Test'+convert(varchar(10), @table_number)exec sp_executesql @sql, N'@count int output', @count output