My table name is dynamic, they are something like "PM1234", whereas "PM" is hard coded, and "1234" comes from a column of another table, say select max(col1) from table1
. Is there any way to put PM and 1234 together as table name?
New data will be inserted into "table1", so the data will be increased, this time "max(col1)" is "1234", next time it will be "1235".
Here's what I tried:
SELECT *
FROM (SELECT 'PM'+ CAST(MAX(col1) AS NVARCHAR)
FROM table1)
but it doesn't work.
I just figured out how to make it work:
DECLARE @sql varchar(200)
DECLARE @ID varchar(200)
SELECT @ID = MAX(ID) FROM table1
SET @sql = 'SELECT * FROM PM' + @ID
EXEC(@sql)