Sunday, March 11, 2012

Declaring a cursor on a temporary table

How do I declare a cursor on a table like #TempPerson
when thhs table is only created when I do :
Select Name, Age Into #TempPerson From Personcan i ask especially what your trying to do more in detail please.|||I hjave NEVER done this...and only did this for myself to see if it works (didn't see why it wouldn't), but I highly DO NOT recommed this...

USE Northwind
GO
SELECT * INTO #TEMP FROM Orders

DECLARE
@.OrderID int
, @.CustomerID nchar(10)
, @.EmployeeID int
, @.OrderDate datetime
, @.RequiredDate datetime
, @.ShippedDate datetime
, @.ShipVia int
, @.Freight money
, @.ShipName nvarchar(80)
, @.ShipAddress nvarchar(120)
, @.ShipCity nvarchar(30)
, @.ShipRegion nvarchar(30)
, @.ShipPostalCode nvarchar(20)
, @.ShipCountry nvarchar(30)
, @.LoopCounter int

DECLARE myCursor99 CURSOR
FOR
SELECT OrderID
, CustomerID
, EmployeeID
, OrderDate
, RequiredDate
, ShippedDate
, ShipVia
, Freight
, ShipName
, ShipAddress
, ShipCity
, ShipRegion
, ShipPostalCode
, ShipCountry
FROM #Temp

OPEN myCursor99

SELECT @.LoopCounter = 0

FETCH NEXT FROM myCursor99 INTO
@.OrderID
, @.CustomerID
, @.EmployeeID
, @.OrderDate
, @.RequiredDate
, @.ShippedDate
, @.ShipVia
, @.Freight
, @.ShipName
, @.ShipAddress
, @.ShipCity
, @.ShipRegion
, @.ShipPostalCode
, @.ShipCountry

WHILE @.@.FETCH_STATUS = 0
BEGIN
-- Some Code
SELECT @.LoopCounter = @.LoopCounter + 1
FETCH NEXT FROM myCursor99 INTO
@.OrderID
, @.CustomerID
, @.EmployeeID
, @.OrderDate
, @.RequiredDate
, @.ShippedDate
, @.ShipVia
, @.Freight
, @.ShipName
, @.ShipAddress
, @.ShipCity
, @.ShipRegion
, @.ShipPostalCode
, @.ShipCountry
END
CLOSE myCursor99
DEALLOCATE myCursor99
DROP TABLE #Temp
SELECT 'Loops incurred: ' + CONVERT(varchar(15),@.LoopCounter)
GO|||I've simplified a stored proc
It's the "declare Age dynamic scroll cursor for select Age from #TempPerson
" that doesn't work

create procedure GetAges
as
begin
declare @.Age char(20),
declare @.PreviousAge char(20),

declare Age dynamic scroll cursor for select Age from #TempPerson

select Name,Age into #TempPerson From Person
insert into #TempPerson (Name,Age) Select LastName,Age From OtherPerson Where TypePerson = '1'

select @.PreviousAge=' '

open Age
while 1=1
begin
fetch next Age into @.Age
if @.Age <> @.PreviousAge
...
select @.Age=@.PreviousAge
close Age
end|||OK Brett thank you -- AGAIN --
that principle will work
I'll give you some news tomorrow
I'm fed up with stored proc for today !!!!!|||Good Luck, but I bet you, if you tell me what you're trying to do, we can find a set based solution...|||I'm migrating the Sybase structure to SQL Server
and I've go to rewrite
Very-Stupidly-And-Badly-Written-Sybase Watcom-Stored proc

I hate working with programs written by spagetti-minded-programmers

So you don't want to see to stored proc (REALLY don't !!!)|||Yup, sometimes it's not an option...

I will go feel bad for you now....

No comments:

Post a Comment