Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Update/Insert in one query

$
0
0
I have been reading up on Merge, but I don't think it is possible with what I am trying to do. I have a view that is my source and based on a recid table will determine if I need to update or insert into a table. If I need to insert, it will take the last recid and increment it by 1 for each row inserted on the fly as this is a primary key column.Thanks in advance for any guidance.If there is an insert, I would need to do something like the following to set the next recid for each row inserted:SET @RecID = COALESCE((SELECT nextval FROM dbo.systemsequences WHERE nextval = @recid), 0) + 1;What would be the best way to do this if Merge is not an option? In Oracle I would do something like the below, but not sure how to do this with TSQL:--Pass in v_dataareaid-- Get Cursor Insert/Update VariablesSELECT fiscalcalendaryear_endyear, fiscalcalendarperiod_periodid, fiscalcalendarperiod_enddate INTO v_fiscalcalendaryear_endyear, v_fiscalcalendarperiod_periodid, v_fiscalcalendarperiod_enddate FROM dbo.sfcfiscalperiods_noaotWHERE dataarea = v_dataareaid AND fiscalcalendarperiod_startdate = dateadd (mm, datediff (mm, 0, getdate ()) - 1, 0);-- CursorSELECT inventdim.inventsiteid, inventdim.inventlocationid, inventdim.wmslocationid, inventdim.inventcolorid, sfciteminventorytransactionsdaily.itemid, sum (sfciteminventorytransactionsdaily.unposted_physical_qty) unposted_physical_qty, sum (sfciteminventorytransactionsdaily.unposted_physical_amt) unposted_physical_amt, sum (sfciteminventorytransactionsdaily.posted_physical_qty) posted_physical_qty, sum (sfciteminventorytransactionsdaily.posted_physical_amt) posted_physical_amt, sum (sfciteminventorytransactionsdaily.total_physical_qty) total_physical_qty, sum (sfciteminventorytransactionsdaily.total_physical_amt) total_physical_amt FROM dbo.sfciteminventorytransactionsdaily_noaot sfciteminventorytransactionsdaily JOIN dbo.inventdim ON (sfciteminventorytransactionsdaily.dataareaid = inventdim.dataareaid AND sfciteminventorytransactionsdaily.inventdimid = inventdim.inventdimid)WHERE sfciteminventorytransactionsdaily.dataareaid = v_dataareaid AND sfciteminventorytransactionsdaily.datephysical <= v_fiscalcalendarperiod_enddateGROUP BY inventdim.dataareaid, inventdim.inventsiteid, inventdim.inventlocationid, inventdim.wmslocationid, inventdim.inventcolorid, sfciteminventorytransactionsdaily.itemid;-- For Each RowUPDATE dbo.sfcdairyunitrunreport SET unpostedphysicalqty = cursor_row.unpostedphysicalqty, unpostedphysicalamount = cursor_row.unpostedphysicalamount, postedphysicalamount = cursor_row.postedphysicalamount, postedphysicalqty = cursor_row.postedphysicalqty, totalphysicalqty = cursor_row.totalphysicalqty, totalphysicalamount = cursor_row.totalphysicalamountWHERE dataareaid = v_dataareaid AND inventsiteid = cursor_row.inventsiteid AND inventlocationid = cursor_row.inventlocationid AND wmslocationid = cursor_row.wmslocationid AND inventcolorid = cursor_row.inventcolorid AND itemid = cursor_row.itemid AND fiscalyearend = v_fiscalcalendaryear_endyear AND fiscalperiod = v_fiscalcalendarperiod_periodid;-- Did you update something? If Not InsertIF SQL%ROWCOUNT = 0THEN-- Get a RecidINSERT INTO dbo.sfcdairyunitrunreport (dataareaid, recversion, recid, inventsiteid, inventlocationid, wmslocationid, inventcolorid, itemid, fiscalyearend, fiscalperiod, unpostedphysicalqty, postedphysicalqty, totalphysicalqty, unpostedphysicalamount, postedphysicalamount, totalphysicalamount)VALUES (v_dataareaid, v_recversion, v_recid, cursor_row.inventsiteid, cursor_row.inventlocationid, cursor_row.wmslocationid, cursor_row.inventcolorid, cursor_row.itemid, v_fiscalyearend, v_fiscalperiod, cursor_row.unpostedphysicalqty, cursor_row.postedphysicalqty, cursor_row.totalphysicalqty, cursor_row.unpostedphysicalamount, cursor_row.postedphysicalamount, cursor_row.totalphysicalamount);END IF; -- did you update?-- END LOOP FOR each Row--ENDOnce done I would then update the table that holds the next value to be used for the recid:update systemsequences set nextval = a.nextvalfrom(select max(s.recid)+1 as nextval from SFCDAIRYUNITRUNREPORT s) awhere tabid = '102891'GO

Viewing all articles
Browse latest Browse all 3145

Trending Articles