Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to have a variable handle on a data table column but don't know how. I am a rookie here anyway

How do I assign the value @PayPattnXEl to the column indicated by the variable @ColumnID.
SQL
ALTER PROCEDURE dbo.TransPayPattn	
(
 @InputArgs VARCHAR(MAX) OUTPUT,
 @ColumnID NCHAR(10),
 @RowNo INT OUTPUT,
 @Response   BIT OUTPUT
)

SET @Column = '['+@Column+']'
UPDATE PayPatternDtab SET @ColumnID = @PayPattnXEl WHERE PattnRowID=@RowNo
Posted
Updated 5-Sep-11 20:24pm
v3
Comments
Wendelius 5-Sep-11 14:35pm    
pre-tags added and all caps text corrected.
Philippe Mori 5-Sep-11 20:13pm    
Why is it tagged for C++. This is SQL.
Emilio Garavaglia 6-Sep-11 2:25am    
Retagged as SQL

1 solution

Do you mean you want to dynamically change the column to update in your SQL statement (and inside a procedure).

If that's correct, you cannot do it directly. Before jumping to solutions, in my opinion this isn't what you should even do unless you have strong reasons.

Don't know anything more about your goal but it looks like you're creating a procedure that would update a single column in a single row at a time. If it's true, why? For example, this may lead to excessive amount of round trips between the client and the database if several columns are updated in a single row. Also that kind of structure may lead to several other problems and extra coding.

Anyhow, if you need to execute dynamic SQL statements inside a procedure, you basically have two options:
- EXECUTE[^]
- sp_executesql[^]
 
Share this answer
 

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