0

我有一个这样的存储过程(缩短到最低限度):

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

现在StyleNumber0 是我的基本风格。StyleType所有不在另一个中的属性( )都是StyleNumber从 0 派生的。但是现在如果我在设置为 0时StyleNumber包含第二个SELECT(Global (Basis) with app),则该值是 GA 而不是 G0A。@StyleNumberDerivedFrom

那么,如果为 0,是否有任何条件可以将其排除SELECT在联合之外@StyleNumber

4

1 回答 1

1

不是很清楚你需要什么。假设您需要从第二个查询中获取G0Aif ,请将(-- Global (Basis) with app)更改 为如下所示。@StyleNumber = 0second select query CASE

SELECT [StyleType],
       [Value], 
       CASE @StyleNumber WHEN 0 THEN 'G0A' ELSE 'GA' END AS 'DerivedFrom'
FROM basis.dbo.GridStyle
--Rest fo the query

NOT to select any records from second select query if @StyleNumber = 0, add CASE to WHERE clause as;

WHERE [Application]  = 
  CASE @StyleNumber
      WHEN 0 THEN 'This app does not exist' --Give unavailable @App number 
      ELSE @App
  END
于 2012-12-15T12:56:05.477 回答