February 14, 2011 at 4:46 pm
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
selectID,
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
February 14, 2011 at 5:12 pm
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
February 14, 2011 at 5:52 pm
I guess my other question would be, what happens when there are more than 8 permits?
--Jeff Moden
Change is inevitable... Change for the better is not.
February 14, 2011 at 8:20 pm
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...
February 14, 2011 at 9:01 pm
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
Change is inevitable... Change for the better is not.
February 14, 2011 at 9:16 pm
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....
February 14, 2011 at 10:00 pm
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
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply