ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 277.9k

How to add top 1 row as header on SQL server as extra row ?

Nov 17 2021 7:10 PM

I work on SQL server 2014 I face issue

but I don't know how to solve it

I need to add columns header as first row on table

I Try as below but i get error

Msg 213, Level 16, State 1, Line 1

Column name or number of supplied values does not match table definition.

Expected result as below :

PartID Code CodeTypeID RevisionID ZPLID ConCount FeatureName FeatureValue
2020 ab5060 877491 26553312 4125 10 Heat IC

what i have tried

CREATE TABLE #AllData
(PartID INT,
Code VARCHAR(20),
CodeTypeID INT,
RevisionID BIGINT,
ZPLID INT,
ConCount INT,
FeatureName nvarchar(500),
FeatureValue  nvarchar(500))
insert into #AllData VALUES(2020,'ab5060',877491,26553312,4125,10,'Heat','IC')
insert into #AllData 
SELECT STUFF((SELECT ',' + CAST(name AS VARCHAR(50))FROM (SELECT nameFROM tempdb.sys.columns
WHERE [object_id] = OBJECT_ID(N'tempdb..#AllData')) kFOR XML PATH('')), 1, 1, '')SELECT * FROM #AllData

Answers (4)