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
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
How do I accomplish that task?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
{Corrected my original post. The @id needs to be converted to a varchar to be concatenated with the rest.}
ASKER