SQL Server: How to use Update statement with xml input -


i use sql server 2008 , have stored procedure 1 input parameter formatted xml. xml list of names, 1 word each without spaces.

for each of these names want check if exist in table, if no should added table, if yes should updated there.

so far have part add them if don't exist yet works intended can't figure out how realise updating part.
can here me ?

just demonstration update part stand-alone (if have 1 input instead of xml):

update  rc_permissionsusers set     ntid = @ntid,         departmentid = @departmentid,         role = @role   ntid = @ntid 

the rest of procedure insert part (working):

begin      set nocount on;       begin transaction;          begin                    insert rc_permissionsusers             (                             ntid,                             departmentid,                             [role]             )             select          paramvalues.ntid.value('.', 'varchar(255)'),                             @departmentid,                             @role                        @xmlusers.nodes('/users/ntid') paramvalues(ntid)             not exists              (                 select      ntid                        rc_permissionsusers                       ntid = paramvalues.ntid.value('.', 'varchar(255)')             )                         end      commit transaction;   end 

many in advance, tim.

we can use merge, select xml values temporary table , use merge

begin      set nocount on;       begin transaction;          begin                  if object_id('tempdb.dbo.#tmplist') not null drop table #tmplist             create table #tmplist(                 id                      varchar(255),                 departmentid            int,                 role                    int             )              insert #tmplist             select          paramvalues.ntid.value('.', 'varchar(255)'),                             @departmentid,                             @role                        @xmlusers.nodes('/users/ntid') paramvalues(ntid)              merge  rc_permissionsusers pu             using ( select id, departmentid, role #tmplist) t                     on pu.ntid = t.id             when matched                  update set departmentid = t.departmentid,                            role = t.role             when not matched                 insert ( ntid, departmentid, role)                      values ( t.id, t.departmentid, t.role)                      end      commit transaction;   end 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -