Ciao a tutti...
Se abbiamo due tabelle con chiavi composite:
CREATE TABLE [dbo].[tbl1](
[c1] [int] IDENTITY(1,1) NOT NULL,
[c2] [int] NOT NULL,
[c3] [int] NOT NULL,
[datains] [datetime] NULL DEFAULT (getdate()),
CONSTRAINT [PK_tbl1] PRIMARY KEY CLUSTERED
(
[c2] ASC,
[c3] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[tbl2](
[c1] [int] IDENTITY(1,1) NOT NULL,
[c2] [int] NOT NULL,
[c3] [int] NOT NULL,
[datains] [datetime] NULL DEFAULT (getdate()),
CONSTRAINT [PK_tbl2] PRIMARY KEY CLUSTERED
(
[c2] ASC,
[c3] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
popolate con dati tipo:
insert into tbl1 (c2, c3) values (1, 1)
insert into tbl1 (c2, c3) values (1, 2)
insert into tbl1 (c2, c3) values (1, 3)
insert into tbl1 (c2, c3) values (1, 4)
insert into tbl1 (c2, c3) values (1, 5)
insert into tbl1 (c2, c3) values (2, 1)
insert into tbl1 (c2, c3) values (3, 1)
insert into tbl1 (c2, c3) values (3, 2)
insert into tbl1 (c2, c3) values (4, 1)
insert into tbl1 (c2, c3) values (5, 1)
insert into tbl2 (c2, c3) values (1, 6)
insert into tbl2 (c2, c3) values (1, 1)
insert into tbl2 (c2, c3) values (1, 5)
insert into tbl2 (c2, c3) values (2, 1)
insert into tbl2 (c2, c3) values (2, 2)
insert into tbl2 (c2, c3) values (3, 2)
insert into tbl2 (c2, c3) values (4, 3)
insert into tbl2 (c2, c3) values (4, 4)
insert into tbl2 (c2, c3) values (4, 5)
insert into tbl2 (c2, c3) values (5, 1)
insert into tbl2 (c2, c3) values (7, 1)
insert into tbl2 (c2, c3) values (3, 3)
A PARITA' DI CHIAVE PRIMARIA... come troviamo tutte le righe della tabella tbl1 che non sono presenti nella tabella tbl2?
Io lo risolvo cosi'...
select * from tbl1
where cast(c2 as varchar(4)) +'-'+ cast(c3 as varchar(4))
not in (select cast(c2 as varchar(4)) +'-'+ cast(c3 as varchar(4)) from tbl2)
ma ho il dubbio che non sia proprio la via migliore....
Soprattutto perche' devo andare a castare tutto, compresi i numerici...
Dove e' che sbaglio?
Ciao
Ale