Hello, SQL Gurus, I am trying to use the case statement in the following
logic and just can't seem to get it to work. Any help would be greatly
appreciated. Thanks a bunch.
INSERT INTO dbo.match_table
SELECT
a.[Glsubact], a.[Dcoscat], [a.Revreduc],a.[Custtype], b.[Tier2] , b.
New_entry, a.PRODTYPE
from BAL_table a JOIN Tier2_table b
ON
b.Custtype = a.Custtype
and b. ICPROFCT = a.ICPROFCT
and ( substring(b.Profctr,1,2)+ '00' ) = a.Profctr
CASE WHEN(PRODTYPE not in ('01','04') and New_entry = 'N') Then
set TIER2 = 'C092'
Else
set TIER2 = 'C094'
EndCould you shed some light on what you are trying to accomplish with the CASE
expression? I can't tell from your post code. Is it supposed to be part of
the SELECT statement (perhaps in its where clause) or part of the join
condition?
Note that CASE ... END is an expression in T-SQL, not a statement. As an
expression, you can't enclose a T-SQL statement in it.
Linchi
"mbouck" wrote:
> Hello, SQL Gurus, I am trying to use the case statement in the following
> logic and just can't seem to get it to work. Any help would be greatly
> appreciated. Thanks a bunch.
>
> INSERT INTO dbo.match_table
> SELECT
> a.[Glsubact], a.[Dcoscat], [a.Revreduc],a.[Custtype], b.[Tier2] , b.
> New_entry, a.PRODTYPE
> from BAL_table a JOIN Tier2_table b
> ON
> b.Custtype = a.Custtype
> and b. ICPROFCT = a.ICPROFCT
> and ( substring(b.Profctr,1,2)+ '00' ) = a.Profctr
> CASE WHEN(PRODTYPE not in ('01','04') and New_entry = 'N') Then
> set TIER2 = 'C092'
> Else
> set TIER2 = 'C094'
> End
>|||Hi
Some ideas
http://dimantdatabasesolutions.blogspot.com/2007/02/some-case-expression-techniques.html
"mbouck" <u32265@.uwe> wrote in message news:6ec0823937b99@.uwe...
> Hello, SQL Gurus, I am trying to use the case statement in the following
> logic and just can't seem to get it to work. Any help would be greatly
> appreciated. Thanks a bunch.
>
> INSERT INTO dbo.match_table
> SELECT
> a.[Glsubact], a.[Dcoscat], [a.Revreduc],a.[Custtype], b.[Tier2] , b.
> New_entry, a.PRODTYPE
> from BAL_table a JOIN Tier2_table b
> ON
> b.Custtype = a.Custtype
> and b. ICPROFCT = a.ICPROFCT
> and ( substring(b.Profctr,1,2)+ '00' ) = a.Profctr
> CASE WHEN(PRODTYPE not in ('01','04') and New_entry = 'N') Then
> set TIER2 = 'C092'
> Else
> set TIER2 = 'C094'
> End
>|||Hi Uri, the case examples that you have posted are very helpful, I will try
them. thanks for the help.
Uri Dimant wrote:
>Hi
>Some ideas
>http://dimantdatabasesolutions.blogspot.com/2007/02/some-case-expression-techniques.html
>> Hello, SQL Gurus, I am trying to use the case statement in the following
>> logic and just can't seem to get it to work. Any help would be greatly
>[quoted text clipped - 20 lines]
>> End
--
Message posted via SQLMonster.com
http://www.sqlmonster.com/Uwe/Forums.aspx/sql-server/200703/1|||Hi Linchi, thank you for responding back and the guidance. I'll try rewriting
it again.
Linchi Shea wrote:
>Could you shed some light on what you are trying to accomplish with the CASE
>expression? I can't tell from your post code. Is it supposed to be part of
>the SELECT statement (perhaps in its where clause) or part of the join
>condition?
>Note that CASE ... END is an expression in T-SQL, not a statement. As an
>expression, you can't enclose a T-SQL statement in it.
>Linchi
>> Hello, SQL Gurus, I am trying to use the case statement in the following
>> logic and just can't seem to get it to work. Any help would be greatly
>[quoted text clipped - 20 lines]
>> End
--
Message posted via http://www.sqlmonster.com
Showing posts with label logic. Show all posts
Showing posts with label logic. Show all posts
Friday, March 23, 2012
Monday, March 12, 2012
Insert Statement logic
I have a problem trying to work out the insert logic for an insert statement
which inserts the rows from one table (Parcel1) into another table (Parcel2)
.
Bare with me for this as I know this mightn't people might say why are you
doing this but its just an example i've drew up. Basically my problem is
that the first time I run an insert statement everything works fine. When I
run the insert statement the second time though the same rows are inserted
into the second table.
What i want to add to my insert statement is something which says if the
fields oneParcel1 and oneParcel2 for a particular row have the same values i
n
the twoparcel1 and twoparcel2 fields as a row which already exists in the
Parcel2 table then don't insert the records. So If a row in Parcel1 has a
row which has car and truck in the table parcel2 regardless of the other
fields value don't insert it.
CREATE TABLE [dbo].[Parcel1] (
[RecNo] [int] NULL ,
[oneParcel1] [bigint] NULL ,
[oneParcel2] [bigint] NULL ,
[Date] [datetime] NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Parcel2] (
[RecNO] [bigint] NULL ,
[twoParcel1] [bigint] NULL ,
[twoParcel2] [bigint] NULL ,
[Date] [datetime] NULL
) ON [PRIMARY]
insert into Parcel1 values(1, 'car', 'bus', '15/06/1982')
insert into Parcel1 values(2, 'bus', 'train', '15/06/1982')
insert into Parcel1 values(3, 'truck', 'car', '15/06/1982')
insert into Parcel1 values(4, 'car', 'truck', '15/06/1982')
insert into Parcel1 values(5, 'truck', 'plane', '15/06/1982')
Table: Parcel1
RecNo oneParcel1 oneParcel2 Date
1 car bus 15/06/1982
2 bus train 15/06/1982
3 truck car 15/06/1982
4 car truck 15/06/1982
5 truck boat 15/06/1982
After 1st insert. I want this. and if I run the insert statement again I
don't want these rows to be inserted again
Table: Parcel2
RecNo twoParcel1 twoParcel2 Date
1 car bus 15/06/1982
2 bus train 15/06/1982
3 truck car 15/06/1982
4 car truck 15/06/1982
5 truck boat 15/06/1982Stephen
Have you even checked your DDL before posting it?
CREATE TABLE [dbo].[Parcel1] (
[RecNo] [int] NULL ,
[oneParcel1] varchar(15) NULL ,
[oneParcel2] varchar(15) NULL ,
[Date] [datetime] NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Parcel2] (
[RecNO] [bigint] NULL ,
[twoParcel1] varchar(15) NULL ,
[twoParcel2] varchar(15) NULL ,
[Date] [datetime] NULL
) ON [PRIMARY]
insert into Parcel1 values(1, 'car', 'bus', '19820615')
insert into Parcel1 values(2, 'bus', 'train', '19820615')
insert into Parcel1 values(3, 'truck', 'car', '19820615')
insert into Parcel1 values(4, 'car', 'truck', '19820615')
insert into Parcel1 values(5, 'truck', 'plane', '19820615')
INSERT INTO Parcel2 SELECT * FROM Parcel1 WHERE NOT EXISTS
(
SELECT * FROM Parcel2 P WHERE P.RecNo=Parcel1.RecNo
)
"Stephen" <Stephen@.discussions.microsoft.com> wrote in message
news:E956A4AA-4D55-4056-AA86-4391054E53BE@.microsoft.com...
>I have a problem trying to work out the insert logic for an insert
>statement
> which inserts the rows from one table (Parcel1) into another table
> (Parcel2).
>
> Bare with me for this as I know this mightn't people might say why are you
> doing this but its just an example i've drew up. Basically my problem is
> that the first time I run an insert statement everything works fine. When
> I
> run the insert statement the second time though the same rows are inserted
> into the second table.
> What i want to add to my insert statement is something which says if the
> fields oneParcel1 and oneParcel2 for a particular row have the same values
> in
> the twoparcel1 and twoparcel2 fields as a row which already exists in the
> Parcel2 table then don't insert the records. So If a row in Parcel1 has a
> row which has car and truck in the table parcel2 regardless of the other
> fields value don't insert it.
> CREATE TABLE [dbo].[Parcel1] (
> [RecNo] [int] NULL ,
> [oneParcel1] [bigint] NULL ,
> [oneParcel2] [bigint] NULL ,
> [Date] [datetime] NULL
> ) ON [PRIMARY]
> GO
> CREATE TABLE [dbo].[Parcel2] (
> [RecNO] [bigint] NULL ,
> [twoParcel1] [bigint] NULL ,
> [twoParcel2] [bigint] NULL ,
> [Date] [datetime] NULL
> ) ON [PRIMARY]
> insert into Parcel1 values(1, 'car', 'bus', '15/06/1982')
> insert into Parcel1 values(2, 'bus', 'train', '15/06/1982')
> insert into Parcel1 values(3, 'truck', 'car', '15/06/1982')
> insert into Parcel1 values(4, 'car', 'truck', '15/06/1982')
> insert into Parcel1 values(5, 'truck', 'plane', '15/06/1982')
> Table: Parcel1
> RecNo oneParcel1 oneParcel2 Date
> 1 car bus 15/06/1982
> 2 bus train 15/06/1982
> 3 truck car 15/06/1982
> 4 car truck 15/06/1982
> 5 truck boat 15/06/1982
> After 1st insert. I want this. and if I run the insert statement again I
> don't want these rows to be inserted again
> Table: Parcel2
> RecNo twoParcel1 twoParcel2 Date
> 1 car bus 15/06/1982
> 2 bus train 15/06/1982
> 3 truck car 15/06/1982
> 4 car truck 15/06/1982
> 5 truck boat 15/06/1982
>|||2 ways
1. Check for the existence of a duplicate before inserting
2. Keep a unique index with IGNORE DUPLICATE option set for Parcel2 table
Rakesh
"Stephen" wrote:
> I have a problem trying to work out the insert logic for an insert stateme
nt
> which inserts the rows from one table (Parcel1) into another table (Parcel
2).
>
> Bare with me for this as I know this mightn't people might say why are you
> doing this but its just an example i've drew up. Basically my problem is
> that the first time I run an insert statement everything works fine. When
I
> run the insert statement the second time though the same rows are inserted
> into the second table.
> What i want to add to my insert statement is something which says if the
> fields oneParcel1 and oneParcel2 for a particular row have the same values
in
> the twoparcel1 and twoparcel2 fields as a row which already exists in the
> Parcel2 table then don't insert the records. So If a row in Parcel1 has a
> row which has car and truck in the table parcel2 regardless of the other
> fields value don't insert it.
> CREATE TABLE [dbo].[Parcel1] (
> [RecNo] [int] NULL ,
> [oneParcel1] [bigint] NULL ,
> [oneParcel2] [bigint] NULL ,
> [Date] [datetime] NULL
> ) ON [PRIMARY]
> GO
> CREATE TABLE [dbo].[Parcel2] (
> [RecNO] [bigint] NULL ,
> [twoParcel1] [bigint] NULL ,
> [twoParcel2] [bigint] NULL ,
> [Date] [datetime] NULL
> ) ON [PRIMARY]
> insert into Parcel1 values(1, 'car', 'bus', '15/06/1982')
> insert into Parcel1 values(2, 'bus', 'train', '15/06/1982')
> insert into Parcel1 values(3, 'truck', 'car', '15/06/1982')
> insert into Parcel1 values(4, 'car', 'truck', '15/06/1982')
> insert into Parcel1 values(5, 'truck', 'plane', '15/06/1982')
> Table: Parcel1
> RecNo oneParcel1 oneParcel2 Date
> 1 car bus 15/06/1982
> 2 bus train 15/06/1982
> 3 truck car 15/06/1982
> 4 car truck 15/06/1982
> 5 truck boat 15/06/1982
> After 1st insert. I want this. and if I run the insert statement again I
> don't want these rows to be inserted again
> Table: Parcel2
> RecNo twoParcel1 twoParcel2 Date
> 1 car bus 15/06/1982
> 2 bus train 15/06/1982
> 3 truck car 15/06/1982
> 4 car truck 15/06/1982
> 5 truck boat 15/06/1982
>|||Try:
INSERT INTO Parcel2 (recno, twoparcel1, twoparcel2, [date])
SELECT recno, oneparcel1, oneparcel2, [date]
FROM Parcel1 AS P1
WHERE NOT EXISTS
(SELECT *
FROM Parcel2 AS P2
WHERE P2.twoparcel1 = P1.oneparcel1
AND P2.twoparcel2 = P1.oneparcel2) ;
Some other things need more attention. First, you don't have a key in
either table! All the columns are nullable, which means both tables
lack any integrity. "Date" is a reserved word and much too vague for a
column name. "RecNo" is not a good identifier in a relational database.
Conventional wisdom has it that tables have rows, not records. Rows are
not numbered and surrogate keys are not "record numbers". BIGINT
appears to be a mistake anyway but are you really expecting more than 2
billion rows in these tables?
Thanks for including the DDL but do remember that keys, constraints and
accurate datatypes are important if you want accurate answers.
Hope this helps.
David Portas
SQL Server MVP
--|||Small addition to what i said
2 ways
1. Check for the existence of a duplicate before inserting
2. Keep a unique index on columns twoParcel1, twoParcel2 with IGNORE
DUPLICATE option set for Parcel2 table. This will let u do the insert
statements as u hv been doing and ignoring any duplicate values without
giving an error.
"Rakesh" wrote:
> 2 ways
> 1. Check for the existence of a duplicate before inserting
> 2. Keep a unique index with IGNORE DUPLICATE option set for Parcel2 table
> Rakesh
> "Stephen" wrote:
>|||> 2. Keep a unique index on columns twoParcel1, twoParcel2 with IGNORE
> DUPLICATE option set for Parcel2 table. This will let u do the insert
> statements as u hv been doing and ignoring any duplicate values without
> giving an error.
Be very, very careful with the IGNORE_DUP_KEY option. I would consider
it suitable for a staging database in special circumstances only - not
for a live database with actual users, queries and updates running on
it.
The reason is that IGNORE_DUP_KEY confounds set-based inserts because
by giving a non-deterministic result in the presence of duplicates. In
short you cannot know or control which rows(s) get inserted and which
get discarded. Of course, if all your developers are aware that this
option has been set then in principle they can safely code around it -
but if they are going to do that anyway then why use it? Just add the
existence check to the INSERT statements instead.
David Portas
SQL Server MVP
--|||Sorry never checked it correct your guess was correct to what it should have
been.
I know this is going to sound awkward but I know who to do it that way but
i'm trying to ignore the RecNO and work out how to only insert records where
Parcel1.oneParcel1 = Parcel2.twoParcel1 AND
Parcel1.oneParcel2 = Parcel2.twoParcel2
I know this probably doesn't make sense and its hard to explain but i want
the sql to not allow records to be inserted when the above conditions are
both true. In other words forgetting about the recno and date if a row in th
e
Parcel 1 table has the values
RecNo oneParcel1 oneParcel2 Date
1 car bus 15/06/1982
and the same row is present in the Parcel 2 table like this
RecNo twoParcel1 twoParcel2 Date
1 car bus 15/06/1982
When a row like this comes along in the Parcel 1 table I don't want it
inserted because the car and buss secenario has already been inserted.
RecNo oneParcel1 oneParcel2 Date
6 car bus 01/11/1983
"Uri Dimant" wrote:
> Stephen
> Have you even checked your DDL before posting it?
> CREATE TABLE [dbo].[Parcel1] (
> [RecNo] [int] NULL ,
> [oneParcel1] varchar(15) NULL ,
> [oneParcel2] varchar(15) NULL ,
> [Date] [datetime] NULL
> ) ON [PRIMARY]
> GO
> CREATE TABLE [dbo].[Parcel2] (
> [RecNO] [bigint] NULL ,
> [twoParcel1] varchar(15) NULL ,
> [twoParcel2] varchar(15) NULL ,
> [Date] [datetime] NULL
> ) ON [PRIMARY]
> insert into Parcel1 values(1, 'car', 'bus', '19820615')
> insert into Parcel1 values(2, 'bus', 'train', '19820615')
> insert into Parcel1 values(3, 'truck', 'car', '19820615')
> insert into Parcel1 values(4, 'car', 'truck', '19820615')
> insert into Parcel1 values(5, 'truck', 'plane', '19820615')
>
> INSERT INTO Parcel2 SELECT * FROM Parcel1 WHERE NOT EXISTS
> (
> SELECT * FROM Parcel2 P WHERE P.RecNo=Parcel1.RecNo
> )
>
>
> "Stephen" <Stephen@.discussions.microsoft.com> wrote in message
> news:E956A4AA-4D55-4056-AA86-4391054E53BE@.microsoft.com...
>
>
which inserts the rows from one table (Parcel1) into another table (Parcel2)
.
Bare with me for this as I know this mightn't people might say why are you
doing this but its just an example i've drew up. Basically my problem is
that the first time I run an insert statement everything works fine. When I
run the insert statement the second time though the same rows are inserted
into the second table.
What i want to add to my insert statement is something which says if the
fields oneParcel1 and oneParcel2 for a particular row have the same values i
n
the twoparcel1 and twoparcel2 fields as a row which already exists in the
Parcel2 table then don't insert the records. So If a row in Parcel1 has a
row which has car and truck in the table parcel2 regardless of the other
fields value don't insert it.
CREATE TABLE [dbo].[Parcel1] (
[RecNo] [int] NULL ,
[oneParcel1] [bigint] NULL ,
[oneParcel2] [bigint] NULL ,
[Date] [datetime] NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Parcel2] (
[RecNO] [bigint] NULL ,
[twoParcel1] [bigint] NULL ,
[twoParcel2] [bigint] NULL ,
[Date] [datetime] NULL
) ON [PRIMARY]
insert into Parcel1 values(1, 'car', 'bus', '15/06/1982')
insert into Parcel1 values(2, 'bus', 'train', '15/06/1982')
insert into Parcel1 values(3, 'truck', 'car', '15/06/1982')
insert into Parcel1 values(4, 'car', 'truck', '15/06/1982')
insert into Parcel1 values(5, 'truck', 'plane', '15/06/1982')
Table: Parcel1
RecNo oneParcel1 oneParcel2 Date
1 car bus 15/06/1982
2 bus train 15/06/1982
3 truck car 15/06/1982
4 car truck 15/06/1982
5 truck boat 15/06/1982
After 1st insert. I want this. and if I run the insert statement again I
don't want these rows to be inserted again
Table: Parcel2
RecNo twoParcel1 twoParcel2 Date
1 car bus 15/06/1982
2 bus train 15/06/1982
3 truck car 15/06/1982
4 car truck 15/06/1982
5 truck boat 15/06/1982Stephen
Have you even checked your DDL before posting it?
CREATE TABLE [dbo].[Parcel1] (
[RecNo] [int] NULL ,
[oneParcel1] varchar(15) NULL ,
[oneParcel2] varchar(15) NULL ,
[Date] [datetime] NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Parcel2] (
[RecNO] [bigint] NULL ,
[twoParcel1] varchar(15) NULL ,
[twoParcel2] varchar(15) NULL ,
[Date] [datetime] NULL
) ON [PRIMARY]
insert into Parcel1 values(1, 'car', 'bus', '19820615')
insert into Parcel1 values(2, 'bus', 'train', '19820615')
insert into Parcel1 values(3, 'truck', 'car', '19820615')
insert into Parcel1 values(4, 'car', 'truck', '19820615')
insert into Parcel1 values(5, 'truck', 'plane', '19820615')
INSERT INTO Parcel2 SELECT * FROM Parcel1 WHERE NOT EXISTS
(
SELECT * FROM Parcel2 P WHERE P.RecNo=Parcel1.RecNo
)
"Stephen" <Stephen@.discussions.microsoft.com> wrote in message
news:E956A4AA-4D55-4056-AA86-4391054E53BE@.microsoft.com...
>I have a problem trying to work out the insert logic for an insert
>statement
> which inserts the rows from one table (Parcel1) into another table
> (Parcel2).
>
> Bare with me for this as I know this mightn't people might say why are you
> doing this but its just an example i've drew up. Basically my problem is
> that the first time I run an insert statement everything works fine. When
> I
> run the insert statement the second time though the same rows are inserted
> into the second table.
> What i want to add to my insert statement is something which says if the
> fields oneParcel1 and oneParcel2 for a particular row have the same values
> in
> the twoparcel1 and twoparcel2 fields as a row which already exists in the
> Parcel2 table then don't insert the records. So If a row in Parcel1 has a
> row which has car and truck in the table parcel2 regardless of the other
> fields value don't insert it.
> CREATE TABLE [dbo].[Parcel1] (
> [RecNo] [int] NULL ,
> [oneParcel1] [bigint] NULL ,
> [oneParcel2] [bigint] NULL ,
> [Date] [datetime] NULL
> ) ON [PRIMARY]
> GO
> CREATE TABLE [dbo].[Parcel2] (
> [RecNO] [bigint] NULL ,
> [twoParcel1] [bigint] NULL ,
> [twoParcel2] [bigint] NULL ,
> [Date] [datetime] NULL
> ) ON [PRIMARY]
> insert into Parcel1 values(1, 'car', 'bus', '15/06/1982')
> insert into Parcel1 values(2, 'bus', 'train', '15/06/1982')
> insert into Parcel1 values(3, 'truck', 'car', '15/06/1982')
> insert into Parcel1 values(4, 'car', 'truck', '15/06/1982')
> insert into Parcel1 values(5, 'truck', 'plane', '15/06/1982')
> Table: Parcel1
> RecNo oneParcel1 oneParcel2 Date
> 1 car bus 15/06/1982
> 2 bus train 15/06/1982
> 3 truck car 15/06/1982
> 4 car truck 15/06/1982
> 5 truck boat 15/06/1982
> After 1st insert. I want this. and if I run the insert statement again I
> don't want these rows to be inserted again
> Table: Parcel2
> RecNo twoParcel1 twoParcel2 Date
> 1 car bus 15/06/1982
> 2 bus train 15/06/1982
> 3 truck car 15/06/1982
> 4 car truck 15/06/1982
> 5 truck boat 15/06/1982
>|||2 ways
1. Check for the existence of a duplicate before inserting
2. Keep a unique index with IGNORE DUPLICATE option set for Parcel2 table
Rakesh
"Stephen" wrote:
> I have a problem trying to work out the insert logic for an insert stateme
nt
> which inserts the rows from one table (Parcel1) into another table (Parcel
2).
>
> Bare with me for this as I know this mightn't people might say why are you
> doing this but its just an example i've drew up. Basically my problem is
> that the first time I run an insert statement everything works fine. When
I
> run the insert statement the second time though the same rows are inserted
> into the second table.
> What i want to add to my insert statement is something which says if the
> fields oneParcel1 and oneParcel2 for a particular row have the same values
in
> the twoparcel1 and twoparcel2 fields as a row which already exists in the
> Parcel2 table then don't insert the records. So If a row in Parcel1 has a
> row which has car and truck in the table parcel2 regardless of the other
> fields value don't insert it.
> CREATE TABLE [dbo].[Parcel1] (
> [RecNo] [int] NULL ,
> [oneParcel1] [bigint] NULL ,
> [oneParcel2] [bigint] NULL ,
> [Date] [datetime] NULL
> ) ON [PRIMARY]
> GO
> CREATE TABLE [dbo].[Parcel2] (
> [RecNO] [bigint] NULL ,
> [twoParcel1] [bigint] NULL ,
> [twoParcel2] [bigint] NULL ,
> [Date] [datetime] NULL
> ) ON [PRIMARY]
> insert into Parcel1 values(1, 'car', 'bus', '15/06/1982')
> insert into Parcel1 values(2, 'bus', 'train', '15/06/1982')
> insert into Parcel1 values(3, 'truck', 'car', '15/06/1982')
> insert into Parcel1 values(4, 'car', 'truck', '15/06/1982')
> insert into Parcel1 values(5, 'truck', 'plane', '15/06/1982')
> Table: Parcel1
> RecNo oneParcel1 oneParcel2 Date
> 1 car bus 15/06/1982
> 2 bus train 15/06/1982
> 3 truck car 15/06/1982
> 4 car truck 15/06/1982
> 5 truck boat 15/06/1982
> After 1st insert. I want this. and if I run the insert statement again I
> don't want these rows to be inserted again
> Table: Parcel2
> RecNo twoParcel1 twoParcel2 Date
> 1 car bus 15/06/1982
> 2 bus train 15/06/1982
> 3 truck car 15/06/1982
> 4 car truck 15/06/1982
> 5 truck boat 15/06/1982
>|||Try:
INSERT INTO Parcel2 (recno, twoparcel1, twoparcel2, [date])
SELECT recno, oneparcel1, oneparcel2, [date]
FROM Parcel1 AS P1
WHERE NOT EXISTS
(SELECT *
FROM Parcel2 AS P2
WHERE P2.twoparcel1 = P1.oneparcel1
AND P2.twoparcel2 = P1.oneparcel2) ;
Some other things need more attention. First, you don't have a key in
either table! All the columns are nullable, which means both tables
lack any integrity. "Date" is a reserved word and much too vague for a
column name. "RecNo" is not a good identifier in a relational database.
Conventional wisdom has it that tables have rows, not records. Rows are
not numbered and surrogate keys are not "record numbers". BIGINT
appears to be a mistake anyway but are you really expecting more than 2
billion rows in these tables?
Thanks for including the DDL but do remember that keys, constraints and
accurate datatypes are important if you want accurate answers.
Hope this helps.
David Portas
SQL Server MVP
--|||Small addition to what i said
2 ways
1. Check for the existence of a duplicate before inserting
2. Keep a unique index on columns twoParcel1, twoParcel2 with IGNORE
DUPLICATE option set for Parcel2 table. This will let u do the insert
statements as u hv been doing and ignoring any duplicate values without
giving an error.
"Rakesh" wrote:
> 2 ways
> 1. Check for the existence of a duplicate before inserting
> 2. Keep a unique index with IGNORE DUPLICATE option set for Parcel2 table
> Rakesh
> "Stephen" wrote:
>|||> 2. Keep a unique index on columns twoParcel1, twoParcel2 with IGNORE
> DUPLICATE option set for Parcel2 table. This will let u do the insert
> statements as u hv been doing and ignoring any duplicate values without
> giving an error.
Be very, very careful with the IGNORE_DUP_KEY option. I would consider
it suitable for a staging database in special circumstances only - not
for a live database with actual users, queries and updates running on
it.
The reason is that IGNORE_DUP_KEY confounds set-based inserts because
by giving a non-deterministic result in the presence of duplicates. In
short you cannot know or control which rows(s) get inserted and which
get discarded. Of course, if all your developers are aware that this
option has been set then in principle they can safely code around it -
but if they are going to do that anyway then why use it? Just add the
existence check to the INSERT statements instead.
David Portas
SQL Server MVP
--|||Sorry never checked it correct your guess was correct to what it should have
been.
I know this is going to sound awkward but I know who to do it that way but
i'm trying to ignore the RecNO and work out how to only insert records where
Parcel1.oneParcel1 = Parcel2.twoParcel1 AND
Parcel1.oneParcel2 = Parcel2.twoParcel2
I know this probably doesn't make sense and its hard to explain but i want
the sql to not allow records to be inserted when the above conditions are
both true. In other words forgetting about the recno and date if a row in th
e
Parcel 1 table has the values
RecNo oneParcel1 oneParcel2 Date
1 car bus 15/06/1982
and the same row is present in the Parcel 2 table like this
RecNo twoParcel1 twoParcel2 Date
1 car bus 15/06/1982
When a row like this comes along in the Parcel 1 table I don't want it
inserted because the car and buss secenario has already been inserted.
RecNo oneParcel1 oneParcel2 Date
6 car bus 01/11/1983
"Uri Dimant" wrote:
> Stephen
> Have you even checked your DDL before posting it?
> CREATE TABLE [dbo].[Parcel1] (
> [RecNo] [int] NULL ,
> [oneParcel1] varchar(15) NULL ,
> [oneParcel2] varchar(15) NULL ,
> [Date] [datetime] NULL
> ) ON [PRIMARY]
> GO
> CREATE TABLE [dbo].[Parcel2] (
> [RecNO] [bigint] NULL ,
> [twoParcel1] varchar(15) NULL ,
> [twoParcel2] varchar(15) NULL ,
> [Date] [datetime] NULL
> ) ON [PRIMARY]
> insert into Parcel1 values(1, 'car', 'bus', '19820615')
> insert into Parcel1 values(2, 'bus', 'train', '19820615')
> insert into Parcel1 values(3, 'truck', 'car', '19820615')
> insert into Parcel1 values(4, 'car', 'truck', '19820615')
> insert into Parcel1 values(5, 'truck', 'plane', '19820615')
>
> INSERT INTO Parcel2 SELECT * FROM Parcel1 WHERE NOT EXISTS
> (
> SELECT * FROM Parcel2 P WHERE P.RecNo=Parcel1.RecNo
> )
>
>
> "Stephen" <Stephen@.discussions.microsoft.com> wrote in message
> news:E956A4AA-4D55-4056-AA86-4391054E53BE@.microsoft.com...
>
>
Wednesday, March 7, 2012
Insert Record Logic Needed
Im trying to write some sql which inserts these rows into another table
called move. Unfortunately i am struggling trying to get the insert statemen
t
not to insert record 5 because the ToURN has been used before in a previous
record(1). Does anyone know how I would write the sql to do this.
RecNO MergeFromURN MergeToURN MergeDateMerged
1 100 200 15/06/1982
2 200 300 15/06/1982
3 300 400 15/06/1982
4 500 600 15/06/1982
5 700 100 15/06/1982
6 100 100 15/06/1982
7 NULL 100 15/06/1982
8 700 0 15/06/1982
So far I have the following sql but need to go that step further to stop
record 5 being inserted because 100 already has been inserted as a from urn
in record 1.
INSERT INTO MOVE (MOVEFROMURN, MOVETOURN, MOVEDATEMERGED)
SELECT MergeFromURN, MergeToURN, MIN(MergeDateMerged)
from myTable
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))
GROUP BY MergeFromURN, MergeToURN
Order by MergeFromURN
while @.@.ROWCOUNT > 0
begin
update A set MoveToURN = B.MoveToURN
from Move A
inner join Move B on A.MoveToURN=B.MoveFromURN
end
Can anyone help me with this.Are you aware of the fact that at least once several people hav tried to hel
p
you?
Maybe you should read their responses...
http://msdn.microsoft.com/newsgroup...92-6b6386cb16a1
ML
called move. Unfortunately i am struggling trying to get the insert statemen
t
not to insert record 5 because the ToURN has been used before in a previous
record(1). Does anyone know how I would write the sql to do this.
RecNO MergeFromURN MergeToURN MergeDateMerged
1 100 200 15/06/1982
2 200 300 15/06/1982
3 300 400 15/06/1982
4 500 600 15/06/1982
5 700 100 15/06/1982
6 100 100 15/06/1982
7 NULL 100 15/06/1982
8 700 0 15/06/1982
So far I have the following sql but need to go that step further to stop
record 5 being inserted because 100 already has been inserted as a from urn
in record 1.
INSERT INTO MOVE (MOVEFROMURN, MOVETOURN, MOVEDATEMERGED)
SELECT MergeFromURN, MergeToURN, MIN(MergeDateMerged)
from myTable
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))
GROUP BY MergeFromURN, MergeToURN
Order by MergeFromURN
while @.@.ROWCOUNT > 0
begin
update A set MoveToURN = B.MoveToURN
from Move A
inner join Move B on A.MoveToURN=B.MoveFromURN
end
Can anyone help me with this.Are you aware of the fact that at least once several people hav tried to hel
p
you?
Maybe you should read their responses...
http://msdn.microsoft.com/newsgroup...92-6b6386cb16a1
ML
Labels:
database,
insert,
inserts,
logic,
microsoft,
mysql,
oracle,
record,
rows,
server,
sql,
struggling,
tablecalled,
unfortunately,
write
Subscribe to:
Posts (Atom)