SQL Server MERGE并不总是为member_ email_ read table

时间:2017-12-22 13:21:42

标签: sql sql-server sql-server-2008 email

这是发布的问题的延续 - Stored Procedure/SQL script that allows me to add a record to a table

我有2个表,一个包含所有电子邮件数据,另一个包含所有特定成员电子邮件数据,如果已读取电子邮件,则会创建一行。一旦成员读取了一封电子邮件,就会将其添加到Member_email_read表格中(根据所有阅读的电子邮件创建并填充)。

表格如下:

SELECT [member_email_id]
  ,[member_email_FK_message_id]
  ,[member_email_FK_user_id]
  ,[member_email_status]
  ,[member_email_read_datetime]
  ,[member_email_delete_datetime]
FROM [MemberData].[dbo].[Member_email_read]



SELECT[message_id]
  ,[email_catlogue_num]
  ,[email_FK_user_id]
  ,[Email_time]
  ,[email_content]
  ,[Email_created_date]
FROM [MemberData].[dbo].[Email]

根据之前的回答,我们认为最好的方法是创建一个MERGE UPDATE当条件为MATCHEDINSERTNOT MATCHED MERGE [MemberData].[dbo].[Member_email_read] AS tgt USING ( SELECT message_id, user_id, 'R', null, CURRENT_TIMESTAMP FROM [MemberData].[dbo].[Email] WHERE user_id = @UserId ) AS src (MessageId, UserId, Status, ReadDate, DeleteDate) ON ( tgt.member_email_FK_message_id = src.message_id AND tgt.member_email_FK_user_id = src.user_id ) WHEN MATCHED THEN UPDATE SET tgt.member_email_status = src.Status, tgt.member_email_read_datetime = src.ReadDate, tgt.member_email_delete_datetime = src.DeleteDate WHEN NOT MATCHED THEN INSERT (member_email_FK_message_id, member_email_FK_user_id, member_email_status, member_email_read_datetime, member_email_delete_datetime) VALUES (src.MessageId, src.UserId, src.Status, src.ReadDate, src.DeleteDate); }。

message_id  email_catlogue_num  email_FK_user_id    Email_time       Email_content
1806        1265                   2                20/12/2017 19:52  Test 1

当将未读电子邮件标记为root用户(发送大部分用户)时,这很有效,但这对其他用户来说并不适用。其他用户会发生的情况是,两个表中的用户ID都已连接,但消息ID不是。这意味着创建了不同的行。似乎root用户id(几乎)与它们相关联的所有message_ids,但是普通用户没有。一旦他们(正常)读取消息,就会创建一个记录,其中包含用户ID和[Email]表中不存在的消息Id。反正有没有解决这个问题,以便它包括这些记录,而无需检查前端的复选框?任何想法@gmiley?

如果我想检查读取user_id = 78的所有电子邮件,我将其输入到MERGE存储过程(如上所述),并且为user_id = 78设置了许多记录。但是如果我尝试运行一个从user_id = 78的收件箱中查找特定的一个(电子邮件内容为"测试1和#34;)。我从

得到了这个结果

如果我想要一封电子邮件内容为'测试1'设置为阅读我做的选择*来自[dbo]。[电子邮件]其中[Email_content] ='测试1'。它检索以下信息 -

    member_email_id member_email_FK_message_id  member_email_FK_user_id member_email_status member_email_read_datetime  member_email_delete_datetime
         0                 0                             0                     0                        0                          0

这表明有一条user_id = 2(root用户)的记录,但是这也显示在user_id = 78的收件箱中。但是它不会显示在表中,我什么都没得到(添加0表示什么都没有) :

member_email_id member_email_FK_message_id  member_email_FK_user_id member_email_status member_email_read_datetime  member_email_delete_datetime
       7035           1806                          78                     r                22/12/2017 14:53          9999-12-31 00:00:00.000

我期待的是(当我以user_id = 78登录并阅读消息时)

+--------------------+--------------+-----------+
|     message_id     |     int      | Unchecked |
+--------------------+--------------+-----------+
| email_catlogue_num | int          | Unchecked |
| email_FK_user_id   | int          | Unchecked |
| Email_time         | datetime     | Checked   |
| email_content      | nvarchar(10) | Checked   |
| Email_created_date | datetime     | Checked   |
|+--------------------+--------------+-----------+

电子邮件表格如下:

+------------------------------+------------+-----------+
|       member_email_id        |    int     | Unchecked |
+------------------------------+------------+-----------+
| member_email_FK_message_id   | int        | Unchecked |
| member_email_FK_user_id      | int        | Unchecked |
| member_email_status          | varchar(1) | Unchecked |
| member_email_read_datetime   | datetime   | Checked   |
| member_email_delete_datetime | datetime   | Unchecked |
+------------------------------+------------+-----------+

Member_email_read表格如下:

currentBuild.currentResult

0 个答案:

没有答案