Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
Outer join non è supportato nelle stored procedure compilate in modo nativo. L'esempio seguente mostra un modo per implementare la funzionalità di un 'left outer join' in una stored procedure compilata in modo nativo.
Negli esempi viene utilizzata una variabile di tabella per simulare un cursore sul lato sinistro del join e una variabile di tabella per costruire un singolo set di risultati, adatto solo quando si elabora un numero limitato di righe perché comporta la creazione di una copia aggiuntiva delle righe di dati.
Una variabile (@outer) di tipo t1_type viene usata per scorrere le righe di t1, usando un ciclo while per simulare un cursore. La variabile @result di tipo t1t2_join_type viene quindi usata per costruire il set di risultati.
È consigliabile testare le prestazioni di questa soluzione alternativa per assicurarsi che funzioni come previsto nell'applicazione.
-- original query:
select
t1.c1 as t1c1,
t1.c2 as t1c2,
t2.c2 as t2c2,
t2.c3 as t2c3
from t1 left join t2 on t1.c2=t2.c3
GO
create table dbo.t1
(c1 int not null primary key nonclustered,
c2 int not null) with (memory_optimized=on)
create table dbo.t2
(c2 int not null primary key nonclustered,
c3 int not null) with (memory_optimized=on)
INSERT t1 VALUES (1,2)
INSERT t1 VALUES (2,3)
INSERT t1 VALUES (3,2)
INSERT t2 VALUES (2,3)
INSERT t2 VALUES (4,3)
GO
create type dbo.t1_type as table
(
id int identity not null primary key nonclustered hash with (bucket_count=1024),
c1 int,
c2 int
) with (memory_optimized=on)
GO
create type dbo.t1t2_join_type as table
(
t1c1 int not null index ix_t1c1,
t1c2 int not null,
t2c2 int,
t2c3 int
) with (memory_optimized=on)
GO
-- ====== scenario: generic left join
-- stored procedure including the workaround
create procedure dbo.usp_left_join
with native_compilation, execute as owner, schemabinding
as
begin atomic with (transaction isolation level = snapshot, language = N'us_english')
DECLARE @outer dbo.t1_type
DECLARE @result dbo.t1t2_join_type
-- populate the variable used for iterating over the outer rows
INSERT @outer(c1, c2) select c1,c2 from dbo.t1
DECLARE @i int = 1
DECLARE @max int = scope_identity()
DECLARE @t1c1 int
DECLARE @t1c2 int
while @i <= @max
begin
select @t1c1 = c1, @t1c2 = c2 from @outer where id = @i
INSERT @result select @t1c1, @t1c2, c2, c3 from dbo.t2 where c3 = @t1c2
if @@rowcount = 0
INSERT @result (t1c1, t1c2) VALUES (@t1c1, @t1c2)
set @i += 1
end
select
t1c1,
t1c2,
t2c2,
t2c3
from @result
end
GO
exec dbo.usp_left_join
Vedere anche
Problemi di migrazione relativi alle stored procedure compilate in modo nativo
Costrutti Transact-SQL non supportati da OLTP in-memory