Dit kan niet met parameters. U moet een dynamische query maken om dit te bereiken.
De procedure die dynamische SQL gebruikt, ziet er als volgt uit:
create procedure MyProc
(
@ColumnName varchar(100),
@ColumnValue varchar(100),
@MH_Description varchar(100)
)
as
begin
declare @Query nvarchar(4000)
declare @ParmDefinition nvarchar(500);
set @Query = '
insert into Tablename(' + quotename(@ColumnName) +',Description)
values (@ColumnValue, @MH_Description)'
set @ParmDefinition = N'@ColumnValue varchar(100), @MH_Description varchar(100)'
exec sp_executesql @Query, @ParmDefinition, @ColumnValue = @ColumnValue, @MH_Description = @MH_Description
end
[EDIT] Antwoord op je tweede vraag. Maak er één vraag van in plaats van twee
set @Query = '
if exists(select * from tbl_temp where '+quotename(@ColumnName)+' = @ColumnValue)
update tbl_temp set' + quotename(@ColumnName) +' = @ColumnValue, [email protected]
else
insert into tbl_temp(' + quotename(@ColumnName) +',Description)
values (@ColumnValue, @Description)'