It there a way to create a table in SQL Server 2016 containing a variable in the name without using code like this?
SET @SQL = 'Create table dbo.tblTest_' + @table + ' ...'
EXEC @SQL
We have existing stored procedures where we create tons of temp tables. I got the task to create tools that would allow the new developers to troubleshoot that big process in an easier way when we have issues with data, so I would like to be able to pass a parameters that will add the service request number to the temporary table name.
Is that clear enough ?
Here is an example:
SELECT DISTINCT
StartingOperatingEntityNumber,
CAST(0 AS INT) AS StartingOperatingId,
OperatingEntityNumber, ParentId, StructureLevel,
CAST(0 AS INT) AS CollectionEntityId
INTO
Temp.tblCEProcessingTempAllAncestors_WIT19101
FROM
AllParents a
CREATE NONCLUSTERED INDEX [IX_TempAllAncestors_StartingOperatingEntityNumber]
ON Temp.tblCEProcessingTempAllAncestors_WIT19101
([StartingOperatingEntityNumber] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF,
DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
CREATE NONCLUSTERED INDEX [IX_TempAllAncestors_ParentId_StructureLevel]
ON Temp.tblCEProcessingTempAllAncestors_WIT19101
([StructureLevel] DESC, [ParentId] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF,
DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
You can see that I add the WIT number to each temporary table.
I tried something like this:
SELECT DISTINCT
StartingOperatingEntityNumber,
CAST(0 AS INT) AS StartingOperatingId,
OperatingEntityNumber, ParentId, StructureLevel,
CAST(0 AS INT) AS CollectionEntityId
INTO
Temp.tblCEProcessingTempAllAncestors_@WIT
FROM
AllParents a
CREATE NONCLUSTERED INDEX [IX_TempAllAncestors_StartingOperatingEntityNumber]
ON Temp.tblCEProcessingTempAllAncestors_@WIT
([StartingOperatingEntityNumber] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF,
DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
CREATE NONCLUSTERED INDEX [IX_TempAllAncestors_ParentId_StructureLevel]
ON Temp.tblCEProcessingTempAllAncestors_@WIT
([StructureLevel] DESC, [ParentId] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF,
DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
But it obviously didn't work....
Is there a fast and easy way to do this ?
Thanks in advance :-)
Mylene