简体   繁体   中英

Insert multiple records into table for each user in another table in SQL Server

I want to create notification application in node js and I just created a database with these three tables in SQL Server:

tbluser

user_id
user_name

tbluser_notification

user_id
noti_id
read

tblnotification

noti_id
noti_title
noti_mesg
noti_create
noti_sender_id

My question is: whenever I insert a notification into tblnotification , I want to insert a record for each user into the tbluser_notification .

Create after insert trigger

CREATE TRIGGER [dbo].[trgtblnotificationInsert]
    ON [dbo].[tblnotification]
    AFTER INSERT
AS
BEGIN
    SET NOCOUNT ON;

    INSERT INTO tbluser_notification (user_id, noti_id, notificationread)
    SELECT tbluser.user_id, inserted.noti_id, 0
    FROM tbluser
       CROSS JOIN inserted
END

Please note: I have changed the column name 'read' to 'notificationread' as read is reserved word.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM