SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Cursor and case function


Cursor and case function

Author
Message
frecapi
frecapi
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
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
LutzM
LutzM
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24407 Visits: 13559
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
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)

Group: General Forum Members
Points: 221030 Visits: 42002
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
frecapi
frecapi
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
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...
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)

Group: General Forum Members
Points: 221030 Visits: 42002
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
frecapi
frecapi
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
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....
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)

Group: General Forum Members
Points: 221030 Visits: 42002
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search