Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C SQL插入查询并显示结果_Sql_Sql Server_Database_Tsql - Fatal编程技术网

C SQL插入查询并显示结果

C SQL插入查询并显示结果,sql,sql-server,database,tsql,Sql,Sql Server,Database,Tsql,我正在使用VS2005和SQLServer2005 我正在尝试执行多个INSERT INTO SQL语句,这些语句连接3个SQL表 这3个表格是: 表1:用户ID、用户名 表2:用户ID、状态 表3:用户ID、用户名、问题 以下是我需要执行的检查: 表1中存在的用户应存在于表2中 表1中存在的用户在表2中不应具有STATUS=DELETE 表2中没有STATUS=DELETE的用户应该存在于表1中 目前我只有一个SELECT语句,它满足上述3个查询,没有添加到第3个表中: SELECT * FR

我正在使用VS2005和SQLServer2005

我正在尝试执行多个INSERT INTO SQL语句,这些语句连接3个SQL表

这3个表格是:

表1:用户ID、用户名 表2:用户ID、状态 表3:用户ID、用户名、问题 以下是我需要执行的检查:

表1中存在的用户应存在于表2中

表1中存在的用户在表2中不应具有STATUS=DELETE

表2中没有STATUS=DELETE的用户应该存在于表1中

目前我只有一个SELECT语句,它满足上述3个查询,没有添加到第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)

非常感谢您的帮助。

我没有用于测试的这些表,因此语法将非常接近:

用户ID的合并将确保在列中插入一个值,因为您不确定该值是否存在于表1或表2中

我刚才还在案例陈述中使用了where子句来填充问题列。您可以更新文本以适合您的应用程序

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)

我没有用于测试的这些表,因此语法将非常接近:

用户ID的合并将确保在列中插入一个值,因为您不确定该值是否存在于表1或表2中

我刚才还在案例陈述中使用了where子句来填充问题列。您可以更新文本以适合您的应用程序

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放在问题列的case语句之后。您还可以将t2.status值连接到issue列中,使用“您的现有字符串和当前用户状态=”+t2.status+“”将状态用双引号括起来。这也很容易让你看到它是否是空的,就像你将看到的那样..Status=谢谢Adam!我已从您的帮助中成功添加状态列和变量。我在谷歌上搜索过SQL中的重复项,但有些文章说这是不可能的。是否可以将SQL检查与不区分大小写的SQL检查集成在一起以检查重复密钥?@RUiHAO,我相信您已经在这里提出了这个问题:我已经为您提供了答案。嗨,Adam,谢谢您的帮助。我想问,如果我也想从t2中检索变量“Status”并将其放入t3中显示,该怎么办?我应该在查询中的何处添加它?目前,您的规范在表3中没有显示状态栏。如果您添加了一个,您可以简单地将t2.status放在问题列的case语句之后。您还可以将t2.status值连接到issue列中,使用“您的现有字符串和当前用户状态=”+t2.status+“”将状态用双引号括起来。这也很容易让你看到它是否是空的,就像你将看到的那样..Status=谢谢Adam!我已从您的帮助中成功添加状态列和变量。我在谷歌上搜索过SQL中的重复项,但有些文章说这是不可能的。有没有可能集成一个SQL检查来检查重复的密钥,而不区分大小写?@RUiHAO,我相信你在这里问了这个问题:我已经为你提供了一个答案。你的代码是一个非常简化的版本,我可以完全理解它!是否可以在区分大小写和不区分大小写的基础上检查重复的密钥,例如t1和t2上的用户ID?嗨,RedHat,为什么在第三次检查时使用右外部联接?用户的状态=删除应该存在于T1中。您的代码是一个非常简化的版本,我可以完全理解它!是否可以在区分大小写和不区分大小写的基础上检查重复的密钥,例如t1和t2上的用户ID?嗨,RedHat,为什么在第三次检查时使用右外部联接?用户的状态=Delete应该存在于t1中
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