Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
justjohno
Starting Member
23 Posts |
Posted - 2007-09-12 : 13:18:58
|
I'm new to stored procedures and would like some help with something I'm trying to accomplish if it can be done. Below is a query that with a snippet of its result set. I would like the SP to do is pass variables with a start and end for the period. I hope this makes sense and any help provided would be greatly appreciated.ThanksPS. Hope this isnt too confusing :)SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED set nocount on SET LOCK_TIMEOUT 50000delete gposoftextractDECLARE Cursor_Accts CURSOR for (Select distinct a.actindx, a.year1 from gl10110 a join gl00100 b on a.actindx = b.actindx where b.pstngtyp = 0)DECLARE @AcctIdx intDECLARE @AcctYear smallintDECLARE @AcctPeriod smallintDECLARE @Counter intset @Counter = 1DECLARE @tbl_balance_accts TABLE (actindx int, Year1 smallint, Periodid smallint, perdblnc numeric(19,5) ) ----------------------------------------------- Create records for all balance sheet accounts-- and all periods---- OPEN Cursor_AcctsFETCH NEXT FROM Cursor_Accts INTO @AcctIdx, @AcctYearWHILE @@FETCH_STATUS = 0begin WHILE @Counter < 13 Begin INSERT INTO @tbl_balance_accts (Actindx, year1, periodid, perdblnc) Values (@AcctIdx, @AcctYear, @counter, 0) Set @Counter = @Counter + 1 End Set @Counter = 1 FETCH NEXT FROM Cursor_Accts INTO @AcctIdx, @AcctYearendCLOSE Cursor_AcctsDEALLOCATE Cursor_AcctsUPDATE @tbl_balance_accts set perdblnc = (SELECT sum(perdblnc) from gl10110 b where b.actindx = a.actindx and b.year1 = a.year1 and b.periodid <= a.periodid) from @tbl_balance_accts a, gl10110 b-------------------------------------- Insert P&L records into the table variableINSERT INTO @tbl_balance_accts SELECT a.actindx, a.year1, a.periodid, a.perdblnc from gl10110 a join gl00100 b on a.actindx = b.actindx where a.Perdblnc <> 0 and b.pstngtyp <> 0----------------------------------------- Select records from table variable to returninsert into GPOSOFTEXTRACT -- Change made to insert result set into GPOSOFTEXTRACT table - John Ortiz select a.actnumbr_1 as 'BizType', a.actnumbr_2 as 'GEO LOC', a.actnumbr_3 as 'NAT CLASS', a.actnumbr_4 as 'DEPT', a.actnumbr_5 as 'COST CTR', periodid, g.year1 ,case when periodid = 1 then cast (year1 - 1 as varchar (30))+ '.' + 'OCT' when periodid = 2 then cast (year1 - 1 as varchar (30))+ '.' + 'NOV' when periodid = 3 then cast (year1 as varchar (30))+ '.' + 'DEC' when periodid = 4 then cast (year1 as varchar (30))+ '.' + 'JAN' when periodid = 5 then cast (year1 as varchar (30))+ '.' + 'FEB' when periodid = 6 then cast (year1 as varchar (30))+ '.' + 'MAR' when periodid = 7 then cast (year1 as varchar (30))+ '.' + 'APR' when periodid = 8 then cast (year1 as varchar (30))+ '.' + 'MAY' when periodid = 9 then cast (year1 as varchar (30))+ '.' + 'JUN' when periodid = 10 then cast (year1 as varchar (30))+ '.' + 'JUL' when periodid = 11 then cast (year1 as varchar (30))+ '.' + 'AUG' when periodid = 12 then cast (year1 as varchar (30))+ '.' + 'SEP' when periodid = 13 then cast (year1 as varchar (30))+ '.' + 'YEARENDADJ' when periodid = 0 then cast (year1 - 1 as varchar (30))+ '.' + 'ROLLFORWARD' else cast (year1 - 1 as varchar (30))+ '.' + 'UNKOWN' END as 'FISCAL PERIOD' ,isnull(Perdblnc,0) as AMOUNT from @tbl_balance_accts gjoin gl00105 a on g.actindx = a.actindxjoin gl00100 d on g.actindx = d.actindxselect * from gposoftextractResult setACCTNUM_1 ACCTNUM_2 ACCTNUM_3 ACCTNUM_4 ACCTNUM_5 PERIOD YEAR PERIODID BALANCE--------- --------- --------- --------- --------- ------ ------ -------------------- ---------------------------------------1 CA03 90430 28 008 3 2007 2007.DEC 222.770001 CA03 90430 28 008 4 2007 2007.JAN 337.280001 CA03 90430 28 008 5 2007 2007.FEB 455.360001 CA03 90430 28 008 6 2007 2007.MAR 538.810001 CA03 90430 28 008 7 2007 2007.APR 613.960001 CA03 90430 28 008 8 2007 2007.MAY 706.870001 CA03 90430 28 008 9 2007 2007.JUN 827.210001 CA03 90430 28 008 10 2007 2007.JUL 958.040001 CA03 90430 28 008 11 2007 2007.AUG 1016.380001 CA03 90430 28 008 12 2007 2007.SEP 837.530001 CA05 90588 40 999 1 2007 2006.OCT 728.000001 CA05 90588 40 999 2 2007 2006.NOV 1174.250001 CA05 90588 40 999 3 2007 2007.DEC 1587.000001 CA05 90588 40 999 4 2007 2007.JAN 2206.250001 CA05 90588 40 999 5 2007 2007.FEB 2554.250001 CA05 90588 40 999 6 2007 2007.MAR 2937.50000John O |
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2007-09-12 : 20:42:41
|
Try getting rid of the cursor and writing it as a single statement.Suspect you want to pass in the two dates to an spcreate proc s_myproc@start datetime ,@end datetimeasNot sure where you want to use the dates in the proc though==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
justjohno
Starting Member
23 Posts |
Posted - 2007-09-13 : 04:42:14
|
NR, thanks for the advice. Let me try to organize this mess of a query and repost. Again, I'm new and learning.John O |
 |
|
|
|
|
|
|