Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Cursor and case function Expand / Collapse
Author
Message
Posted Monday, February 14, 2011 4:46 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, February 18, 2011 1:13 PM
Points: 3, Visits: 8
I'm trying to update the permitnum in the table below where PERMITNUM is equal to permit_1 for the first row within that same APN, equal to permit_2 for the second row within that same APN, equal to permit_3 for the third row within that same APN and so on. Noting that If I have more than 8 records under the same APN then permitnum would equal permit_8 + '_9' for row 9, permit_8 + '_10' for row 10 and so on.
Also when the APN changes the ID resets and the same logic applies.


--===== If the test table already exists, drop it
IF OBJECT_ID('TempDB..#mytable','U') IS NOT NULL
DROP TABLE #mytable

--===== Create the test table with
CREATE TABLE #mytable
( ID INT,APN varchar(30), PERMITNUM varchar(30), PERMIT1 varchar(30),PERMIT2 varchar(30),PERMIT3 varchar(30),PERMIT4 varchar(30),PERMIT5 varchar(30),
PERMIT6 varchar(30),PERMIT7 varchar(30),PERMIT8 varchar(30))

--===== Insert test data into mytable
insert into #mytable (ID, APN, PERMITNUM, PERMIT1, PERMIT2, PERMIT3, PERMIT4, PERMIT5, PERMIT6, PERMIT7, PERMIT8)
select 1,'382-070-41-00',NULL,'18027','36027','41323','45953',NULL,NULL,NULL,NULL union all
select 2,'382-070-41-00',NULL,'18027','36027','41323','45953',NULL,NULL,NULL,NULL union all
select 3,'382-070-41-00',NULL,'18027','36027','41323','45953',NULL,NULL,NULL,NULL union all
select 4,'382-070-41-00',NULL,'18027','36027','41323','45953',NULL,NULL,NULL,NULL union all
select 1,'382-070-67-00',NULL,'18542','23747','33782','39306','39918','45482',NULL,NULL union all
select 2,'382-070-67-00',NULL,'18542','23747','33782','39306','39918','45482',NULL,NULL

--=====Cursor script written so far but not working
DROP TABLE APN_MANY_BUS_PERMITS_CURSOR
GO

CREATE TABLE [dbo].[APN_MANY_BUS_PERMITS_CURSOR](
[ID] [bigint] NULL,
[apn] [nvarchar](30),
[permitnum] [nvarchar](30),
[permit1] [nvarchar](30),
[permit2] [nvarchar](30),
[permit3] [nvarchar](30),
[permit4] [nvarchar](30),
[permit5] [nvarchar](30),
[permit6] [nvarchar](30),
[permit7] [nvarchar](30),
[permit8] [nvarchar](30)
) ON [PRIMARY]
GO


SET NOCOUNT ON
GO
DECLARE
@ID int,
@apn varchar(255),
@permitnum varchar(30),
@permit1 varchar(30),
@permit2 varchar(30),
@permit3 varchar(30),
@permit4 varchar(30),
@permit5 varchar(30),
@permit6 varchar(30),
@permit7 varchar(30),
@permit8 varchar(30),
@ID_new int,
@apn_new varchar(255),
@ID_old int,
@apn_old varchar(255)


SET @apn_old ='382-070-41-00'
SET @id_old = 1

DECLARE wf_cursor CURSOR FAST_FORWARD FOR
select ID,
apn,
permit1,
permit2,
permit3,
permit4,
permit5,
permit6,
permit7,
permit8
from #Mytable
order by APN,ID

OPEN wf_cursor

FETCH NEXT FROM wf_cursor INTO
@ID,
@apn,
@permit1,
@permit2,
@permit3,
@permit4,
@permit5,
@permit6,
@permit7,
@permit8

WHILE (@@FETCH_STATUS = 0)
BEGIN
--SET @apn_old = @apn
--SET @id_old= @id


BEGIN
INSERT INTO APN_MANY_BUS_PERMITS_CURSOR
(id,
apn,
permitnum,
permit1,
permit2,
permit3,
permit4,
permit5,
permit6,
permit7,
permit8)
VALUES (@ID,
@apn,
null,
@permit1,
@permit2,
@permit3,
@permit4,
@permit5,
@permit6,
@permit7,
@permit8)
END

BEGIN
IF @apn = Ltrim(@apn_old) and @id = 1
BEGIN
UPDATE APN_MANY_BUS_PERMITS_CURSOR
set permitnum =
case
when id = 1 then permit1
when id = 2 then permit2
when id = 3 then permit3
where @apn = @apn_old
END

END

SET @apn_old = @apn
SET @id_old = @id

FETCH NEXT FROM wf_cursor INTO
@ID,
@apn,
@permit_1,
@permit_2,
@permit_3,
@permit_4,
@permit_5,
@permit_6,
@permit_7,
@permit_8

END
CLOSE wf_cursor
DEALLOCATE wf_cursor

GO
Post #1063932
Posted Monday, February 14, 2011 5:12 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Wednesday, March 4, 2015 3:33 PM
Points: 6,897, Visits: 13,550
What do you need that c.u.r.s.o.r. for?
Unless I'm missing something, the following statement should do it:

UPDATE #mytable
set permitnum =
case
when id = 1 then permit1
when id = 2 then permit2
when id = 3 then permit3
...
END





Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Post #1063935
Posted Monday, February 14, 2011 5:52 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 10:30 PM
Points: 37,455, Visits: 34,317
I guess my other question would be, what happens when there are more than 8 permits?

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1063938
Posted Monday, February 14, 2011 8:20 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, February 18, 2011 1:13 PM
Points: 3, Visits: 8
There can only be a maximum of 8 permits per record. There can be more than 8 records per APN in which case the permitnum would equal permit_8 + '_9' for row 9, permit_8 + '_10' for row 10 and so on...
Post #1063969
Posted Monday, February 14, 2011 9:01 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 10:30 PM
Points: 37,455, Visits: 34,317
frecapi (2/14/2011)
There can only be a maximum of 8 permits per record. There can be more than 8 records per APN in which case the permitnum would equal permit_8 + '_9' for row 9, permit_8 + '_10' for row 10 and so on...


Sorry... looking back, I see you included that in the original post.

Have you considered normalizing the table so that you only have one permit per row?


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1063978
Posted Monday, February 14, 2011 9:16 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, February 18, 2011 1:13 PM
Points: 3, Visits: 8
No I didn't think about that. Something like:
Select ID, apn, (select permit_1 from #mytable), from #mytable union all
Select ID, apn, (select permit_2 from #mytable), from #mytable....
Post #1063980
Posted Monday, February 14, 2011 10:00 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 10:30 PM
Points: 37,455, Visits: 34,317
frecapi (2/14/2011)
No I didn't think about that. Something like:
Select ID, apn, (select permit_1 from #mytable), from #mytable union all
Select ID, apn, (select permit_2 from #mytable), from #mytable....



Yep... kind of. You need to have something in there so you don't include nulls for permits that don't exist. Then you could also have start and end dates for each permit. Every APN could have 1 to as many permits as you need without worrying about running out of columns.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1063992
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse