Sql instead of insert trigger - insert data if does not exist -
i have following trigger on table redirects data , includes data 2 other tables based on left outer join.
if i.ndl_deviceid not exist in bbownermap column null fine.
what want is, if i.ndl_deviceid not exist in bbownermap want insert there , return resulting autonumber bbownermap.ownerid
trigger follows:-
alter trigger [dbo].[redirect] on [dbo].[ndl_dump] instead of insert begin insert ndl_data (ndl_image,ndl_text,ndl_lat,ndl_lng,ndl_categoryid,ownerid) select i.ndl_image, i.ndl_text, i.ndl_lat, i.ndl_lng, ndl_config.ndl_categoryid, bbownermap.ownerid inserted left outer join ndl_config on i.ndl_category = ndl_config.ndl_categoryname left outer join bbownermap on i.ndl_deviceid = bbownermap.deviceid end bbownermap table this:-
[dbo].[bbownermap]( [ownerid] [int] identity(1,1) not null, [deviceid] [varchar](50) not null, [devicenumber] [varchar](50) not null, constraint [pk_bbownermap] primary key clustered any on how modify appreciated.
thanks.
you should able add insert statement @ top of trigger. remember inserted table can have more 1 row, dealing sets, not rows. code assumes sql server. left joins don't find matches, same not exists clause:
insert bbownermap (deviceid, devicenumber) select i.ndl_deviceid, <devicenum> inserted left join bbownermap b on i.deviceid=b.deviceid b.ownerid null
Comments
Post a Comment