My name is Shenn Sellers, and I'm a "Cursor-olic". I've been using cursors for far longer than I care to admit because of their simplicity. I was able to get away with this when my database was Oracle, but now am using MS SQL Server and running into issues. (i.e. I can't do an insert within an active cursor) The main thing I do is call a cursor, loop through the rows, use the fetched data to do variable modification, and then insert/update into another table.
Can someone correct my ways and help me to see the better and best ways of doing this without a cursor? I have been reviewing set-based INSERT/SELECT statements, but don't see how I can do all my variable modification using this approach.
My cursor filled code is below.
declare raw cursor for select department_id, invoice_amt, acct_string from printing_raw_data where invoice_date >= '2014-11-27' and invoice_date <= '2014-12-31' order by invoice_date, invoice_num; open raw; fetch raw into :ll_dept_id, :ld_trans_amt, :ls_acct_string; ll_line_num = 1 li_fiscal_year = 2015 li_fiscal_month = 12 ls_month = f_get_month_abbrev(li_fiscal_month) do while(SQLCA.SQLCode = 0) ls_dept_id_full = string(ll_dept_id) + Left(ls_acct_string, 5) ll_fund_code = long(Mid(ls_acct_string, 6, 5)) ll_acctnum = long(Mid(ls_acct_string, 11, 6)) ls_program_code = Trim(Mid(ls_acct_string, 17, 5)) ls_class_fld = Trim(Mid(ls_acct_string, 22, 5)) ls_project_id = Trim(Mid(ls_acct_string, 27, 20)) //For each row of data, 4 Journal Lines are created in the table for li_line = 1 to 4 li_mod = Mod(ll_line_num, 4) //Customer Charge Line if (li_mod = 1) then if (Sign(ld_trans_amt) = 1) then ls_line_desc = "Print Printing Co Depts" li_line_type = 1 elseif (Sign(ld_trans_amt) = -1) then ls_line_desc = "C. M. Print Services" li_line_type = 2 end if ls_line_desc += " " + ls_month insert into journal_entry(fiscal_year, fiscal_month, line_num, department_id_full, acct_num, fund_code, program_code, class_fld, project_id, trans_amt, line_desc, line_type) values(:li_fiscal_year, :li_fiscal_month, :ll_line_num, :ls_dept_id_full, :ll_acctnum, :ll_fund_code, :ls_program_code, :ls_class_fld, :ls_project_id, :ld_trans_amt, :ls_line_desc, :li_line_type); ll_line_num++ //Customer Cash Line elseif (li_mod = 2) then ls_line_desc = "Credit Cash " + ls_month ld_neg_amt = ld_trans_amt * -1 li_line_type = 3 insert into journal_entry(fiscal_year, fiscal_month, line_num, department_id_full, acct_num, fund_code, trans_amt, line_desc, line_type) values(:li_fiscal_year, :li_fiscal_month, :ll_line_num, :ls_dept_id_full, 101100, :ll_fund_code, :ld_neg_amt, :ls_line_desc, :li_line_type); ll_line_num++ //Revenue Line for Printing elseif (li_mod = 3) then ls_line_desc = "Sales - Printing Svcs " + ls_month ld_neg_amt = ld_trans_amt * -1 li_line_type = 4 insert into journal_entry(fiscal_year, fiscal_month, line_num, department_id_full, acct_num, fund_code, trans_amt, line_desc, line_type) values(:li_fiscal_year, :li_fiscal_month, :ll_line_num, "7300300000", 777670, 45600, :ld_neg_amt, :ls_line_desc, :li_line_type); ll_line_num++ //Cash Line for Printing elseif (li_mod = 0) then ls_line_desc = "Print Printing Co Depts ~~ " + ls_month li_line_type = 5 insert into journal_entry(fiscal_year, fiscal_month, line_num, department_id_full, acct_num, fund_code, trans_amt, line_desc, line_type) values(:li_fiscal_year, :li_fiscal_month, :ll_line_num, "7300300000", 101100, 45600, :ld_trans_amt, :ls_line_desc, :li_line_type); ll_line_num++ end if next fetch raw into :ll_dept_id, :ld_trans_amt, :ls_acct_string; loop close raw;