demo999 89
demo999 89

Reputation: 105

Insert data if it does not exist

I want to insert some data in my column only if my query parameters are not already added.

For example, if my row contains :

a=4&b=7&c=9

and now, when update happens with : b=7&c=9, then i should not append it.

o/p:a=4&b=7&c=9

But if update happens with d=9&e=9 then it should append it.

o/p : a=4&b=7&c=9&d=9&e=9

My normal Update query is :

@AdditionalParams = 'b=7&c=9'
SELECT @id = mid FROM Table2 WHERE sid = @SId
                AND cid = @CId;

            UPDATE Table1
            SET additional_params = CONCAT (
                    additional_params
                    ,iif(additional_params IS NULL, NULL, '&')
                    ,@AdditionalParams
                    )
            WHERE mid = @id

How can i use here the NOT EXIST Clasue.

But with not exist clause it checks whole row, I just want to check if parameters exist, then dont insert it.

Upvotes: 0

Views: 52

Answers (1)

vercelli
vercelli

Reputation: 4767

I guess you are looking for a not like clause

declare @AdditionalParams varchar(50) = 'b=7&c=8'
SELECT @id = mid 
   FROM Table2 
  WHERE sid = @SId
   AND cid = @CId;

  UPDATE Table1
  SET additional_params = CONCAT (
                     additional_params
                    ,iif(additional_params IS NULL, NULL, '&')
                    ,@AdditionalParams
                    )
   WHERE mid = @id
     and additional_params not like '%'+ @AdditionalParams +'%';

Upvotes: 1

Related Questions