Question : SQL insert into using select from another table but doing one line at a time...

I have a temporary table with a few hundred records that has been formated to match a destination table. I want to copy all records from the temporary table to the actual table. See code snippet. All fields required are available in the temporary table except a field called PROOrderId. This field value looks like bellow:
tf20090505_1
tf20090505_2
tf20090505_3
Notice the date and a number value at the end. Basically the number at the end is incremented for each new inserted record for that particular date. I have a udf that generates this Id.

GenerateFuturesPROOrderId(PORPortfolioId,PROOrderDate) That locates the highest
PROOrderId for the date and regenerates the new PROOrderId by adding 1 to the last part of the Id. This function works perfectly.


However when I use the method bellow and have more than one records for the same date all inserted records for the date get the same dupplicated PROOrderId e.g. tf20090505_1 for 05/05/2009 and  tf20090512_1 for 12/05/2009. I think this is because the update is done in batch hence the first record for the date has not yet been posted to be available when the next instance is met.

How can I make each individual record be commited before moving to the next line?

Thanks
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
INSERT INTO 
  dbo.ORDPROOrder
(
  PORPortfolioId,
  PORAccountId,
  PROOrderDate,
  PROOrderId,
  SECSecurityId,
  SECExtendedDetailId,
  BuySell,
  ORDOrderTypeId,
  Quantity,
  Price,
  Filled,
  AverageFillPrice
) 
SELECT 
  PORPortfolioId,
  PORAccountId,
  PROOrderDate,
  dbo.GenerateFuturesPROOrderId(PORPortfolioId,PROOrderDate), 
  SECSecurityId,
  SECExtendedDetailId,
  BuySell,
  ORDOrderTypeId,
  Quantity,
  Price,
  Filled,
  AverageFillPrice
  
FROM dbo.Temp_ORDPROOrder Where PROOrderDate = '20071130'
Open in New Window Select All

Answer : SQL insert into using select from another table but doing one line at a time...

if you need to increment the orderid for each record, you will need to use a cursor.  Ideally you would let the table create the ID, but then that ID would be unique on the table, rather than for a date, which doesn't sound like what you want.

I put in an example below.. I didn't actually test it.. so there might be a syntax error here and there.  Also.. I just guessed at the datatypes.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
DECLARE CURSOR @cCursor FOR
SELECT 
  PORPortfolioId,
  PORAccountId,
  PROOrderDate,
  dbo.GenerateFuturesPROOrderId(PORPortfolioId,PROOrderDate), 
  SECSecurityId,
  SECExtendedDetailId,
  BuySell,
  ORDOrderTypeId,
  Quantity,
  Price,
  Filled,
  AverageFillPrice
  
FROM dbo.Temp_ORDPROOrder Where PROOrderDate = '20071130'
 
DECLARE @PORPortfolioId int
DECLARE @PORAccountId int
DECLARE @PROOrderDate datetime
DECLARE @GenerateFuturesPROOrderId int
DECLARE @SECSecurityId int
DECLARE @SECExtendedDetailId int
DECLARE @BuySell varchar(20)
DECLARE @ORDOrderTypeId int
DECLARE @Quantity int
DECLARE @Price money
DECLARE @Filled bit
DECLARE @AverageFillPrice money
 
FETCH NEXT FROM @cCursor INTO 
		@PORPortfolioId, @PORAccountId, @PROOrderDate, @GenerateFuturesPROOrderId, 
		@SECSecurityId, @SECExtendedDetailId, @BuySell, @ORDOrderTypeId,
		@Quantity, @Price, @Filled, @AverageFillPrice
 
WHILE @@FETCH_STATUS = 0 
BEGIN
	INSERT INTO 
	  dbo.ORDPROOrder
	(
	  @PORPortfolioId,
	  @PORAccountId,
	  @PROOrderDate,
	  @GenerateFuturesPROOrderId,
	  @SECSecurityId,
	  @SECExtendedDetailId,
	  @BuySell,
	  @ORDOrderTypeId,
	  @Quantity,
	  @Price,
	  @Filled,
	  @AverageFillPrice
	)
	FETCH NEXT FROM @cCursor INTO 
			@PORPortfolioId, @PORAccountId, @PROOrderDate, @GenerateFuturesPROOrderId, 
			@SECSecurityId, @SECExtendedDetailId, @BuySell, @ORDOrderTypeId,
			@Quantity, @Price, @Filled, @AverageFillPrice 
END
CLOSE @cCursor
DEALLOCATE @cCursor
Open in New Window Select All
Random Solutions  
 
programming4us programming4us