I would like to be able to choose which field in my table is updated by my stored procedure based on an input parameter’s value. If the input field is equal to ‘draft’ it should update id_draft, otherwise it should update id.
Is this possible?
This is one of my failed attempts that may help illustrate what I want to do:
CREATE OR REPLACE PROCEDURE sp_test (input_field VARCHAR DEFAULT NULL)
IS
BEGIN
UPDATE TABLE
SET
CASE
WHEN input_field = 'draft' THEN id_draft
ELSE id
END = 'x'
You can’t use CASE statement to alter the query structure itself. It only returns data, it’s a function.
What you have (
CASE WHEN input_field = 'draft' then id_draft ELSE id END) returns the value in either theidfield or theid_draftfield. It’s like doing this…Instead, you need to put the CASE statement on the right hand side…
But, actually, you may just be better off doing this…
EDIT:*
To use a value from another table instead of just
'x', you can use something like this…