I have a stored procedure like this (shortened to the minimum necessary):
CREATE PROCEDURE [dbo].[proc_Foo]
-- Add the parameters for the stored procedure here
@StyleNumber int,
@App nvarchar(50),
@User nvarchar(20)
AS
BEGIN
DECLARE @UserChar varchar(1) = 'U'
-- User with App
SELECT [StyleType],
[Value], 'UA' AS 'DerivedFrom'
FROM daten.dbo.GridStyleLocal
WHERE [StyleAssign] = @UserChar
AND [User] = @User
AND [StyleNumber] = @StyleNumber
AND [Application] = @App
UNION
-- Global (Basis) with app
SELECT [StyleType],
[Value], 'GA' AS 'DerivedFrom'
FROM basis.dbo.GridStyle
WHERE [StyleNumber] = @StyleNumber
AND [Application] = @App
AND [StyleType] NOT IN (
SELECT [StyleType]
FROM daten.dbo.GridStyleLocal
WHERE [StyleAssign] = @UserChar
AND [User] = @User
AND [StyleNumber] = @StyleNumber
AND [Application] = @App)
UNION
-- Scrape missing StyleTypes from Number 0
SELECT [StyleType],
[Value], 'G0A' AS 'DerivedFrom'
FROM basis.dbo.GridStyle
WHERE [StyleNumber] = 0
AND [Application] = @App
AND [StyleType] NOT IN (
SELECT [StyleType]
FROM daten.dbo.GridStyleLocal
WHERE [StyleAssign] = @UserChar
AND [User] = @User
AND [StyleNumber] = @StyleNumber
AND [Application] = @App)
AND [StyleType] NOT IN (
SELECT [StyleType]
FROM basis.dbo.GridStyle
WHERE [StyleNumber] = @StyleNumber
AND [Application] = @App)
END
GO
Now StyleNumber 0 is my base style. All properties (StyleType) that are not in another StyleNumber are derived from StyleNumber 0. But now if I include the second SELECT (Global (Basis) with app) when @StyleNumber is set to 0, the DerivedFrom value is GA instead of G0A.
So is there a any kind of conditional, that could exclude this SELECT out of the union if @StyleNumber is 0?
Not very clear exactly what you need. Assuming you need to get
G0Aif@StyleNumber = 0from the second query, change yoursecond select query(– Global (Basis) with app) as follows with theCASE.NOT to select any records from second select query if
@StyleNumber = 0, addCASEtoWHERE clauseas;