Link to home
Start Free TrialLog in
Avatar of Russ Suter
Russ Suter

asked on

Create a stored procedure that can update any column

Here's a simple stored procedure:
CREATE PROCEDURE [foo]
(
	@Id INT,
	@Value VARCHAR(MAX)
)

AS

UPDATE
	[dbo].[Element]
SET
	[Element].[ColumnName] = @Value
WHERE
	[Element].[Id] = @Id

Open in new window

It works just fine for its purpose. However, what do I do if I want ColumnName to be a passed variable? Something that would work more like this:
CREATE PROCEDURE [foo]
(
	@Id INT,
	@Key VARCHAR(MAX),
	@Value VARCHAR(MAX)
)

AS

UPDATE
	[dbo].[Element]
SET
	[Element].[@Key] = @Value
WHERE
	[Element].[Id] = @Id

Open in new window

How do I accomplish that task?
ASKER CERTIFIED SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Russ Suter
Russ Suter

ASKER

Oh, I know but this is an unusual case. I'm not a fan of it either but this is an internal deployment only and I have no idea how else to accomplish what I need.
{Corrected my original post.  The @id needs to be converted to a varchar to be concatenated with the rest.}