sql情况下的情况,如果条件通过

发布于 2025-01-25 18:18:20 字数 666 浏览 1 评论 0原文

我不知道如何添加最后一个条件。 这是动态代码 +光标的一部分。光标由表列列表组成。我正在比较两个表格中相同结构,将它们连接到PK的表格中的值,并在值不匹配的时候检查一列。 我在最后一个条件下需要的是:

  • 是'untcode'
  • 那么我想排除tmc.untcode无效的行
    DECLARE @columnName CHAR(10) = 'untcode';
    
    SELECT MC.@columnName, TMC.@columnName 
    FROM dbo.firstable AS TMC
    INNER JOIN dbo.secondtable AS MC
        ON MC.ID = TMC.ID
        AND MC.Code = TMC.Code        
    WHERE MC.ID = 123
    AND TMC.@columnName<> MC.@columnName
    --How do I add this??
    AND TMC.@columnName CASE 
        WHEN @columnName='untCode' THEN not null 
        ELSE --What???
        END ;

如果@columnname

I cannot figure out how to add the last condition.
This is part of dynamic code + cursor. Cursor consists of a list of table columns. I am comparing values in two tables that are identical in their structure, joining them on PK, and checking one column at a time where the values are not matching.
What I need in my last condition is:

  • if @columnName is 'untCode'
  • then I want to exclude those rows where TMC.untCode is null
  • otherwise this condition does not apply
    DECLARE @columnName CHAR(10) = 'untcode';
    
    SELECT MC.@columnName, TMC.@columnName 
    FROM dbo.firstable AS TMC
    INNER JOIN dbo.secondtable AS MC
        ON MC.ID = TMC.ID
        AND MC.Code = TMC.Code        
    WHERE MC.ID = 123
    AND TMC.@columnName<> MC.@columnName
    --How do I add this??
    AND TMC.@columnName CASE 
        WHEN @columnName='untCode' THEN not null 
        ELSE --What???
        END ;

Appreciate the help

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

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

发布评论

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

评论(1

苦妄 2025-02-01 18:18:20

正如您所说的那样:

    --How do I add this??
    AND NOT (@columnName='untCode' AND TMC.untCode is NULL)

或者略有更改:

    --How do I add this??
    AND (@columnName<>'untCode' OR TMC.untCode is NOT NULL)

但是您应该更正和tmc。@columnName&lt;&gt; @larnu所说的mc。 @columnName;您的意思是和tmc.columnname = mc.columnname`吗?

As you said it:

    --How do I add this??
    AND NOT (@columnName='untCode' AND TMC.untCode is NULL)

or with a slight change:

    --How do I add this??
    AND (@columnName<>'untCode' OR TMC.untCode is NOT NULL)

But you should correct AND TMC.@columnName<> MC.@columnName as @Larnu said; did you mean AND TMC.columnName=MC.columnName`?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文