lsmyth1717
Programmer
I have a query about writing code to use a sql server to loop through a record set in one table and then depending on the record insert a row into a new table. Each night I will receive a merge table file with new values in it. Depending on these values I will want to insert a record across to a new Table called Move. Some nights I might want to update values in Move based on a new row which I'll receive in the Merge Table. Below is a working example of what I have to do. Can someone please help me finish writing the code for the cursor.
MONDAY NIGHT
MERGE TABLE
From To
100 200
200 300
Need this to be inserted into Move as follows: -
MOVE TABLE
From To
100 300
200 300
(As you can see the from row with 100 has had its to value updated)
TUESDAY NIGHT
MERGE TABLE
From To
300 100
Don't want to bring this row accross because 300 already exists as a ToURN in the Move table so want to ignore this row and not insert it.
MOVE TABLE
From To
100 300
200 300
(No exta row inserted)
WEDNESDAY NIGHT
MERGE TABLE
From To
Null 100
100 Null
Null Null
Null 0
0 100
100 0
0 0
100 300 (already exists so don't insert it)
If I get any of these combinations above I don't want to insert the record
MOVE TABLE
From To
100 300
200 300
(No exta row inserted)
THURSDAY
MERGE TABLE
From To
200 400
If I get a row with a From value the same as one which exists in the Move table and the to value for that row doesn't exist as a from value in the Move table then I would like to update the row with the correct value.
MOVE TABLE
From To
100 300
200 400
(No exta row inserted)
My starter cursor looks as follows: -
Declare @FromURN bigint
Declare @ToURN bigint
Declare MoveCursor CURSOR FOR
--This gets all the rows which don't have a null or don't have a 0 and
--MergeFromURN and MergeToURN don't exist in the Move Table
Select MergeFromURN, MergeToURN from Merge where MergeFromURN is not null and MergeToURN is not null
and MergeFromURN <> 0 and MergeToURN <> 0 and
MergeFromURN <> MergeToURN and
(MergeFromURN not in (select MoveFromURN from Move) and
MergeToURN not in (select MoveToURN from Move))
Open MoveCursor
Fetch Next From MoveCursor INTO @FromURN, @ToURN
While @@Fetch_Status = 0
BEGIN
--Never used a cursor before and am not sure what I need to do here to get my
--to code to loop around each record amd do the necessary checking and inserting
--or updating a row as appropriate.
FETCH NEXT FROM MoveCursor
INTO @FromURN, @ToURN
END
Close MoveCursor
Deallocate MoveCursor
Here our the tables I'll be using. Each night the Merge FROM URN is cleared out.
CREATE TABLE [dbo].[MERGE] (
[RecNo] [int] NULL ,
[MergeFromURN] [bigint] NULL ,
[MergeToURN] [bigint] NULL ,
[MergeDateMerged] [datetime] NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[MOVE] (
[RecNO] [bigint] NULL ,
[MoveFromURN] [bigint] NULL ,
[MoveToURN] [bigint] NULL ,
[MoveDateMerged] [datetime] NULL ,
[Timestamp] [datetime] NULL
) ON [PRIMARY]
MONDAY NIGHT
MERGE TABLE
From To
100 200
200 300
Need this to be inserted into Move as follows: -
MOVE TABLE
From To
100 300
200 300
(As you can see the from row with 100 has had its to value updated)
TUESDAY NIGHT
MERGE TABLE
From To
300 100
Don't want to bring this row accross because 300 already exists as a ToURN in the Move table so want to ignore this row and not insert it.
MOVE TABLE
From To
100 300
200 300
(No exta row inserted)
WEDNESDAY NIGHT
MERGE TABLE
From To
Null 100
100 Null
Null Null
Null 0
0 100
100 0
0 0
100 300 (already exists so don't insert it)
If I get any of these combinations above I don't want to insert the record
MOVE TABLE
From To
100 300
200 300
(No exta row inserted)
THURSDAY
MERGE TABLE
From To
200 400
If I get a row with a From value the same as one which exists in the Move table and the to value for that row doesn't exist as a from value in the Move table then I would like to update the row with the correct value.
MOVE TABLE
From To
100 300
200 400
(No exta row inserted)
My starter cursor looks as follows: -
Declare @FromURN bigint
Declare @ToURN bigint
Declare MoveCursor CURSOR FOR
--This gets all the rows which don't have a null or don't have a 0 and
--MergeFromURN and MergeToURN don't exist in the Move Table
Select MergeFromURN, MergeToURN from Merge where MergeFromURN is not null and MergeToURN is not null
and MergeFromURN <> 0 and MergeToURN <> 0 and
MergeFromURN <> MergeToURN and
(MergeFromURN not in (select MoveFromURN from Move) and
MergeToURN not in (select MoveToURN from Move))
Open MoveCursor
Fetch Next From MoveCursor INTO @FromURN, @ToURN
While @@Fetch_Status = 0
BEGIN
--Never used a cursor before and am not sure what I need to do here to get my
--to code to loop around each record amd do the necessary checking and inserting
--or updating a row as appropriate.
FETCH NEXT FROM MoveCursor
INTO @FromURN, @ToURN
END
Close MoveCursor
Deallocate MoveCursor
Here our the tables I'll be using. Each night the Merge FROM URN is cleared out.
CREATE TABLE [dbo].[MERGE] (
[RecNo] [int] NULL ,
[MergeFromURN] [bigint] NULL ,
[MergeToURN] [bigint] NULL ,
[MergeDateMerged] [datetime] NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[MOVE] (
[RecNO] [bigint] NULL ,
[MoveFromURN] [bigint] NULL ,
[MoveToURN] [bigint] NULL ,
[MoveDateMerged] [datetime] NULL ,
[Timestamp] [datetime] NULL
) ON [PRIMARY]