【问题标题】:C# SQL INSERT INTO query and displaying resultC# SQL INSERT INTO 查询并显示结果
【发布时间】:2011-11-15 03:27:52
【问题描述】:

我正在使用 VS2005 和 SQL Server 2005。

我正在尝试执行连接 3 个 sql 表的多个 INSERT INTO SQL 语句。

这三个表是:

  • Table1: UserID, Username
  • Table2: UserID, Status
  • Table3: UserID, Username, Issue

以下是我需要执行的检查:

  1. 存在于Table1 中的用户应该存在于Table2

  2. 存在于Table1 中的用户不应在Table2 中拥有STATUS=DELETE

  3. Table2 中没有STATUS=DELETE 的用户应该存在于Table1

目前我只有一个SELECT 语句满足上述3个查询,没有添加到第三个表:

SELECT *
FROM table1 t1
FULL OUTER JOIN table2 t2 ON t1.userid = t2.userid
WHERE (t2.userid IS NULL AND t1.userid IS NOT NULL)
OR (t2.status = 'DELETE' AND t1.userid IS NOT NULL)
OR (t2.userid IS NOT NULL AND t2.status <> 'DELETE' AND t1.userid IS NULL)

我想,对于上述每项检查,INSERT 将结果放入具有唯一 ISSUE 变量的 Table3 中(例如,对于 1 号检查,ISSUE=User exist in t1 but not t2

目前我正在尝试形成一个将 3 个表连接在一起的查询,对于第一次检查找到的每个结果,t2.userid IS NULL AND t1.userid IS NOT NULL,它将在Table3 中插入一个新行。但是我的 SQL 查询有一些问题。

INSERT INTO Table3 (userid, username, issue) 
VALUES (t1.userid, t1.username, 'user not found in t2')
FULL OUTER JOIN table2 t2 ON t1.userid = t2.userid
WHERE (t2.userid IS NULL AND t1.userid IS NOT NULL)

非常感谢您的帮助。

【问题讨论】:

    标签: sql sql-server database tsql


    【解决方案1】:

    我没有这些表用于测试,所以语法会很接近:

    UserIdCOALESCE 将确保您在列中插入了一个值,因为您不确定该值是否存在于 Table1 或 Table2 中

    我还刚刚使用了 CASE 语句的 where 子句来填充 Issue 列。您可以更新文本以适合您的应用程序。

    INSERT INTO Table3(UserId, Username, Issue)
    SELECT COALESCE(t1.UserId, t2.UserId), t1.UserName
       , CASE
            WHEN (t2.userid IS NULL AND t1.userid IS NOT NULL)
               THEN 'User exists in t1 but not in t2'
            WHEN (t2.status = 'DELETE' AND t1.userid IS NOT NULL)
               THEN 'User Exists in t1, but status in t2 is not DELETE'
            WHEN (t2.userid IS NOT NULL AND t2.status != 'DELETE' AND t1.userid IS NULL)
               THEN 'Non-Deleted user in t2 does not exist in t1'
         END
    FROM table1 t1
    FULL OUTER JOIN table2 t2 ON t1.userid = t2.userid
    WHERE (t2.userid IS NULL AND t1.userid IS NOT NULL)
       OR (t2.status = 'DELETE' AND t1.userid IS NOT NULL)
       OR (t2.userid IS NOT NULL AND t2.status != 'DELETE' AND t1.userid IS NULL)
    

    【讨论】:

    • 嗨,亚当,感谢您的大力帮助。我想问一下,如果我也想从 t2 中检索变量 'Status' 并把它放在我的 t3 中显示呢?我在查询中的什么位置添加它?
    • 目前您的规范没有在表 3 中显示状态列。如果您添加了一个,您可以简单地将, t2.status 放在issue 列的case 语句之后。您还可以将t2.status 值连接到issue 列,使用'your existing string And Current User Status = "' + t2.status + '"' 将状态用双引号括起来。这也很容易让您查看它是否为空,因为您将拥有..Status = ""
    • 感谢亚当!我已在您的帮助下成功添加了状态列和变量。我用谷歌搜索了检查 SQL 中的重复项,但有些文章说这是不可能的。是否可以集成一个不区分大小写检查重复键的 SQL 检查?
    • @RUiHAO,我相信你在这里问过这个问题:stackoverflow.com/questions/8132763/…,我已经为你提供了答案。
    【解决方案2】:
    1.
        Insert in to Table3(userid,issue)
            SELECT t1.userid,'check no.1'
            FROM table1 t1
            FULL OUTER JOIN table2 t2 ON t1.userid = t2.userid
            where t1.userid not null and t2.userid is null
    2.
        Insert in to Table3(userid,issue)
            SELECT t1.userid,'check no.2'
            FROM table1 t1
            inner JOIN table2 t2 ON t1.userid = t2.userid
            where t2.status = 'DELETE'
    
    3.
        Insert in to Table3(userid,issue)
            SELECT t2.userid,'check no.3'
            FROM table1 t1
            right outer JOIN table2 t2 ON t1.userid = t2.userid
            where t2.status = 'DELETE' and t1.userid is null
    

    【讨论】:

    • 您的代码是一个非常简化的版本,我完全可以理解!是否可以根据是否区分大小写对 t1 和 t2 上的重复键(例如用户 ID)进行检查?
    • 嗨 RedHat,为什么在第三次检查时使用 RIGHT OUTER JOIN? Status!=Delete 的用户应该存在于 t1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-02
    • 1970-01-01
    • 2016-07-27
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多