Showing posts with label below. Show all posts
Showing posts with label below. Show all posts

Wednesday, March 28, 2012

New to Cursors - HELP

My stored proc is below but I have no idea as to why the proc just spins - i
t
runs and never returns - I am assuming I have an infinite look somehow in my
cursor but this is the first SP I've written with a cursor so I'm lost -
Anyone who can point me in the right direction I would be very grateful -
ALSO - is there a way to return to the results screen values at certain
points in a transaction - for example as each row changes can you return a
value to the results screen so you can see that it is doing something at all
?
- No debug authority on the server or I'd do that.
ALTER PROCEDURE dbo.InterrogateRealignRecords AS
/* ----
GO SEE IF THERE ARE ANY CHANGED SHADE
RECORDS FIRST AND RETURN THE COUNT INTO
@.ICOUNTSHDCHGS
---- */
Declare @.numSUCforShade int,@.ICOUNTSHDCHGS int,@.ICOUNTPROPCHGS INT,
@.ICOUNTSUBSEGCHGS INT,@.old_shade_ID Int, @.old_property_ID Int, @.old_subseg_I
D
Int
-- select count(*) from #shades_suc_bu
-- drop table #shades_suc_bu
select Shade_ID, SUC_Code,UPC
into #shades_suc_bu
from shade_Suc
Declare curChangeValues Scroll Cursor For
Select Count(*)
From PPGG_Admin.UPC_Shades_Realigned
Where shade_ID <> old_Shade_ID
Open curChangeValues
Fetch Next From curChangeValues
Into @.iCOUNTSHDCHGS
WHILE (@.@.fetch_status = 0)
BEGIN
if @.ICOUNTSHDCHGS > 0
/* --- */
/* THERE ARE CHANGED SHADE ID'S TO LOOK AT */
/* GO AND GET THE RECORDS SO WE CAN LOOK */
/* AT EACH ONE SEPARATELY */
/* --- */
declare curShadeChngVal scroll cursor for
Select old_Shade_ID, old_property_ID,old_product_Subsegment_I
D
From PPGG_Admin.UPC_Shades_Realigned
Where shade_ID <> old_Shade_ID
Open curShadeChngVal
Fetch Next From curShadeChngVal
Into @.old_shade_ID
,@.old_property_ID
,@.old_subseg_ID
Select @.old_shade_ID
,@.old_property_ID
,@.old_subseg_ID
WHILE (@.@.fetch_status =0)
Begin
/* ---*/
/* DURING THE LOOP CHECK THE SUC TABLE
TO SEE IF ANY UPCS STILL EXIST
FOR THIS SHADE */
/* ---*/
DECLARE curSUCVal scroll cursor for
Select Count(*) from Shade_SUC
where Shade_ID = @.old_Shade_ID
OPEN curSUCVal
fetch next from curSUCVal
into @.numSUCforShade
if @.numSUCforShade > 0
Begin
--declare @.dan int
--set @.dan =1
delete from #shades_suc_bu where Shade_ID = @.old_Shade_ID
end
CLOSE curSUCVal
Deallocate curSUCVal
END
end
Close curChangeValues
Deallocate curChangeValues
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GOHEY THANKS - I sufficiently feel like an idiot - not everyone is a guru my
friend. But thanks for your tact. (not)
"CBretana" wrote:
> Entire thing is messed up
> 1) Each time you execute a cursor loop you have to Fetch Next INSIDE THE
> LOOP at the end, or you're just processing the same row over and over..
> 2) Same with cursor curShadeChngVal, there's a Ferch Next just befor the
> While Loop, but no Fetch Next at the end of the While loop to move to the
> nect record...
> 3) you have triply nested cursors and the innermost cursor (curSUCVal) see
ms
> not to be looping at all, (THERE'S NO WHILE LOOP FOR IT !!) you're just
> processing the first row of it...
> So why use cursor curSUCVal at all '
> "Dan" wrote:
>|||Entire thing is messed up
1) Each time you execute a cursor loop you have to Fetch Next INSIDE THE
LOOP at the end, or you're just processing the same row over and over..
2) Same with cursor curShadeChngVal, there's a Ferch Next just befor the
While Loop, but no Fetch Next at the end of the While loop to move to the
nect record...
3) you have triply nested cursors and the innermost cursor (curSUCVal) seems
not to be looping at all, (THERE'S NO WHILE LOOP FOR IT !!) you're just
processing the first row of it...
So why use cursor curSUCVal at all '
"Dan" wrote:

> My stored proc is below but I have no idea as to why the proc just spins -
it
> runs and never returns - I am assuming I have an infinite look somehow in
my
> cursor but this is the first SP I've written with a cursor so I'm lost -
> Anyone who can point me in the right direction I would be very grateful -
> ALSO - is there a way to return to the results screen values at certain
> points in a transaction - for example as each row changes can you return a
> value to the results screen so you can see that it is doing something at a
ll?
> - No debug authority on the server or I'd do that.
> ALTER PROCEDURE dbo.InterrogateRealignRecords AS
> /* ----
> GO SEE IF THERE ARE ANY CHANGED SHADE
> RECORDS FIRST AND RETURN THE COUNT INTO
> @.ICOUNTSHDCHGS
> ---- */
> Declare @.numSUCforShade int,@.ICOUNTSHDCHGS int,@.ICOUNTPROPCHGS INT,
> @.ICOUNTSUBSEGCHGS INT,@.old_shade_ID Int, @.old_property_ID Int, @.old_subseg
_ID
> Int
> -- select count(*) from #shades_suc_bu
> -- drop table #shades_suc_bu
> select Shade_ID, SUC_Code,UPC
> into #shades_suc_bu
> from shade_Suc
> Declare curChangeValues Scroll Cursor For
> Select Count(*)
> From PPGG_Admin.UPC_Shades_Realigned
> Where shade_ID <> old_Shade_ID
> Open curChangeValues
> Fetch Next From curChangeValues
> Into @.iCOUNTSHDCHGS
> WHILE (@.@.fetch_status = 0)
> BEGIN
> if @.ICOUNTSHDCHGS > 0
> /* --- */
> /* THERE ARE CHANGED SHADE ID'S TO LOOK AT */
> /* GO AND GET THE RECORDS SO WE CAN LOOK */
> /* AT EACH ONE SEPARATELY */
> /* --- */
> declare curShadeChngVal scroll cursor for
> Select old_Shade_ID, old_property_ID,old_product_Subsegment_I
D
> From PPGG_Admin.UPC_Shades_Realigned
> Where shade_ID <> old_Shade_ID
> Open curShadeChngVal
> Fetch Next From curShadeChngVal
> Into @.old_shade_ID
> ,@.old_property_ID
> ,@.old_subseg_ID
> Select @.old_shade_ID
> ,@.old_property_ID
> ,@.old_subseg_ID
> WHILE (@.@.fetch_status =0)
> Begin
> /* ---*/
> /* DURING THE LOOP CHECK THE SUC TABLE
> TO SEE IF ANY UPCS STILL EXIST
> FOR THIS SHADE */
> /* ---*/
> DECLARE curSUCVal scroll cursor for
> Select Count(*) from Shade_SUC
> where Shade_ID = @.old_Shade_ID
> OPEN curSUCVal
> fetch next from curSUCVal
> into @.numSUCforShade
> if @.numSUCforShade > 0
> Begin
> --declare @.dan int
> --set @.dan =1
> delete from #shades_suc_bu where Shade_ID = @.old_Shade_ID
> end
> CLOSE curSUCVal
> Deallocate curSUCVal
> END
>
> end
> Close curChangeValues
> Deallocate curChangeValues
>
> GO
> SET QUOTED_IDENTIFIER OFF
> GO
> SET ANSI_NULLS ON
> GO
>|||Dan, I Just noticed somethiong else...
the First cursor, curChangeValues is defined on
Select Count(*)
From PPGG_Admin.UPC_Shades_Realigned
Where shade_ID <> old_Shade_ID
This doesn't return a set of records at all, it just returns a single count
of records.. .You use cursors (when there's no other alternative) to ITERAT
E
through a COLLECTION of records when you need t oprocess each one
individually... not simply to get data. I suspect you really need to read a
bit about the basics of SQL.
"Dan" wrote:

> My stored proc is below but I have no idea as to why the proc just spins -
it
> runs and never returns - I am assuming I have an infinite look somehow in
my
> cursor but this is the first SP I've written with a cursor so I'm lost -
> Anyone who can point me in the right direction I would be very grateful -
> ALSO - is there a way to return to the results screen values at certain
> points in a transaction - for example as each row changes can you return a
> value to the results screen so you can see that it is doing something at a
ll?
> - No debug authority on the server or I'd do that.
> ALTER PROCEDURE dbo.InterrogateRealignRecords AS
> /* ----
> GO SEE IF THERE ARE ANY CHANGED SHADE
> RECORDS FIRST AND RETURN THE COUNT INTO
> @.ICOUNTSHDCHGS
> ---- */
> Declare @.numSUCforShade int,@.ICOUNTSHDCHGS int,@.ICOUNTPROPCHGS INT,
> @.ICOUNTSUBSEGCHGS INT,@.old_shade_ID Int, @.old_property_ID Int, @.old_subseg
_ID
> Int
> -- select count(*) from #shades_suc_bu
> -- drop table #shades_suc_bu
> select Shade_ID, SUC_Code,UPC
> into #shades_suc_bu
> from shade_Suc
> Declare curChangeValues Scroll Cursor For
> Select Count(*)
> From PPGG_Admin.UPC_Shades_Realigned
> Where shade_ID <> old_Shade_ID
> Open curChangeValues
> Fetch Next From curChangeValues
> Into @.iCOUNTSHDCHGS
> WHILE (@.@.fetch_status = 0)
> BEGIN
> if @.ICOUNTSHDCHGS > 0
> /* --- */
> /* THERE ARE CHANGED SHADE ID'S TO LOOK AT */
> /* GO AND GET THE RECORDS SO WE CAN LOOK */
> /* AT EACH ONE SEPARATELY */
> /* --- */
> declare curShadeChngVal scroll cursor for
> Select old_Shade_ID, old_property_ID,old_product_Subsegment_I
D
> From PPGG_Admin.UPC_Shades_Realigned
> Where shade_ID <> old_Shade_ID
> Open curShadeChngVal
> Fetch Next From curShadeChngVal
> Into @.old_shade_ID
> ,@.old_property_ID
> ,@.old_subseg_ID
> Select @.old_shade_ID
> ,@.old_property_ID
> ,@.old_subseg_ID
> WHILE (@.@.fetch_status =0)
> Begin
> /* ---*/
> /* DURING THE LOOP CHECK THE SUC TABLE
> TO SEE IF ANY UPCS STILL EXIST
> FOR THIS SHADE */
> /* ---*/
> DECLARE curSUCVal scroll cursor for
> Select Count(*) from Shade_SUC
> where Shade_ID = @.old_Shade_ID
> OPEN curSUCVal
> fetch next from curSUCVal
> into @.numSUCforShade
> if @.numSUCforShade > 0
> Begin
> --declare @.dan int
> --set @.dan =1
> delete from #shades_suc_bu where Shade_ID = @.old_Shade_ID
> end
> CLOSE curSUCVal
> Deallocate curSUCVal
> END
>
> end
> Close curChangeValues
> Deallocate curChangeValues
>
> GO
> SET QUOTED_IDENTIFIER OFF
> GO
> SET ANSI_NULLS ON
> GO
>|||Seriously, if you are new to cursors then you should stay away from
them for straight data manipulation stuff. Most of the time there are
better solutions without using cursors so until you are VERY confident
that you have enough SQL expertise to know when a cursor is the right
choice it's best to assume that you don't need them at all.
If you need more help with a solution then please come back with DDL
and sample data so that we can understand your requirements. See:
http://www.aspfaq.com/etiquette.asp?id=5006
David Portas
SQL Server MVP
--|||OK David,
If I am in need of iterating through a row of records to decide on an action
(update, delete, insert new etc) then what other option do I have in a Store
d
Proc?
Thanks
"David Portas" wrote:

> Seriously, if you are new to cursors then you should stay away from
> them for straight data manipulation stuff. Most of the time there are
> better solutions without using cursors so until you are VERY confident
> that you have enough SQL expertise to know when a cursor is the right
> choice it's best to assume that you don't need them at all.
> If you need more help with a solution then please come back with DDL
> and sample data so that we can understand your requirements. See:
> http://www.aspfaq.com/etiquette.asp?id=5006
> --
> David Portas
> SQL Server MVP
> --
>|||Dan,
CAPS not meant to insult, just to emphasize, sorry if tone was rough...
No attempt to insult or denigrate was intended.
And there's a differencve between being intelligence, and education.
EVERYONE (CAPS only to emphasize) is "undeducated" when they begin learning
a
new skill... There's no need to feel offended because you're not yet educate
d
...
From your post, a judgement might be made about your SQL knowledge, but
not about your intelligence. It's what you decide to do next that will
speak to the latter issue !
"Dan" wrote:
> HEY THANKS - I sufficiently feel like an idiot - not everyone is a guru my
> friend. But thanks for your tact. (not)
> "CBretana" wrote:
>|||Maybe a WHERE clause on each of those statements in succession -
probably with EXISTS, but that depends on what you are doing.
UPDATE x
SET ... ?
WHERE EXISTS
(SELECT *
FROM PPGG_Admin.UPC_Shades_Realigne=ADd
WHERE id =3D x.id
AND ... ?)
INSERT INTO z (...)
SELECT ...
FROM PPGG_Admin.UPC_Shades_Realigne=ADd AS T
WHERE NOT EXISTS
(SELECT *
FROM z
WHERE T.id =3D z.id
AND ... ?)
--=20
David Portas
SQL Server MVP
--|||Why do you need to iterate through a collection of records?
It appears from the code you posted the only thing the SP is doing is
deleting selected records from temp table #shades_suc_bu in the line
delete from #shades_suc_bu where Shade_ID = @.old_Shade_ID
Which is sort of pointless because the temp table is not then used for
anything else, and it will dissapear immediately after the SP terminates.
But even if it was doing something, the delete could probably be done in one
SQL statement that properly identified the records that need to be deleted,
instead of using any cursors at all...
But first, what exactly is this SP supposed to be doing ?
"Dan" wrote:
> OK David,
> If I am in need of iterating through a row of records to decide on an acti
on
> (update, delete, insert new etc) then what other option do I have in a Sto
red
> Proc?
> Thanks
> "David Portas" wrote:
>|||Usenet drama....
"Dan" <Dan@.discussions.microsoft.com> wrote in message
news:4F63E75A-9565-464A-AA41-36F7F3B46CDA@.microsoft.com...
> My stored proc is below but I have no idea as to why the proc just spins -
it
> runs and never returns - I am assuming I have an infinite look somehow in
my
> cursor but this is the first SP I've written with a cursor so I'm lost -
> Anyone who can point me in the right direction I would be very grateful -
> ALSO - is there a way to return to the results screen values at certain
> points in a transaction - for example as each row changes can you return a
> value to the results screen so you can see that it is doing something at
all?
> - No debug authority on the server or I'd do that.
> ALTER PROCEDURE dbo.InterrogateRealignRecords AS
> /* ----
> GO SEE IF THERE ARE ANY CHANGED SHADE
> RECORDS FIRST AND RETURN THE COUNT INTO
> @.ICOUNTSHDCHGS
> ---- */
> Declare @.numSUCforShade int,@.ICOUNTSHDCHGS int,@.ICOUNTPROPCHGS INT,
> @.ICOUNTSUBSEGCHGS INT,@.old_shade_ID Int, @.old_property_ID Int,
@.old_subseg_ID
> Int
> -- select count(*) from #shades_suc_bu
> -- drop table #shades_suc_bu
> select Shade_ID, SUC_Code,UPC
> into #shades_suc_bu
> from shade_Suc
> Declare curChangeValues Scroll Cursor For
> Select Count(*)
> From PPGG_Admin.UPC_Shades_Realigned
> Where shade_ID <> old_Shade_ID
> Open curChangeValues
> Fetch Next From curChangeValues
> Into @.iCOUNTSHDCHGS
> WHILE (@.@.fetch_status = 0)
> BEGIN
> if @.ICOUNTSHDCHGS > 0
> /* --- */
> /* THERE ARE CHANGED SHADE ID'S TO LOOK AT */
> /* GO AND GET THE RECORDS SO WE CAN LOOK */
> /* AT EACH ONE SEPARATELY */
> /* --- */
> declare curShadeChngVal scroll cursor for
> Select old_Shade_ID, old_property_ID,old_product_Subsegment_I
D
> From PPGG_Admin.UPC_Shades_Realigned
> Where shade_ID <> old_Shade_ID
> Open curShadeChngVal
> Fetch Next From curShadeChngVal
> Into @.old_shade_ID
> ,@.old_property_ID
> ,@.old_subseg_ID
> Select @.old_shade_ID
> ,@.old_property_ID
> ,@.old_subseg_ID
> WHILE (@.@.fetch_status =0)
> Begin
> /* ---*/
> /* DURING THE LOOP CHECK THE SUC TABLE
> TO SEE IF ANY UPCS STILL EXIST
> FOR THIS SHADE */
> /* ---*/
> DECLARE curSUCVal scroll cursor for
> Select Count(*) from Shade_SUC
> where Shade_ID = @.old_Shade_ID
> OPEN curSUCVal
> fetch next from curSUCVal
> into @.numSUCforShade
> if @.numSUCforShade > 0
> Begin
> --declare @.dan int
> --set @.dan =1
> delete from #shades_suc_bu where Shade_ID = @.old_Shade_ID
> end
> CLOSE curSUCVal
> Deallocate curSUCVal
> END
>
> end
> Close curChangeValues
> Deallocate curChangeValues
>
> GO
> SET QUOTED_IDENTIFIER OFF
> GO
> SET ANSI_NULLS ON
> GO
>

Saturday, February 25, 2012

New features for Katmai

Many of you are curious about the new features for Service Broker in Katmai. Below is a list of the features a brief description:

Conversation Priority - This feature allows the messages of a one conversation to be prioritized higher than the priority of another conversation. The priority affects both the sending and the receiving side. On the sending side it affects the order messages from different conversations are put on the wire as they are pulled out of the transmission queue. On the receiving side it affects the order in which the next conversation group is chosen via a RECEIVE call. A priority only applies to one side of the conversation (i.e. priority does not travel across the wire). Priority information is stored in a system table and DDL is used to create/alter the priority. You can configure priority via local service name, remote service name, and contract. The system uses these rules to set the priority of a conversation at the time it is created. The priority level will not change for the lifetime of the conversation.

Diagnostic Tool - This is a command line tool that allows you to easily diagnose both configuration and runtime problems in your Service Broker application. It reports the errors as it finds them along with recommendations on how to fix them. Below are examples of the kinds of problems it can find:

- Service Broker is not enabled

- A route is missing

External Activation - Activation is the mechanism that launches business logic to process messages whenever there is an event on the queue (such as message arrival, queue enabled, timer, etc...). In Yukon the business logic had to be a stored procedure. This is what we call "internal activation" because the business logic runs inside the database. In Katmai we are supporting "external activation" where the business logic can run outside the database. This works by having an NT service, that runs on the same machine as the database or a different machine, watch a queue for events. When a queue event happens it starts the associated process. The executable file is bound to the queue via an XML configuration file deployed with the NT service. The Service Broker team supported external activation as a sample for Yukon but it is being productized for Katmai. The Katmai tool will support both Katmai and Yukon instances.


Management Studio Enhancements - Of course one of our top asks has been for better Management Studio enhancements for the Broker objects. We will be adding additional actions, such as a Create and Alter on all the Service Broker objects in the Object Explorer. We will also add custom actions like "Enable Activation". There will also be a grid view of the contents of a queue and a property page.

If you have any questions about these features please let us know!

Rick Negrin

Program Manager

Sql Server Service Broker

Hi Rick!

Thanks for the information. Can you already tell us, in which CTP we will see these features?

What's about integrating Service Listing Manager into SQL Server Management Studio?

Thanks

Klaus Aschenbrenner

http://www.csharp.at

http://www.csharp.at/blog

|||

We are not sure yet exactly what improvements will fall into what CTPs. We will let you know when we know. Unfortunately the Service Listing will not be integrated into Management Studio in this release.

Rick Negrin

Program Manager

Sql Server Service Broker

|||

Are there any specific reasons why the Service Listing Manager isn't integrated? Because it's a tool that is completely accepted by the Service Broker community...

Thanks

-Klaus