Ich habe eine gespeicherte Proc, wo ich die gleiche "nicht existiert" -Klausel wiederholt habe. Jede 'not exists' -Klausel in jedem select wird in den nachfolgenden select-Abfragen wiederholt und die 'exist'-Klausel wird' non exist 'in den nachfolgenden select-Abfragen. Gibt es Möglichkeit, ich kann jedes "nicht existiert" und "existiert" nur einmal und speichern Sie diese Ergebnisse und verwenden Sie sie über die proc? Was ist der optimierte Weg dafür? Vielen Dank!Wie werden repetitive exists und non exists in sql gespeicherten Prozeduren optimiert?
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_GetCaseType]
@ID numeric(18,0)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @cnt int;
SELECT @cnt = COUNT(*)
FROM tblCase c WITH (NOLOCK)
join tblDepartment cd WITH (NOLOCK) on cd.DepartmentID = c.DepartmentID
join tblType ct WITH (NOLOCK) on ct.TypeID = cd.TypeID
WHERE ct.Type in ('AG', 'PH')
and
not exists (SELECT * FROM tblCharge ch WITH (NOLOCK)
join tblAction ca WITH (NOLOCK) on ch.ActionID = ca.ActionID
WHERE ca.Code in ('Z', 'W')
and ch.ID = @ID)
and
c.ID = @ID
IF (@cnt > 1)
RETURN 'CATEGORY 1'
SELECT @cnt = COUNT(*)
FROM tblCase c WITH (NOLOCK)
join tblDepartment cd WITH (NOLOCK) on cd.DepartmentID = c.DepartmentID
join tblType ct WITH (NOLOCK) on ct.TypeID = cd.TypeID
WHERE ct.Type in ('AG', 'PH')
and
not exists (SELECT * FROM tblCharge ch WITH (NOLOCK)
join tblAction ca WITH (NOLOCK) on ch.ActionID = ca.ActionID
WHERE ca.Code in ('Z', 'W')
and ch.ID = @ID)
and
exists (SELECT * FROM tblCharge ch WITH (NOLOCK)
join tblAction ca WITH (NOLOCK) on ch.ActionID = ca.ActionID
WHERE ca.Code in ('N', 'O')
and ch.ID = @ID)
and
c.ID = @ID
IF (@cnt > 1)
RETURN 'CATEGORY 2'
SELECT @cnt = COUNT(*)
FROM tblCase c WITH (NOLOCK)
join tblDepartment cd WITH (NOLOCK) on cd.DepartmentID = c.DepartmentID
join tblType ct WITH (NOLOCK) on ct.TypeID = cd.TypeID
WHERE ct.Type in ('AG', 'PH')
and
not exists (SELECT * FROM tblCharge ch WITH (NOLOCK)
join tblAction ca WITH (NOLOCK) on ch.ActionID = ca.ActionID
WHERE ca.Code in ('Z', 'W')
and ch.ID = @ID)
and
not exists (SELECT * FROM tblCharge ch WITH (NOLOCK)
join tblAction ca WITH (NOLOCK) on ch.ActionID = ca.ActionID
WHERE ca.Code in ('N', 'O')
and ch.ID = @ID)
and
exists (SELECT * FROM tblCharge ch WITH (NOLOCK)
join tblAction ca WITH (NOLOCK) on ch.ActionID = ca.ActionID
WHERE ca.Code in ('D', 'B')
and ch.ID = @ID)
and
c.ID = @ID
IF (@cnt > 1)
RETURN 'CATEGORY 2'
SELECT @cnt = COUNT(*)
FROM tblCase c WITH (NOLOCK)
join tblDepartment cd WITH (NOLOCK) on cd.DepartmentID = c.DepartmentID
join tblType ct WITH (NOLOCK) on ct.TypeID = cd.TypeID
WHERE ct.Type in ('AG', 'PH')
and
not exists (SELECT * FROM tblCharge ch WITH (NOLOCK)
join tblAction ca WITH (NOLOCK) on ch.ActionID = ca.ActionID
WHERE ca.Code in ('Z', 'W')
and ch.ID = @ID)
and
not exists (SELECT * FROM tblCharge ch WITH (NOLOCK)
join tblAction ca WITH (NOLOCK) on ch.ActionID = ca.ActionID
WHERE ca.Code in ('N', 'O')
and ch.ID = @ID)
and
not exists (SELECT * FROM tblCharge ch WITH (NOLOCK)
join tblAction ca WITH (NOLOCK) on ch.ActionID = ca.ActionID
WHERE ca.Code in ('D', 'B')
and ch.ID = @ID)
and
exists (SELECT * FROM tblCharge ch WITH (NOLOCK)
join tblAction ca WITH (NOLOCK) on ch.ActionID = ca.ActionID
WHERE ca.Code in ('A', 'C')
and ch.ID = @ID)
and
c.ID = @ID
IF (@cnt > 1)
RETURN 'CATEGORY 2'
END
Einfach fragen ... wenn Sie es bei den Ausführungsplan aussehen, es lassen vermuten, dass es automatisch optimiert es für Du? –