在SQL Server的另一个表中为每个用户插入多个记录到表中

时间:2021-04-12 16:29:03

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

我想在节点js中创建通知应用程序,我只是在SQL Server中使用这三个表创建了一个数据库:

tbluser

tbluser

user_id
user_name

tbluser_notification

tbluser_notification

user_id
noti_id
read

tblnotification

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.

我的问题是:每当我在tblnotification中插入通知时,我想将每个用户的记录插入到tbluser_notification中。

1 个解决方案

#1


0  

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.

请注意:我已将列名'read'更改为'notificationread',因为read是保留字。

#1


0  

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.

请注意:我已将列名'read'更改为'notificationread',因为read是保留字。