I am using MSSQL 2016,
I need to be able to update a row on a table dynamically.
I got a stored procedure :
CREATE PROCEDURE sp_lookupData_UpdatelookupValues
(
@FullTableName nvarchar(50),
@Id nvarchar(10),
@Name nvarchar(50),
@Description nvarchar(50)
)
AS
BEGIN
DECLARE @Cmd nvarchar(150) = N'UPDATE ' + @FullTableName + ' SET Name = ' + @Name + ', Description = ' + @Description + ' WHERE ID = ' + @Id + '';
EXECUTE sp_executesql @Cmd;
END
The problem is that Name and Description values are passed into the @Cmd like this :
UPDATE TABLE_NAME SET Name = Private, Description = Default WHERE ID = 1
Instead of 'Private' and 'Default'.
The result is an error where Private is being counted as a column which doesnt exist ( because of the bad format ).
Invalid column name 'Private'.