Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.

I have an issue in below SP.
I have created DOno in Sequence (MS2012)
If execute SP, Dono for all record shows same.

Pls advise me

Maideen

Here is SP

SQL
ALTER Procedure [dbo].[W_usp_SO_GENERATION]
	@SoNo nvarchar(20),
	@SoDate datetime,
	@STDB nvarchar(5),
	@Products nvarchar(50),
	@issueNo nvarchar(10),
	@User nvarchar(20),
	@terminal nvarchar(50)

As
Begin

Declare @DoNo varchar(10)
SET @DoNo = next value for DONo
set @DoNo = 'DO-' + right('000000'+convert(varchar,@DoNo),6) 

INSERT INTO [dbo].[SOMaster_TEMP] (SoNo, SODate, SVCode, SVName,  MVCode, DistCode,RouteCode,Area,Edition, SeqNo,
								   Qty,Rate, STDB, BSTD, BEND, BLOS,Products, IssueNo, CreatedBy, CreatedOn, Terminal, 
								   DONo,GSTP,Type)

SELECT      @sono, @SoDate,  SVCode,  SVName,  MVCode,  DistCode, RouteCode, Area, Edition,  SeqNo,  '0', Rate, @STDB, '0', '0', '1', @Products, @issueNo, @User, @SoDate, 
	@terminal,  @DoNo, '6', type  FROM      [dbo].[vwRDMS]
Posted
Comments
_Asif_ 14-Jul-15 2:33am    
SET @DoNo = next value for DONo -- Please let us know how you getting next value for DONo?

1 solution

Based on the code you provided, value for @DoNo variable should be different on each call.

For seeing the sequence behaviour you can use PRINT statement to show the content of you variable, like:
SQL
Declare @DoNo varchar(10)
SET @DoNo = next value for DoNo
PRINT @DoNo
set @DoNo = 'DO-' + right('000000'+convert(varchar,@DoNo),6) 
PRINT @DoNo


As a side note, I would suggest storing the value from the sequence as-is. If you need to format it when displayed, either do the formatting in SELECT statements or use a computed column.

EDIT:

If the select used in the insert return several rows then the same value from the variable is used for all of the rows. Because of this the value need to be generated on row basis.

So try modifying the procedure as follows

SQL
ALTER Procedure [dbo].[W_usp_SO_GENERATION]
	@SoNo nvarchar(20),
	@SoDate datetime,
	@STDB nvarchar(5),
	@Products nvarchar(50),
	@issueNo nvarchar(10),
	@User nvarchar(20),
	@terminal nvarchar(50)
 
AS
BEGIN
 
INSERT INTO [dbo].[SOMaster_TEMP] 
   (SoNo, SODate, SVCode, SVName,  MVCode, DistCode,
    RouteCode,Area,Edition, SeqNo,Qty,Rate, STDB, BSTD, BEND, BLOS,
    Products, IssueNo, CreatedBy, CreatedOn, Terminal, DONo,GSTP,Type)
SELECT @sono, @SoDate,  SVCode,  SVName,  MVCode,  DistCode, RouteCode, 
       Area, Edition,  SeqNo,  '0', Rate, @STDB, '0', '0', '1', 
       @Products, @issueNo, @User, @SoDate, @terminal,  
       'DO-' + right('000000'+convert(varchar,(next value for DoNo)),6), 
       '6', type  
FROM [dbo].[vwRDMS]
 
Share this answer
 
v3
Comments
Maideen Abdul Kader 14-Jul-15 0:41am    
Thank for your reply.

I explain my issue. Pls advice me

I have the view "vwRDMS". It is basic data of customer nearly 500 customer. Every month user need to generate DO and Invoice based on Standing order. Once execute SP, It is inserted the data into temp table "SOMaster_TEMP". It is work fine. But DO No can not be generated.

I have setup DONo In Sequences. It capture the same DO number for all customer. Here is I am facing problem.

Pls advice me.

Thank you

Maideen
Wendelius 14-Jul-15 1:08am    
Have you tried printing the value? Does it change on each call?
Maideen Abdul Kader 14-Jul-15 3:13am    
Yes I did. But show the Only one number... Because there are the more than 500 customer... need more than 500 DO No.
Wendelius 14-Jul-15 3:22am    
I see so the select used in the INSERT returns 500 rows. Have a look at the modified answer.
Maideen Abdul Kader 14-Jul-15 4:50am    
Thank you So Much Mika Solved

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900