Showing posts with label cursor. Show all posts
Showing posts with label cursor. Show all posts

Monday, March 26, 2012

Fetch within a fetch

Is it possible to have fetch within a fetch? I am getting this error message "A cursor with the name 'crImgGrp' does not exist." So i separate the process into two stored procedures?

CREATE PROCEDURE TrigSendPreNewIMAlertP2
@.REID int

AS

Declare @.RRID int
Declare @.ITID int

Declare @.intIMEmail varchar(300)

Declare crReqRec cursor for
select RRID from RequestRecords where REID = @.REID and RRSTatus = 'IA' and APID is not null
open crReqRec
fetch next from crReqRec
into
@.RRID

Declare crImpGrp cursor for
select ITID from RequestRecords where RRID = @.RRID
open crImpGrp
fetch next from crImgGrp
into
@.ITID
while @.@.fetch_status = 0

EXEC TrigSendNewIMAlertP2 @.ITID

FETCH NEXT FROM crImpGrp
into
@.ITID

close crImpGrp
deallocate crImpGrp

while @.@.fetch_status = 0

FETCH NEXT FROM crReqRec
into
@.RRID

close crReqRec
deallocate crReqRec
GO... I'd re-think a different solution than what you want to do.

fetch results of a cursor to a temporary DB for manipulation

Hi all

I want to put the fetch results of a cursor to a temporary DB for manipulation, Im selecting all columns from the table in the cursor and the number of total columns is unknow.

Please guide me on how this could be done...

Thanks in advance

Regards

BennyGet rid of the cursor and use a select into statement with the same query to create a table in the temp database.

If you want to perform row by row oprerations on the resultset use the identity function to create an identity on it then you can loop through that without need for a cursor.

Fetch cursor help

Let's say i have 5 unique RRID's, column APID and ITID

RRID - APID - ITID
1 13 700
2 13 700
3 13 700
4 14 700
5 15 700

If I run the stored procedure below, I get the results above however, I want my result to be

RRID - APID - ITID
1 13 700
2 13 700
3 13 700
4 14 701
5 15 702

I want my cursor to loop at the same APID then assign one ITID then move to the next APID and so on...

Any help is highly appreciated...


SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

ALTER PROCEDURE InsNewEmployeeImpTaskP2
@.REID int,
@.LOID int,
@.RetValintoutput

AS

Declare @.RRID int
Declare @.APID int
Declare @.intREID varchar(20)
Declare @.intIMID varchar(20)

Declare crReqRec cursor for
select RRID from RequestRecords where REID = @.REID and RRSTatus = 'AC' and APID is not null
open crReqRec
fetch next from crReqRec
into
@.RRID

set @.APID = (select APID from RequestRecords where REID = @.REID and RRID = @.RRID)

set @.intIMID = (SELECT ImplementationGroup.IMID
FROM ImplementationGroup_Location INNER JOIN
ImplementationGroup ON ImplementationGroup_Location.IMID = ImplementationGroup.IMID INNER JOIN
Applications_ImplementationGroup ON ImplementationGroup.IMID = Applications_ImplementationGroup.IMID where APID = @.APID and ImplementationGroup_Location.LOID = @.LOID )

insert into ImplementationTasks
(
IMID,
ITStatus,
ITStatusDate
)
VALUES
(
@.intIMID,
'2',
GetDate()
)
SET @.RetVal = @.@.Identity
while @.@.fetch_status = 0
Begin

Update RequestRecords
set ITID = @.RETVal, RRStatus = 'IA'
where REID = @.REID and RRID = @.RRID

FETCH NEXT FROM crReqRec
into
@.RRID
end

close crReqRec
deallocate crReqRec

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

This is the newer version but still getting the same results. PLEASE HELP...

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
ALTER PROCEDURE InsNewEmployeeImpTaskP2
@.REID int,
@.LOID int,
@.RetValintoutput

AS

Declare @.RRID int
Declare @.APID int
Declare @.APID2 int
Declare @.FS1 int
Declare @.FS2 int
Declare @.intREID varchar(20)
Declare @.intIMID varchar(20)

Declare crReqRec cursor local for
select RRID from RequestRecords where REID = @.REID and RRSTatus = 'AC' and APID is not null

open crReqRec
fetch next from crReqRec into @.RRID
set @.FS1 = @.@.fetch_status

Declare crAPID cursor local for
select APID from RequestRecords where REID = @.REID and RRID = @.RRID
open crAPID
fetch next from crAPID into @.APID2
set @.FS2 = @.@.fetch_status
set @.APID2 = (select APID from RequestRecords where APID = @.APID)
set @.intIMID = (SELECT ImplementationGroup.IMID FROM ImplementationGroup_Location INNER JOIN
ImplementationGroup ON ImplementationGroup_Location.IMID = ImplementationGroup.IMID INNER JOIN
Applications_ImplementationGroup ON ImplementationGroup.IMID = Applications_ImplementationGroup.IMID where APID = @.APID2 and ImplementationGroup_Location.LOID = @.LOID )

insert into ImplementationTasks
(IMID, ITStatus,ITStatusDate) VALUES (@.intIMID,'2',GetDate())
SET @.RetVal = @.@.Identity

while @.FS2 = 0
while @.FS1 = 0

Begin
Update RequestRecords
set ITID = @.RETVal, RRStatus = 'IA'
where REID = @.REID and RRID = @.RRID

fetch next from crAPID into @.APID2
FETCH NEXT FROM crReqRec into @.RRID
end

close crReqRec
deallocate crReqRec
close crAPID
deallocate crAPID

--EXEC TrigRetReqRecIDP2 @.REID
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GOsql

Wednesday, March 7, 2012

FAST_FORWARD Cursor

Hi Experts,
(1) Will there be any considerable performance gain in
using the default cursor type (FORWARD_ONLY) over
FAST_FORWARD cursors. Cursors are used in my application
as READ ONLY source of data.
(2) Will the use of GOTO inside a SP degrade performance?
Kindly suggest.
TIA,
Hari
On Wed, 4 Aug 2004 05:02:01 -0700, Hari wrote:

>Hi Experts,
>(1) Will there be any considerable performance gain in
>using the default cursor type (FORWARD_ONLY) over
>FAST_FORWARD cursors. Cursors are used in my application
>as READ ONLY source of data.
>(2) Will the use of GOTO inside a SP degrade performance?
>Kindly suggest.
>TIA,
>Hari
Hi Hari,
(1) No. FORWARD_ONLY is implied by FAST_FORWARD. There will be a
performance gain in using a set-based solution over a cursor based
solution, if possible.
(2) No.
Best, Hugo
(Remove _NO_ and _SPAM_ to get my e-mail address)
|||Hugo,
Could you please tell me what is the SET-based solution?
Thanks,
Hari

>--Original Message--
>On Wed, 4 Aug 2004 05:02:01 -0700, Hari wrote:
>
>Hi Hari,
>(1) No. FORWARD_ONLY is implied by FAST_FORWARD. There
will be a
>performance gain in using a set-based solution over a
cursor based
>solution, if possible.
>(2) No.
>Best, Hugo
>--
>(Remove _NO_ and _SPAM_ to get my e-mail address)
>.
>
|||On Wed, 4 Aug 2004 05:31:30 -0700, Hari wrote:

>Hugo,
>Could you please tell me what is the SET-based solution?
>Thanks,
>Hari
Hi Hari,
Without knowing what your current cursor-based solution actually does?
No.
But if you post DDL (create table statements, including all constraints
but excluding irrelevant columns), sample data (as INSERT statements),
expected output, your current cursor-based SQL and a description of the
business problem you're trying to solve, I could give it a shot.
Best, Hugo
(Remove _NO_ and _SPAM_ to get my e-mail address)

FAST_FORWARD Cursor

Hi Experts,
(1) Will there be any considerable performance gain in
using the default cursor type (FORWARD_ONLY) over
FAST_FORWARD cursors. Cursors are used in my application
as READ ONLY source of data.
(2) Will the use of GOTO inside a SP degrade performance?
Kindly suggest.
TIA,
HariOn Wed, 4 Aug 2004 05:02:01 -0700, Hari wrote:
>Hi Experts,
>(1) Will there be any considerable performance gain in
>using the default cursor type (FORWARD_ONLY) over
>FAST_FORWARD cursors. Cursors are used in my application
>as READ ONLY source of data.
>(2) Will the use of GOTO inside a SP degrade performance?
>Kindly suggest.
>TIA,
>Hari
Hi Hari,
(1) No. FORWARD_ONLY is implied by FAST_FORWARD. There will be a
performance gain in using a set-based solution over a cursor based
solution, if possible.
(2) No.
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||Hugo,
Could you please tell me what is the SET-based solution?
Thanks,
Hari
>--Original Message--
>On Wed, 4 Aug 2004 05:02:01 -0700, Hari wrote:
>>Hi Experts,
>>(1) Will there be any considerable performance gain in
>>using the default cursor type (FORWARD_ONLY) over
>>FAST_FORWARD cursors. Cursors are used in my application
>>as READ ONLY source of data.
>>(2) Will the use of GOTO inside a SP degrade performance?
>>Kindly suggest.
>>TIA,
>>Hari
>Hi Hari,
>(1) No. FORWARD_ONLY is implied by FAST_FORWARD. There
will be a
>performance gain in using a set-based solution over a
cursor based
>solution, if possible.
>(2) No.
>Best, Hugo
>--
>(Remove _NO_ and _SPAM_ to get my e-mail address)
>.
>|||On Wed, 4 Aug 2004 05:31:30 -0700, Hari wrote:
>Hugo,
>Could you please tell me what is the SET-based solution?
>Thanks,
>Hari
Hi Hari,
Without knowing what your current cursor-based solution actually does?
No.
But if you post DDL (create table statements, including all constraints
but excluding irrelevant columns), sample data (as INSERT statements),
expected output, your current cursor-based SQL and a description of the
business problem you're trying to solve, I could give it a shot.
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)

FAST_FORWARD Cursor

Hi Experts,
(1) Will there be any considerable performance gain in
using the default cursor type (FORWARD_ONLY) over
FAST_FORWARD cursors. Cursors are used in my application
as READ ONLY source of data.
(2) Will the use of GOTO inside a SP degrade performance?
Kindly suggest.
TIA,
HariOn Wed, 4 Aug 2004 05:02:01 -0700, Hari wrote:

>Hi Experts,
>(1) Will there be any considerable performance gain in
>using the default cursor type (FORWARD_ONLY) over
>FAST_FORWARD cursors. Cursors are used in my application
>as READ ONLY source of data.
>(2) Will the use of GOTO inside a SP degrade performance?
>Kindly suggest.
>TIA,
>Hari
Hi Hari,
(1) No. FORWARD_ONLY is implied by FAST_FORWARD. There will be a
performance gain in using a set-based solution over a cursor based
solution, if possible.
(2) No.
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||Hugo,
Could you please tell me what is the SET-based solution?
Thanks,
Hari

>--Original Message--
>On Wed, 4 Aug 2004 05:02:01 -0700, Hari wrote:
>
>Hi Hari,
>(1) No. FORWARD_ONLY is implied by FAST_FORWARD. There
will be a
>performance gain in using a set-based solution over a
cursor based
>solution, if possible.
>(2) No.
>Best, Hugo
>--
>(Remove _NO_ and _SPAM_ to get my e-mail address)
>.
>|||On Wed, 4 Aug 2004 05:31:30 -0700, Hari wrote:

>Hugo,
>Could you please tell me what is the SET-based solution?
>Thanks,
>Hari
Hi Hari,
Without knowing what your current cursor-based solution actually does?
No.
But if you post DDL (create table statements, including all constraints
but excluding irrelevant columns), sample data (as INSERT statements),
expected output, your current cursor-based SQL and a description of the
business problem you're trying to solve, I could give it a shot.
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)