sql - How to insert unique value into non-identity field -
I have an insert to try a primary key field in which he and other fields in a set table (this field 1 Call it) This is unique (this is a unique obstacle in other specific fields that prevents my inclusion). Field 1 is not an identity field, so it does not automatically number. Unfortunately I can not change the table. The existing insertion code is made to increase and contains looping / cursor in it. Select MAX (Field 1) + 1
Anything like this, without any looping / cursor to insert anyway? This area is meant for me, but there already are having 500,000+ records using their silly numbering plan, so I should respect that
It's simple, but (ReceiptNumber Regions I want to insert an) .:
set lane XACT_ABORT-announced transaction start TransMain @nvErrMsg nvarchar (4000) --Insert inventory receipts Avanti_InventoryReceipts (ReceiptNumber, ItemNumber, ReceiptDate, ORDERNUMBER, JobNumber, Supplier, LineNumber, MultiLineNumber, insert in [position], QtyOrdered, QtyRecei ,,,,,,,,,,,,,,,,,, ConsignmentUnitPrice, select RecFromProduction, QtyCommitted) (select Max (ReceiptNumber) + 1 from Avanti_inventoryReceipts), CR.ItemNumber, convert (char (8), GETDA TE (), 112), PONum, 'FL-INV', PH.POVendor , 0, 0, 'o', CR.QtyOrdered, QtyReceivedToday, QtyReceivedToday, case @closePO when 'n' then the case when (QtyOrdered - QtyReceivedToday) & LT; 0 Otherwise 0 (QtyOrdered - QtyReceivedToday) end when Y is 0 else 0 end, PD.TransCost * QtyReceivedToday, IH.PriceWholeSale, IH.CostLast, QtyReceivedToday, 0, ', PODetailDescription,' ',' ',' ' ,, (four (8), GETDATE (), 112) convert '' '', @employeeCode, '', 'FLEXO', '', 0, 'Flexo material', 0, 0, 0, '', FI_CurrentReceiptData included CR.PONum = PH.PONumber left on CR.ItemNumber = IH.ItemNumber CR Join CR.PONum = PD.PONumber left Avanti_PODetails PD 0 left are Avanti_InventoryHeader IH Avanti_POHeader Join @@ error if the & lt pH; & Gt; Starting 0 @nvErrMsg = 'Error [InventoryReceipts] Log in - [details' + master..sysmessages] where [error] = @@ error RAISERROR (@nvErrMsg, 16, 1) goto Err_ termination committed transactions TransMain Goto Exit_ Err_ rollback transaction TransMain Exit_: SET XACT_ABORT off
you can do this:
insert mytable (fields 1, field2, ...) into values (select Max (field 1) + 1 from mytable), 'value2', ...);
Comments
Post a Comment