Click here to Skip to main content
15,901,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Is it Possible pass the column name as string value to INSERT STORED PROCEDURE in SQL Server?

SQL
INSERT INTO SwipeHrs(EmpID,EmpCode,Dept_Id,Team_Id,At_Date,@UConcateSHColumn,@UConcatePTColumn)VALUES (@empid,@empcode,@dept,@team,@CurrentDate,@UConcateSH,@UConcatePT)
Posted
Updated 14-Feb-14 22:05pm
v2

Yes...ish.
You can't do it directly, as the column names are parsed at a higher level, so you can't directly pass a variable.
But...you can do it if you build up a string:
SQL
DECLARE @COLNAME NVARCHAR(MAX)
SET @COLNAME = 'MyColumn'
DECLARE @SQL NVARCHAR(MAX)
SET @SQL = 'SELECT ' + @COLNAME + ' FROM MyTable'
EXEC (@SQL)

This selects the column, but INSERT is the same procedure
 
Share this answer
 
Hi OriginalGriff


But, i have 62 Columns.. how do i Check and pass the columns to there?
 
Share this answer
 
Comments
Krunal Rohit 17-Feb-14 11:18am    
This is not an answer.
Use the Comment widget underneath the Solution. :)

-KR

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