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

Sunday, March 11, 2012

Calling a SP inside a cursor loop..

I have SP, which has a cursor iterations. Need to call another SP for
every loop iteration of the cursor. The pseudo code is as follows..

Create proc1 as
Begin

Variable declrations...

declare EffectiveDate_Cursor cursor for
select field1,fld2 from tab1,tab2 where tab1.effectivedate<Getdate()
--/////Assuming the above query would result in 3 records
Open EffectiveDate_Cursor
Fetch next From EffectiveDate_Cursor Into @.FLD1,@.FLD2
begin
/*Calling my second stored proc with fld1 as a In parameter
and Op1 and OP2 Out parameters*/
Exec sp_minCheck @.fld1, @.OP1 output,@.OP2 output
Do something based on Op1 and Op2.
end
While @.@.Fetch_Status = 0
Fetch next From EffectiveDate_Cursor Into @.FLD1,@.FLD2
/* Assume If loop count is 3.
and If the Fetch stmt is below the begin Stmt, the loop iterations are
4 else the loop iterations are 2*/
begin
/*Calling my second stored proc with fld1 as a In parameter and Op1
and OP2 Out parameters*/
Exec sp_minCheck @.fld1, @.OP1 output,@.OP2 output
Do something based on Op1 and Op2.
end

The problem I had been facing is that, the when a stored proc is called
within the loop, the proc is getting into infinite loops.
Any Help would be appreciated.

Satish(satishchandra999@.gmail.com) writes:
> I have SP, which has a cursor iterations. Need to call another SP for
> every loop iteration of the cursor. The pseudo code is as follows..
> Create proc1 as
> Begin
> Variable declrations...
>...
> While @.@.Fetch_Status = 0
> Fetch next From EffectiveDate_Cursor Into @.FLD1,@.FLD2
> /* Assume If loop count is 3.
> and If the Fetch stmt is below the begin Stmt, the loop iterations are
> 4 else the loop iterations are 2*/
> begin
> /*Calling my second stored proc with fld1 as a In parameter and Op1
> and OP2 Out parameters*/
> Exec sp_minCheck @.fld1, @.OP1 output,@.OP2 output
> Do something based on Op1 and Op2.
> end
>
> The problem I had been facing is that, the when a stored proc is called
> within the loop, the proc is getting into infinite loops.

May I guess: the inner process also uses cursors?

Anyway, the proper way to program a cursor loop is:

DECLARE cur INENSITIVE CURSOR FOR
SELECT ...
-- Error handling goes here

OPEN cur

WHILE 1 = 1
BEGIN
FETCH cur INTO @.x, @.y, ...
IF @.@.fetch_status <> 0
BREAK

-- Do stuff
END

DEALLOCATE cur

By using only one FETCH statements you avoid funny errors, when you change
the cursor and forgets to change the cursor at the end of the loop. And by
checl @.@.fetch_status directly after the FETCH, you know that @.@.fetch_status
relates to that FETCH.

... and in case no one ever told you before: avoid iterations as much as
you can, and try to always work set-based. Yes, I can understand that you
want to reuse code, and if the oomplexity is high enough it may be
warranted if the number of rows in the cursor is moderate. But the cost
in performance for iterative solutions can be *enourmous*. A database
engine is simply not designed for this type of processing.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||>> I have SP, which has a cursor iterations. Need to call another SP for
every loop iteration of the cursor. <<

No. You need to learn to program in SQL. All you are doing is
mimicing a 1960's 3GL magnetic tape file system . In your pseudo
code, you even refer to fields instead of columns! You put the "sp_"
prefix on procedure names!

Don't you understand that SQL is a non-procedurdal language? You
should write only a few cursors in 20 years, not two in one
application.

Your whole approach to the problem is **fundamentally** wrong.

>> The problem I had been facing is that, the when a stored proc is called within the loop, the proc is getting into infinite loops. <<

It is very hard to de-bug code that you will not show us. But when
pseudo code is this awful, I bet that the real code is a total mess.
More cursors? Dynamic SQL? Badly written procedural code with poor
coupling and cohesion?

>> Any Help would be appreciated. <<

You have no idea what you are doing. What you will get on Newsgroups
is a quick kludge to get rid of you, but not any real help. You need
to stop programming and get some education; then get some training.

calling a remote SP call that returns a cursor

Hi,
a SP in my database is calling a SP in external database using a linked
server.
ie ni my SP: exec YourLink.YourDB.dbo.GetResultsSP @.CursorReturn out
the SP in the external database must return me a result set OR cursor. I
have having issues reading the result set from my SP so have tried
implementing logic as a cursor. I get error to do with RPC and cursors not
allowed.
Is there any way I can get this cursor back to my SP if it is returned as
just a default result set from the external SP? or if not then as a defined
cursor?
Any help would be apprecated,
SteveI read this post after reading your later on.
You could try returning table datatype from a user defined function from
your remote procedure call.
In your calling procedure, you could get resultset from the table datatype
returned from user defined function.
Please let me know if I misunderstood your question or you need more help on
this.
--
http://zulfiqar.typepad.com
BSEE, MCP
"Steve" wrote:

> Hi,
> a SP in my database is calling a SP in external database using a linked
> server.
> ie ni my SP: exec YourLink.YourDB.dbo.GetResultsSP @.CursorReturn out
> the SP in the external database must return me a result set OR cursor. I
> have having issues reading the result set from my SP so have tried
> implementing logic as a cursor. I get error to do with RPC and cursors not
> allowed.
> Is there any way I can get this cursor back to my SP if it is returned as
> just a default result set from the external SP? or if not then as a define
d
> cursor?
> Any help would be apprecated,
> Steve|||Hi,
it is a 3rd party vendor db and they have "exposed" a public SP that I can
call to get all time and attendance activity for a period of time that I pas
s
as parameters.
The issue is that the vendor SP returns the rows as a result set of the SP
and I am finding it difficult to load these into my SP for processing.
I could potentially contact the vendor and ask them to make some changes but
I need to be clear on what I need them to do.
thanks,
Steve
"ZULFIQAR SYED" wrote:
> I read this post after reading your later on.
> You could try returning table datatype from a user defined function from
> your remote procedure call.
> In your calling procedure, you could get resultset from the table datatype
> returned from user defined function.
> Please let me know if I misunderstood your question or you need more help
on
> this.
> --
> http://zulfiqar.typepad.com
> BSEE, MCP
>
> "Steve" wrote:
>|||Here I got some of the code from BOL (books online) to give you an example
on how to populate a table from a SP resultset.
HTH..
use pubs
go
drop table author_sales
go
CREATE TABLE author_sales
( data_source varchar(20),
au_id varchar(11),
au_lname varchar(40),
sales_dollars smallmoney
)
GO
-- ========================================
=====
-- Create procedure basic template
-- ========================================
=====
-- creating the store procedure
IF EXISTS (SELECT name
FROM sysobjects
WHERE name = N'mytestprocA'
AND type = 'P')
DROP PROCEDURE dbo.mytestprocA
GO
CREATE PROCEDURE dbo.mytestprocA
AS
INSERT author_sales EXECUTE get_author_sales
select * from author_sales
GO
-- ========================================
=====
-- example to execute the store procedure
-- ========================================
=====
EXECUTE dbo.mytestprocA
GO
http://zulfiqar.typepad.com
BSEE, MCP
"Steve" wrote:
> Hi,
> it is a 3rd party vendor db and they have "exposed" a public SP that I can
> call to get all time and attendance activity for a period of time that I p
ass
> as parameters.
> The issue is that the vendor SP returns the rows as a result set of the SP
> and I am finding it difficult to load these into my SP for processing.
> I could potentially contact the vendor and ask them to make some changes b
ut
> I need to be clear on what I need them to do.
> thanks,
> Steve
> "ZULFIQAR SYED" wrote:
>

Thursday, March 8, 2012

call stored procedure in cursor

Hi all
Can I call and use the stored procedure resultset in the
cursor like
DECLARE WeeklyYield_Cursor Cursor For
dbo.RPT_WeeklyYieldCycleTimeForWeekRange '301','327'
Open WeeklyYield_Cursor
Fetch next from WeeklyYield_Cursor into @.WeekID,@.LotID
While @.@.Fetch_Status = 0
begin
up_RPT_PrepareWeeklyDefectDetailData @.Lotid
up_RPT_UpdateWeeklyYieldCycleTime
Fetch next from WeeklyYield_Cursor into @.WeekID,@.LotID
end
CLOSE WeeklyYield_Cursor
DEALLOCATE WeeklyYield_Cursor
Plese help
AnandHi Anand,
You have to insert the results of the stored procedure into a temporary
table and then define the cursor on the temporary table:
CREATE TABLE #WeeklyYield (WeekID INT, LotID INT)
INSERT INTO #WeeklyYield
EXEC dbo.RPT_WeeklyYieldCycleTimeForWeekRange '301','327'
Keep in mind though that cursor are much slower than set based solutions, so
you might want to look if you can rewrite things. The least you should do is
declare your cursor as LOCAL FAST_FORWARD for optimal cursor performance.
--
Jacco Schalkwijk MCDBA, MCSD, MCSE
Database Administrator
Eurostop Ltd.
"Anand" <gurusanand1@.sifymail.com> wrote in message
news:079401c35705$e6e13e00$a301280a@.phx.gbl...
> Hi all
> Can I call and use the stored procedure resultset in the
> cursor like
> DECLARE WeeklyYield_Cursor Cursor For
> dbo.RPT_WeeklyYieldCycleTimeForWeekRange '301','327'
> Open WeeklyYield_Cursor
> Fetch next from WeeklyYield_Cursor into @.WeekID,@.LotID
> While @.@.Fetch_Status = 0
> begin
> up_RPT_PrepareWeeklyDefectDetailData @.Lotid
> up_RPT_UpdateWeeklyYieldCycleTime
> Fetch next from WeeklyYield_Cursor into @.WeekID,@.LotID
> end
> CLOSE WeeklyYield_Cursor
> DEALLOCATE WeeklyYield_Cursor
> Plese help
> Anand|||I assume you're asking if you can use
"dbo.RPT_WeeklyYieldCycleTimeForWeekRange '301','327'" as the cursor...
the answer is no...If you are using SQL Server 2000, you could use a
table function to return a selectable resultset. I've never actually done
it from a cursor, but have done it as part of otehr select statements.
Can't be all that different.
TG
Anand wrote:
> Hi all
> Can I call and use the stored procedure resultset in the
> cursor like
> DECLARE WeeklyYield_Cursor Cursor For
> dbo.RPT_WeeklyYieldCycleTimeForWeekRange '301','327'
> Open WeeklyYield_Cursor
> Fetch next from WeeklyYield_Cursor into @.WeekID,@.LotID
> While @.@.Fetch_Status = 0
> begin
> up_RPT_PrepareWeeklyDefectDetailData @.Lotid
> up_RPT_UpdateWeeklyYieldCycleTime
> Fetch next from WeeklyYield_Cursor into @.WeekID,@.LotID
> end
> CLOSE WeeklyYield_Cursor
> DEALLOCATE WeeklyYield_Cursor
> Plese help
> Anand
/n/n/n==================================*** Sent via DeveloperKB.com http://www.developerkb.com ***
For all your programming needs.
==================================

Wednesday, March 7, 2012

Call function from a procedure and assign return value to variable

What is that cursor supposed to be doing?
Are you really looping through the whole table to get a single value'
And possibly some random value at that?
i cannot in good conscience give you an answer and let you keep that UDF...
Chris wrote:
> Hi all,
> I have a procedure proc1, which needs to update a table.
> One of the columns to update the table is calculated using a function
> funct1 (which returns a smalldatetime)
> My problem is how do I call the function funct1 from procedure proc1,
> so that the return value of function is saved in a variable in
> procedure.
>
> Sample Procedure Proc1
> ALTER PROCEDURE proc1
> @.x int
> AS
> declare @.ret smalldatetime
> --Here, I want @.ret to be a smalldatetime value returned by function
> funct1.
> --I tried the below (though knowing it wont work):
> --@.ret = select dbo.funct1(param1), but it gives error.
> --I even tried(again, though knowing it wont work):
> --update myTable set field1 = select dbo.funct1(param1), but it gives
> error.
> --WHERE ...
> update myTable set field1 = @.ret
> WHERE ...
>
> Sample function funct1:
> alter FUNCTION dbo.funct1
> (
> @.val1 decimal,
> @.val2 int
> )
> RETURNS smalldatetime
> AS
> BEGIN
> DECLARE @.ret1 decimal
> DECLARE @.ret2 smalldatetime
> DECLARE sel_Cursor CURSOR FOR SELECT field1, field2
> FROM myTable
> OPEN sel_Cursor
> FETCH NEXT FROM sel_Cursor INTO @.ret1, @.ret2
> CLOSE sel_Cursor
> DEALLOCATE sel_Cursor
> if @.val1 = @.ret2
> return @.ret2
> else
> set @.ret2 = some function...
> RETURN @.ret2
> END
> ---
> Pls help..
> TIA..
>ah - i've been enlightened by a colleague
MSSQL isn't Oracle - many things don't work the same - functions and
cursors in particular.
in oracle (prior to 9i), it's best to use a function to return single
values [in mssql - only if the scalar function will truly get a lot of
re-use; sql2005 is better at udfs than sql2k],
and i understand that performance can be better with the cursor/fetch
first row value [hopefully, there was supposed to be a where on that
select *] in mssql, cursors are to be avoided.
unless this udf is re-used a lot, in mssql this works better in-line
with more specific ddl, a better mssql approach can be suggested
Trey Walpole wrote:
> What is that cursor supposed to be doing?
> Are you really looping through the whole table to get a single value'
> And possibly some random value at that?
> i cannot in good conscience give you an answer and let you keep that UDF..
.
>
> Chris wrote:
>|||see reply to myself :)
Chris wrote:
> Thanks guys..
> Damn, I dont knw why i was using the 'select'. Now it worked for me.
> Trey,
> I am using the cursor to fetch value and assign it to a variable.
> The 'where' clause fetches single row.
> And the select query returns single value.
> But still i have used cursor coz thts the only way I know (I am pretty
> new to SQL :( ).
> Is there any other way to do so'
> Trey Walpole wrote:
>
>