sql server 2008 - Updating a table instead of Insert Into -


i selecting data table , inserting if data in table not exist.

declare @employeenumber nvarchar(100) set @employeenumber = 'emp001' if exists(select e.employeenumber [databaseone].[dbo].[employee] e e.employeenumber =    @employeenumber) begin if not exists(select ut.employeenumber usertable ut ut.employeenumber = @employeenumber) begin     insert usertable(employeenumber, surname)     select e.employeenumber, e.surname [databaseone].[dbo].[employee] e     e.employeenumber = @employeenumber end else   begin       --update   end end 

if data exist want update existing data.

how can update opposed insert into.

kind regards

you should use merge funcion.

your query this:

merge usertable target using (select e.employeenumber, e.surname [databaseone].[dbo].[employee] e         employeenumber = @employeenumber) source on target.employeenumber = source.employeenumber when mathced       update set target.surname = source.surname when not matched       insert (employeenumber, surname) values (source.employeenumber,source.surname); 

refer here.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

How do you convert a timestamp into a datetime in python with the correct timezone? -