Hi Friends,
I am facing a challenge with returning data with a SELECT and when I insert that SELECT output into a temp table:
/* Create table and insert value */
CREATE TABLE #T (TableName VARCHAR(50), SchemaName Varchar(20))
Insert into #T Values ('Employee','EMP')
Insert into #T Values ('Inventory','INV')
Insert into #T Values ('SalesOrder','SLE')
/* SELECT Data and put into a temp table #Schemas */
IF object_id('tempdb.dbo.#Schemas') IS NOT NULL drop table #Schemas
SELECT
SchemaName,
RANK() OVER (ORDER BY CHARINDEX(CAST(TableName AS VARCHAR), 'Inventory,Employee,SalesOrder') ) AS SeqNo
INTO #Schemas
FROM
#T
WHERE
TableName IN ('Inventory','Employee','SalesOrder')
ORDER BY CHARINDEX(CAST(TableName AS VARCHAR), 'Inventory,Employee,SalesOrder')
/* Do a SELECT on Temp Table - Returning Data in Right Sequence */
SELECT
SchemaName
FROM #Schemas
GROUP BY
SchemaName
ORDER BY MAX(SeqNo)
Output
INV
EMP
SLE
/* When insert output into a temp table for further use getting wrong ordering */
IF object_id('tempdb.dbo.#Final') IS NOT NULL drop table #Final
SELECT
SchemaName
INTO #Final
FROM #Schemas
GROUP BY
SchemaName
ORDER BY MAX(SeqNo)
select * from #Final
Output
EMP
INV
SLE
Can you point me to a right direction so that I can get output in the way (order of table).
REagrds
Regards Gursethi Blog: http://gursethi.blogspot.com/ ++++ Please mark "Propose As Answer" if my answer helped ++++