如何在SQL Server中的JOIN中使用案例语句?

发布于 2025-01-21 20:02:23 字数 4448 浏览 0 评论 0 原文

我有一个存储过程,如果 applicationTypeid 在ID列表中,我想选择一个列。我正在尝试使用案例语句,但我不知道我在做什么错。我创建了一个表,并向它添加了 applicationTypeids 。表名是 @ReLationshipStypeApplicationIDID

换句话说,我要做的是以下内容:

  1. 如果 applicationTypeID 位于 @relations> @relationshipspepepepepeapeplicationid 表中
  2. 加入并从 igcrelationshiptypes 表中选择 name 列,

这是我的原始存储过程

WITH AllCustomers AS
(
    SELECT 
        app.ApplicationId,
        cust.Name,
        ApplicationTypes.TypeName AS ApplicationType,
        ApplicationSources.ApplicationSourceName AS ApplicationSource,
        CreatedDate = app.CreatedDate,
        LastUpdateDate = app.StatusChangeDate,
        app.StatusId,
        InProgress = STUFF((SELECT ';'+#aw.ApplicationWorkflowName
                            FROM #aw
                            WHERE #aw.ApplicationId = app.ApplicationId
                            ORDER BY StepNumber
                            FOR XML PATH('')), 1, 1, ''),
        ActionRequired = (SELECT top 1 1 FROM #aw 
                          WHERE #aw.ApplicationId = app.ApplicationId 
                            AND #aw.WorkflowStatusId = 6),
        AccountId = STUFF((SELECT ';' + acct.AccountId 
                           FROM #accounts AS acct
                           WHERE acct.ApplicationId = app.ApplicationId
                           ORDER BY acct.ParentAccountId
                           FOR XML PATH('')), 1, 1, '')
     FROM 
         [dbo].[Applications] app
     INNER JOIN 
         [dbo].[Customers] AS cust ON cust.CustomerId = app.CustomerId
     INNER JOIN 
         [dbo].[ApplicationTypes] ON ApplicationTypes.ApplicationTypeId = app.ApplicationTypeId
     INNER JOIN 
         [dbo].[ApplicationSources] ON ApplicationSources.ApplicationSourceId = app.ApplicationSourceId
     WHERE 
         app.StatusId <> '4020-8901-64FFE33F06B1' 
         AND (@applicationTypeId IS NULL OR app.ApplicationTypeId = @applicationTypeId)

,这是我更新的内容:

WITH AllCustomers AS
(
    SELECT 
        app.ApplicationId,
        cust.Name,
        ApplicationTypes.TypeName AS ApplicationType,
        ApplicationSources.ApplicationSourceName AS ApplicationSource,
        CreatedDate = app.CreatedDate,
        LastUpdateDate = app.StatusChangeDate,
        app.StatusId,
        InProgress = STUFF((SELECT ';' + #aw.ApplicationWorkflowName
                            FROM #aw
                            WHERE #aw.ApplicationId = app.ApplicationId
                            ORDER BY StepNumber
                            FOR XML PATH('')), 1, 1, ''),
        ActionRequired = (SELECT top 1 1 FROM #aw 
                          WHERE #aw.ApplicationId = app.ApplicationId 
                            AND #aw.WorkflowStatusId = 6),
        AccountId = STUFF((SELECT ';' + acct.AccountId 
                           FROM #accounts as acct
                           WHERE acct.ApplicationId = app.ApplicationId
                           ORDER BY acct.ParentAccountId
                           FOR XML PATH('')), 1, 1, ''),
        CASE
            WHEN app.ApplicationTypeId IN (SELECT * FROM @RelationshipsTypeApplicationId)
                THEN relationshipType.Name  
        END
    FROM 
        [dbo].[Applications] app
    INNER JOIN 
        [dbo].[Customers] AS cust ON cust.CustomerId = app.CustomerId
    INNER JOIN 
        [dbo].[ApplicationTypes] ON ApplicationTypes.ApplicationTypeId = app.ApplicationTypeId
    INNER JOIN 
        [dbo].[ApplicationSources] ON ApplicationSources.ApplicationSourceId = app.ApplicationSourceId
    INNER JOIN 
        [dbo].[IGCRelationshipTypes] AS relationshipType 
                ON CASE 
                       WHEN app.ApplicationTypeId IN (SELECT * FROM @RelationshipsTypeApplicationId)
                           THEN (relationshipType.Name = @relationshipType)
                   END
    WHERE 
        app.StatusId <> '4020-8901-64FFE33F06B1' 
        AND (@applicationTypeId IS NULL OR app.ApplicationTypeId = @applicationTypeId)

这是语法错误的屏幕截图:

有人可以阐明我在这里缺少的东西吗?

I have a stored procedure that I want to select a column if the applicationTypeId is in a list of IDs. I am trying to use the CASE statement but I don't know what I am doing wrong. I created a table and added the ApplicationTypeIds to it. The table name is @RelationshipsTypeApplicationId

In other words, what I am trying to do is the following:

  1. If the applicationTypeId is in the @RelationshipsTypeApplicationId table
  2. Join and select the Name column from the IGCRelationshipTypes table

Here is my original stored procedure

WITH AllCustomers AS
(
    SELECT 
        app.ApplicationId,
        cust.Name,
        ApplicationTypes.TypeName AS ApplicationType,
        ApplicationSources.ApplicationSourceName AS ApplicationSource,
        CreatedDate = app.CreatedDate,
        LastUpdateDate = app.StatusChangeDate,
        app.StatusId,
        InProgress = STUFF((SELECT ';'+#aw.ApplicationWorkflowName
                            FROM #aw
                            WHERE #aw.ApplicationId = app.ApplicationId
                            ORDER BY StepNumber
                            FOR XML PATH('')), 1, 1, ''),
        ActionRequired = (SELECT top 1 1 FROM #aw 
                          WHERE #aw.ApplicationId = app.ApplicationId 
                            AND #aw.WorkflowStatusId = 6),
        AccountId = STUFF((SELECT ';' + acct.AccountId 
                           FROM #accounts AS acct
                           WHERE acct.ApplicationId = app.ApplicationId
                           ORDER BY acct.ParentAccountId
                           FOR XML PATH('')), 1, 1, '')
     FROM 
         [dbo].[Applications] app
     INNER JOIN 
         [dbo].[Customers] AS cust ON cust.CustomerId = app.CustomerId
     INNER JOIN 
         [dbo].[ApplicationTypes] ON ApplicationTypes.ApplicationTypeId = app.ApplicationTypeId
     INNER JOIN 
         [dbo].[ApplicationSources] ON ApplicationSources.ApplicationSourceId = app.ApplicationSourceId
     WHERE 
         app.StatusId <> '4020-8901-64FFE33F06B1' 
         AND (@applicationTypeId IS NULL OR app.ApplicationTypeId = @applicationTypeId)

And here is what I updated:

WITH AllCustomers AS
(
    SELECT 
        app.ApplicationId,
        cust.Name,
        ApplicationTypes.TypeName AS ApplicationType,
        ApplicationSources.ApplicationSourceName AS ApplicationSource,
        CreatedDate = app.CreatedDate,
        LastUpdateDate = app.StatusChangeDate,
        app.StatusId,
        InProgress = STUFF((SELECT ';' + #aw.ApplicationWorkflowName
                            FROM #aw
                            WHERE #aw.ApplicationId = app.ApplicationId
                            ORDER BY StepNumber
                            FOR XML PATH('')), 1, 1, ''),
        ActionRequired = (SELECT top 1 1 FROM #aw 
                          WHERE #aw.ApplicationId = app.ApplicationId 
                            AND #aw.WorkflowStatusId = 6),
        AccountId = STUFF((SELECT ';' + acct.AccountId 
                           FROM #accounts as acct
                           WHERE acct.ApplicationId = app.ApplicationId
                           ORDER BY acct.ParentAccountId
                           FOR XML PATH('')), 1, 1, ''),
        CASE
            WHEN app.ApplicationTypeId IN (SELECT * FROM @RelationshipsTypeApplicationId)
                THEN relationshipType.Name  
        END
    FROM 
        [dbo].[Applications] app
    INNER JOIN 
        [dbo].[Customers] AS cust ON cust.CustomerId = app.CustomerId
    INNER JOIN 
        [dbo].[ApplicationTypes] ON ApplicationTypes.ApplicationTypeId = app.ApplicationTypeId
    INNER JOIN 
        [dbo].[ApplicationSources] ON ApplicationSources.ApplicationSourceId = app.ApplicationSourceId
    INNER JOIN 
        [dbo].[IGCRelationshipTypes] AS relationshipType 
                ON CASE 
                       WHEN app.ApplicationTypeId IN (SELECT * FROM @RelationshipsTypeApplicationId)
                           THEN (relationshipType.Name = @relationshipType)
                   END
    WHERE 
        app.StatusId <> '4020-8901-64FFE33F06B1' 
        AND (@applicationTypeId IS NULL OR app.ApplicationTypeId = @applicationTypeId)

Here is a screenshot of the syntax error:

enter image description here

Can someone shed some light on what I am missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2025-01-28 20:02:23

case 在T-SQL中是返回单个标量值的表达式,而不是为了控制流程。参见案例表达式的肮脏秘密/a>。您需要这样的东西,但是我没有能力尝试确定一个名为 @relationshipStypeApplicationIdID 带有一个列的表(因为您真的不应该说 在(select *))中,或者如果您想在不存在匹配时做一些不同的事情。

...
ON relationshipType.Name = CASE 
     WHEN app.ApplicationTypeId IN 
          (SELECT * FROM @RelationshipsTypeApplicationId)
     THEN @relationshipType END
...

CASE in T-SQL is an expression that returns a single scalar value, it is not for control of flow. See Dirty secrets of the CASE expression. You'll need something like this, but I don't have the energy to try to determine why you have a table named @RelationshipsTypeApplicationId with a single column (because you really shouldn't say IN (SELECT *)) or if you want to do something different when the match does not exist.

...
ON relationshipType.Name = CASE 
     WHEN app.ApplicationTypeId IN 
          (SELECT * FROM @RelationshipsTypeApplicationId)
     THEN @relationshipType END
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文