I have 2 tables, one with all the email data, and another with all the specific member email data that one creates a row if an email has been read. Once an email has been read by a member its added to the Member_email_read table (which is created and populated based on all read emails).
I am trying to set (on mass) all of the messages to read (this would populate the Member_email_read table) but whilst I can add them one at a time (see the stored procedue below), I am unable to add them on mass.
The two tables are Email, which holds a record for every email into the system. The other table is a table of all email that the member has read. Each time an email is read a record is added to the Member_email_read table. They are assiocited on the message_id (and both should use the same user_id). The two tables are as follows -
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]
To set all the messages (for a certain user) to unread all I would have to do is delete every record from that table for that user, which can be done with the following:
DELETE FROM [MemberData].[dbo].[Member_email_read]
WHERE [member_email_FK_message_id_FK_user_id] ='2';
I am basically looking for the reverse of this delete.
I have created a Stored procedure that allows for the setting of ONE specific email to be set to read, however this stored procedure (when executed) requires the member to enter a email_id, message_id, user_id, status, read_datetime & delete_datetime.
CREATE PROCEDURE [dbo].[set_member_email_to_read]
@member_email_id int,
@member_email_FK_message_id int,
@member_email_FK_user_id int
@member_email_status varchar(1),
@member_email_read_datetime dateTime,
@member_email_delete_datetime dateTime
as
if not exists (Select * from [dbo].[Member_email_read] where [member_email_FK_message_id] = @member_email_FK_message_id) begin
insert into [dbo].[Member_email_read]
(
[member_email_FK_message_id]
,[member_email_FK_user_id]
,[member_email_status]
,[member_email_read_datetime]
,[member_email_delete_datetime]
)
values
(
@member_email_FK_message_id,
@member_email_FK_user_id
@member_email_status,
@member_email_read_datetime,
@member_email_delete_datetime
)
SELECT Convert(int,SCOPE_IDENTITY()) As InsertedID
end else begin
update [dbo].[Member_email_read] set
[member_email_FK_message_id] = @member_email_FK_message_id
,[member_email_FK_user_id] = @member_email_FK_user_id
,[member_email_status] = @member_email_status
,[member_email_read_datetime] = @member_email_read_datetime
,[member_email_delete_datetime] = @member_email_delete_datetime
where [member_email_FK_user_id] = @member_email_FK_user_id
if (@@ERROR = 0) begin
SELECT Convert(int,@member_email_FK_user_id) As InsertedID
end
end
GO
I was hoping to create a stored procedure (or general SQL script) that would allow me to enter in a user_id and then allow for all emails for that user to change from unread to read (populate the Member_email_read table).
You can try using MERGE
to perform a bulk insert
/ update
from your Email
table into your Member_email_read
table:
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)
;
This should work, though as I mentioned in my comment above, you should rethink your table design and simply add a read_datetime
and delete_datetime
(possibly a status
column if you really need that as well) to your Email
table, rather than having a whole separate table simply to hold records identifying a delete status.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.