Click here to Skip to main content
15,886,714 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
PartID	Code	CodeTypeID	RevisionID	ZPLID	ConCount	FeatureName	FeatureValue
2020	ab5060	877491	26553312	4125	10	Heat	IC


What I have tried:

SQL
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 name
                FROM tempdb.sys.columns
				WHERE [object_id] = OBJECT_ID(N'tempdb..#AllData')
        
                ) k
            FOR XML PATH('')
            ), 1, 1, '')

			SELECT * FROM #AllData
Posted
Updated 17-Nov-21 8:36am

1 solution

You have numeric data types in your table, for example partid, so it's not possible to insert a row with text in those columns.

What you can do is that you add the column names in your select to the result set but in order to do this you'd need to convert all data to text and that's generally not a good idea.

I don't quite see the reason for your request. The only thing I can imagine is that you need to know the names of the columns on the client side. However, almost all modern query API's fetch the column metadata such as name, type, length etc so this would be easy to investigate on the client side even when you don't add the names of the columns into the data.
 
Share this answer
 
Comments
Maciej Los 18-Nov-21 17:06pm    
5ed!
Wendelius 20-Nov-21 1:35am    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900