Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

More cursor help!!  :)

Posted on 2009-07-13
4
Medium Priority
?
224 Views
Last Modified: 2012-05-07
I have a cursor that creates a detail records for invoice.  The primary key is based on the source code, group num, and transaction num.  I am incrementing my transaction number but it is being overwritten on the next Fetch and being set back to 1.  So if the second record is the same grp number and source code the transnumber must be different.  How can I increment the trans_num without being overwritten...Hope this makes sense.  I have included the part of my code I'm having an issue with.  Thanks so much for any assistance.
SET @trans_num = 1
SET @encumb_gl_flag = 'G'
SET @encumb_gl_trans_sts = 'C'
SET @offset_flag = 'P'
SET @subsid_trans_sts = 'C'
 
DECLARE curDetail CURSOR FOR 
SELECT distinct @source, ih.grp_num, @trans_num, @tran_dte, @amt, cd.amt, cd.tran_desc,
cd.gl, @project_cde, @encumb_gl_flag, @encumb_gl_trans_sts, ch.id_num,
@subsid_cde, @inv_num, @offset_flag, @subsid_trans_sts, @user, @job, @tran_dte
FROM
ccheader ch left outer join invoice_header ih 
on ch.id_num = ih.id_num
left outer join ccdetail cd ON cd.id_num = ch.id_num
WHERE ih.invoice_num = @inv_num
order by ih.grp_num
 
OPEN curDetail
FETCH NEXT FROM curDetail
INTO
@source, @grp_num, @trans_num, @tran_dte, @amt, @amt_det, @tran_desc, @gl,
@project_cde, @encumb_gl_flag, @encumb_gl_trans_sts, @id_num, @subsid_cde,
@inv_num, @offset_flag, @subsid_trans_sts, @user, @job, @tran_dte
 
WHILE @@FETCH_STATUS = 0
 
BEGIN
 
print @source + ' ' + cast(@grp_num as varchar) + ' '+ 
cast(@amt_det as varchar)+ ' '+ cast(@trans_num as varchar)+ ' ' + @gl
 
INSERT INTO trans_hist
(source_cde, group_num, trans_key_line_num, trans_dte, trans_amt, trans_desc,
acct_cde, project_code, encumb_gl_flag, encumb_gl_trans_st, ap_sbs_id_num, ap_sbs_cde_subsid,
invoice_num, subsid_trans_sts, user_name, job_name, job_time)
SELECT
@source, @grp_num, @trans_num, @tran_dte, @amt_det, @tran_desc, @gl,
@project_cde, @encumb_gl_flag, @encumb_gl_trans_sts, @id_num, @subsid_cde,
@inv_num,  @subsid_trans_sts, @user, @job, @tran_dte
 
SET @trans_num = @trans_num + 1
 
print @source + ' ' + cast(@grp_num as char) + ' '+ 
cast(-(@amt_det)as char)+ ' '+ cast(@trans_num as char)+ ' ' + @gl
 
INSERT INTO trans_hist
(source_cde, group_num, trans_key_line_num, trans_dte, trans_amt, trans_desc,
acct_cde, project_code, encumb_gl_flag, encumb_gl_trans_st, ap_sbs_id_num, ap_sbs_cde_subsid,
offset_flag, invoice_num, subsid_trans_sts, user_name, job_name, job_time)
SELECT
@source, @grp_num, @trans_num, @tran_dte, -(@amt_det), @tran_desc, @gl,
@project_cde, @encumb_gl_flag, @encumb_gl_trans_sts, @id_num, @subsid_cde, @offset_flag,
@inv_num,  @subsid_trans_sts, @user, @job, @tran_dte
 
 
SET @trans_num = @trans_num + 1
 
print cast(@trans_num as char) + ' trans num after first run'
 
FETCH NEXT FROM curDetail 
INTO 
@source, @grp_num, @trans_num, @tran_dte, @amt, @amt_det, @tran_desc, @gl,
@project_cde, @encumb_gl_flag, @encumb_gl_trans_sts, @id_num, @subsid_cde,
@inv_num, @offset_flag, @subsid_trans_sts, @user, @job, @tran_dte

Open in new window

0
Comment
Question by:jasonbrandt3
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 22

Expert Comment

by:dportas
ID: 24842733
Cursors are rarely a good idea for this kind of thing. You should be able to get the same result using two INSERT statements, no cursor required. For example:

INSERT INTO trans_hist (...)
SELECT ...
FROM ccheader ch
LEFT OUTER JOIN invoice_header ih
ON ch.id_num = ih.id_num
LEFT OUTER JOIN ccdetail cd
ON cd.id_num = ch.id_num
WHERE ih.invoice_num = @inv_num ;

If the purpose of the cursor is to generate a new number for each row then use the ROW_NUMBER() function instead.

Note that the "WHERE ih.invoice_num = @inv_num" condition makes the OUTER join into an INNER join, which may or may not be what you intended.
0
 

Author Comment

by:jasonbrandt3
ID: 24843110
So you are saying insert all records at once.  How could I increment the transaction number for each row?
0
 
LVL 22

Accepted Solution

by:
dportas earned 2000 total points
ID: 24843456
0
 

Author Closing Comment

by:jasonbrandt3
ID: 31602935
I will give it a try, these seems to be the more efficient way.  Appreciate the help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question