I have a problem that I don't know how to solve in SQL. Or two problems.
- Create a Loop that would use table1 to update variable
- Use the variable in the name of source temp table and the name of final out output table.
Table1 has just one column
+--------+--+
| Header | |
+--------+--+
| Name1 | |
| Name2 | |
| Name3 | |
| Name4 | |
| ... | |
+--------+--+
So I want to Loop through those rows and use them as variable @variable. Something like:
IF OBJECT_ID('table_@variable','U') IS NOT NULL
DROP TABLE table_@variable
SELECT *
INTO table_@variable
FROM #@variable
So the first itinerary of the loop would be:
IF OBJECT_ID('table_Name1','U') IS NOT NULL
DROP TABLE table_Name1
SELECT *
INTO table_Name1
FROM #Name1
The second:
IF OBJECT_ID('table_Name2','U') IS NOT NULL
DROP TABLE table_Name2
SELECT *
INTO table_Name2
FROM #Name2
And so on as long there are rows in table1
I hope I explained it well enough.
Thank you for your help. Matt
p.s. I am using Microsoft SQL Server Management Studio to run my queries.