sql server - SQL stored procedure inserting duplicate OrderNumber -
I searched the Internet for the day, no effort, maybe I do not want to ask properly.
I have a SQL table like this:
to create table items (id int identity (1,1), order number varchar (7), item name varchar ( Then I have to create a stored procedure to put items on demand, new ORDERNUMBER :
Process spx_insertItems @insertNewOrderNr bit, @orderNumber varchar (7), @item name varchar (255), @n count on start count as int; If the announcement of (@insertNewOrderNr = 1) declaration @newNr = start (dbo.fun_getNewOrderNr Selection ()), enter values (@newNr, @itemName, @count) in item (ORDERNUMBER, ITEMNAME calculation) @newNr in another item Enter the starting start date (ORDERNUMBER, ITEMNAME, count) values (@orderNumber, @itemName, @count), SCOPE_IDENTITY () end-end new ORDERNUMBER in the end A user-defined function is returning to the code>: create function dbo.fun_getNewOrderNr () Return as varchar (7) / * This function works well * / @ Output varchar (7) Setting @currentMaxNr varchar (7) set @currentMaxNr = (isnull ((select max (ORDERNUMBER from item), 'some_default_value_here') / * value is @currentMaxNr '01 / 2014 ', the argument here is that Comes to @ newValue = '02 / 2014 'and lets set @output, so: * / set @output = @newValue return @output end
Items in the item can be inserted that are not related to any OrderNumber .
Whether an item should be new OrderNumber , this process is called with @ insertNewOrderNr = 1 , the new order number that uses That can be done to include the next item with the OrderNumber , while @ insertNewOrderNr = 0 .
Sometimes it happens that there are 2 requests for @inertNewOrderNr and there is a problem - items , which are different OrderNumbers should be consistent with the same ORDERNUMBER .
I tried to use the transaction without success.
The table structure can not be modified by me.
What will be the correct way to ensure that, when making the request together, the same newOrderNumber will not be used? I have lived here for a long time so far. Please, help
Until you use MAX (OrderNumber) you will have that problem.
You can consider using sequences:
Create Sequence
Make Siluex dbo.OrderNumbers Cache not increase in 1 with INST 1; Go Sequence by increase in 2014 as dbo.OrderNumberYear with no cafe increased by 1; Choose the next price for Dbo.OrderNumberYear; - Important, run this once after creation, an initial time should be returned to work correctly this year's value. Insert code
Announcement @ Order number job INT = (Select Convert (INT, current_value) from sys.sequences WHERE name = 'OrderNumberYear'); If (@ Order No. Year & lt; YEAR (GETDATE ())) Start @ orderNumberYear = dbo.OrderNumberYear for next price; Restart with optional sequence dbo.OrderNumbers 1; END if (@order year year! = YEAR (GETDATE ())) Rizer (N'Order year sequence is out of sync. ', 16, 1); DECLARE @newnr varchar (15) = CONCAT (FORMAT (next value for dbo.OrderNumbers, '00 / ',' en-US '), @orderNumberYear); Include in item (order number, item name, calculation) value (@newer, @imetame, @ number) SELECT @newnr
Comments
Post a Comment