I need to write a generic procedure that set value for one column or no of a columns in a table depending on parameters. Any idea how to do it.
A:
I would guess you want something like:
CREATE PROC UpdateProc
@RowID UNIQUEIDENTIFIER,
@Parameter1 NVARCHAR(50) NULL,
@Parameter2 INT NULL
AS
SET NOCOUNT ON
GO
IF @Parameter1 IS NOT NULL
BEGIN
UPDATE MyTable
SET Column1 = @Parameter1
WHERE ID = @RowID
END
IF @Parameter2 IS NOT NULL
BEGIN
UPDATE MyTable
SET Column2 = @Parameter2
WHERE ID = @RowID
END
It doesn't feel particularly elegant but if you don't know/can't guarantee which parameters will be passed I don't know of a better way than to test them in turn for NULL
.
PhilPursglove
2010-03-11 10:50:15
Can I do this by using @Parameter1 NVARCHAR(50) NULL, set id = ISNULL(@Parameter1, id) - Will it set the value to null if the parameter is passed as null
sam
2010-03-11 10:59:28
Not sure I understand what you mean - can you give me a bit more info on what you're trying to do?
PhilPursglove
2010-03-11 12:03:30