| Author |
Topic |
|
trimavis
Starting Member
1 Post |
Posted - 2006-04-20 : 20:55:48
|
| Hello guys... Maybe you can help me on my dilemma in using this cursor that I created.. It takes 47 - 60 seconds to complete the process.. I have included the source here:******************************************************************************SET QUOTED_IDENTIFIER ON GOSET ANSI_NULLS ON GO--drop procedure QueryAffectedInvoices 'PTG0600076',8ALTER procedure QueryAffectedInvoices @InvoiceNumber varchar(50), @CustID bigint asbegin--use NIMPEXSET NOCOUNT ON--declare @InvoiceNumber varchar(50)--declare @CustID bigint--set @InvoiceNumber = 'PJG0400137'--set @CustID = 19select ic.ICID,ic.ICID ICIDRef,ic.CustID,ic.ItemID,ic.[Effective Date],ic.[Fiscal Year],ic.Updated,ic.Tag, d.pklinvd_ID,d.pklinvIDRef,d.[PO Number],(select [Part Code]+ ' ('+convert(varchar,d.[Part Number])+')' from item where i_ID=d.[Part Number])PartNumber,d.Quantity,d.[Total Amount],t.[Invoice Number],t.ETD [Date Prepared],t.[Commercialized Date] into #tempQueryAffected from [item change] ic inner join (pkl_inv_details d inner join pkl_inv_table t on d.pklinvIDRef=t.pklinvID) on d.[Part Number]=ic.ItemIDwhere (ic.[Effective Date] <= t.ETD) and (t.[Invoice Number]=@InvoiceNumber) and (d.Active=1) and (ic.CustID=@CustID) and (d.Tag='check')if (select count(*) from #tempQueryAffected) = 0 return 0declare @numrows bigintdeclare @counter bigintdeclare @QCount bigint--*************************************declare @ICID bigintdeclare @ICIDRef bigintdeclare @CustIDRef bigintdeclare @ItemID bigintdeclare @EffectiveDate datetimedeclare @FiscalYear bigintdeclare @Updated bitdeclare @Tag varchar(255)declare @pklinvd_ID bigintdeclare @pklinvIDRef bigintdeclare @PONumber varchar(50)declare @PartNumber varchar(50)declare @Quantity bigintdeclare @TotalAmount floatdeclare @InvoiceRef varchar(50)declare @DatePrepared datetimedeclare @CommercializedDate datetimedeclare @IsUpdated bitdeclare @DateModified datetime--*************************************-- SELECT * FROM [AFFECTED INVOICES]exec CreateQueryAffectedCursorselect @numrows = count(ICID) from #tempQueryAffectedset @counter = 1open QueryAffectedfetch first from QueryAffected into @ICID,@ICIDRef,@CustIDRef,@ItemID,@EffectiveDate,@FiscalYear,@Updated,@Tag, @pklinvd_ID,@pklinvIDRef,@PONumber,@PartNumber,@Quantity,@TotalAmount, @InvoiceRef,@DatePrepared,@CommercializedDate--check first the affected invoices table if there exist, use QCount :)if (select count(pklinvd_id) from [affected invoices] ai where ai.ICIDRef=@ICID and ai.[Invoice Number]=@InvoiceRef) > 0 begin select @DateModified = [Date Modified] from [Affected Invoices] ai where ai.ICIDRef=@ICID and ai.[Invoice Number]=@InvoiceRef select @IsUpdated = Updated from [Affected Invoices] ai where ai.ICIDRef=@ICID and ai.[Invoice Number]=@InvoiceRef insert into [Affected Invoices] values (@pklinvd_ID,@pklinvIDRef,@InvoiceRef,@PONumber,@PartNumber,@Quantity,@TotalAmount, @DatePrepared,@ICIDRef,@IsUpdated,@Tag,@DateModified,@CommercializedDate) endelse begin insert into [Affected Invoices] values (@pklinvd_ID,@pklinvIDRef,@InvoiceRef,@PONumber,@PartNumber,@Quantity,@TotalAmount, @DatePrepared,@ICIDRef,0,@Tag,NULL,@CommercializedDate) endwhile (@@fetch_status=0) and (@counter<@numrows)begin fetch next from QueryAffected into @ICID,@ICIDRef,@CustIDRef,@ItemID,@EffectiveDate,@FiscalYear,@Updated,@Tag, @pklinvd_ID,@pklinvIDRef,@PONumber,@PartNumber,@Quantity,@TotalAmount, @InvoiceRef,@DatePrepared,@CommercializedDate--check first the affected invoices table if there exist, use QCount :) if (select count(pklinvd_id) from [affected invoices] ai where ai.ICIDRef=@ICID and ai.[Invoice Number]=@InvoiceRef) > 0 begin select @DateModified = [Date Modified] from [Affected Invoices] ai where ai.ICIDRef=@ICID and ai.[Invoice Number]=@InvoiceRef select @IsUpdated = Updated from [Affected Invoices] ai where ai.ICIDRef=@ICID and ai.[Invoice Number]=@InvoiceRef insert into [Affected Invoices] values (@pklinvd_ID,@pklinvIDRef,@InvoiceRef,@PONumber,@PartNumber,@Quantity,@TotalAmount, @DatePrepared,@ICIDRef,@IsUpdated,@Tag,@DateModified,@CommercializedDate) end else begin insert into [Affected Invoices] values (@pklinvd_ID,@pklinvIDRef,@InvoiceRef,@PONumber,@PartNumber,@Quantity,@TotalAmount, @DatePrepared,@ICIDRef,0,@Tag,NULL,@CommercializedDate) end set @counter = @counter+1enddrop table #tempQueryAffected close QueryAffecteddeallocate QueryAffectedendGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GO******************************************************************************hope someone could help... i'm also trying to figure this out without using any cursor... |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-04-20 : 21:05:11
|
Don't use cursor. Rewrite your query using set based operation KH |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-04-21 : 02:15:58
|
| Tell us what you are trying to do. If you want to copy data from one table to other, thenInsert into table1(columns)select columns from table2MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|