How should I migrate this data into these Sql Server tables? - sql

I wish to migrate some data from a single table into these new THREE tables.
Here's my destination schema:
Notice that I need to insert into the first Location table .. grab the SCOPE_IDENTITY() .. then insert the rows into the Boundary and Country tables.
The SCOPE_IDENTITY() is killing me :( meaning, I can only see a way to do this via CURSORS. Is there a better alternative?
UPDATE
Here's the scripts for the DB Schema....
Location
CREATE TABLE [dbo].[Locations](
[LocationId] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](100) NOT NULL,
[OriginalLocationId] [int] NOT NULL,
CONSTRAINT [PK_Locations] PRIMARY KEY CLUSTERED
(
[LocationId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
)
Country
CREATE TABLE [dbo].[Locations_Country](
[IsoCode] [nchar](2) NOT NULL,
[LocationId] [int] NOT NULL,
CONSTRAINT [PK_Locations_Country] PRIMARY KEY CLUSTERED
(
[LocationId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Locations_Country] WITH CHECK ADD CONSTRAINT [FK_Country_inherits_Location] FOREIGN KEY([LocationId])
REFERENCES [dbo].[Locations] ([LocationId])
GO
ALTER TABLE [dbo].[Locations_Country] CHECK CONSTRAINT [FK_Country_inherits_Location]
GO
Boundary
CREATE TABLE [dbo].[Boundaries](
[LocationId] [int] NOT NULL,
[CentrePoint] [varbinary](max) NOT NULL,
[OriginalBoundary] [varbinary](max) NULL,
[LargeReducedBoundary] [varbinary](max) NULL,
[MediumReducedBoundary] [varbinary](max) NULL,
[SmallReducedBoundary] [varbinary](max) NULL,
CONSTRAINT [PK_Boundaries] PRIMARY KEY CLUSTERED
(
[LocationId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[Boundaries] WITH CHECK ADD CONSTRAINT [FK_LocationBoundary] FOREIGN KEY([LocationId])
REFERENCES [dbo].[Locations] ([LocationId])
GO
ALTER TABLE [dbo].[Boundaries] CHECK CONSTRAINT [FK_LocationBoundary]
GO

I don't see a need for SCOPE_IDENTITY or cursors if you approach the data in order of the parent/child relationship:
INSERT INTO LOCATION
SELECT t.name,
t.originallocationid
FROM ORIGINAL_TABLE t
GROUP BY t.name, t.originallocationid
INSERT INTO COUNTRY
SELECT DISTINCT
t.isocode,
l.locationid
FROM ORIGINAL_TABLE t
JOIN LOCATION l ON l.name = t.name
AND l.originallocationid = t.originalocationid
INSERT INTO BOUNDARY
SELECT DISTINCT
l.locationid,
t.centrepoint,
t.originalboundary,
t.largereducedboundary,
t.mediumreducedboundary,
t.smallreducedboundary
FROM ORIGINAL_TABLE t
JOIN LOCATION l ON l.name = t.name
AND l.originallocationid = t.originalocationid

After loading your Location table you could create a query that joins Location with your source single table. The join criteria would be the natural key (is that the Name column?) and it would return the new LocationId along with the Boundary data. The results would inserted into the new Boundary table.

Related

Trigger to update table after insert to insert or update into another table

I am having a little trouble with my after insert trigger it does not update/insert into the table and I am not sure why.
I have the tables employee, position, certification, employeecertification, positioncertification.
What I need to do is after a Position is assigned a Certification then all employees that are defined in that position (Employee:PositionId = Ins.PositionId] need to get the certification added (if it does not already exist in their record) , I might need to update them where they do exist (but for the moment do not believe that will be a requirement).
I have been getting a couple different results (neither is correct)
1: EmployeeId can not insert null into EmployeeId
2: On some other test data - nothing is inserted.
I have the following trigger with the sql for creation of tables that follows:
The Trigger
CREATE TRIGGER [dbo].[trig_InsNew_PositionCertification]
ON [dbo].[PositionCertification]
AFTER INSERT
AS
Insert into EmployeeCertification
(CertificationId, EmployeeId)
Select Ins.CertificationId, Emp.EmployeeId
From inserted Ins
Left Join dbo.Employee Emp On Emp.PositionId = Ins.PositionId
Left Join dbo.Certification Trw On Trw.CertificationId = Ins.CertificationId
WHERE NOT EXISTS
(SELECT EmployeeId, CertificationId
FROM
EmployeeCertification
WHERE EmployeeId = Emp.EmployeeId AND CertificationId = Ins.CertificationId);
GO
ALTER TABLE [dbo].[PositionCertification] ENABLE TRIGGER [trig_InsNew_PositionCertification]
GO
CREATE TABLES:
CREATE TABLE [dbo].[Position](
[PositionId] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Position] PRIMARY KEY CLUSTERED
(
[PositionId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE TABLE [dbo].[Employee](
[EmployeeId] [int] IDENTITY(1,1) NOT NULL,
[EmployeeName] [nvarchar](512) NOT NULL,
[PositionId] [int] NOT NULL,
CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
(
[EmployeeId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Employee] WITH CHECK ADD CONSTRAINT [FK_Employee_Position] FOREIGN KEY([PositionId])
REFERENCES [dbo].[Position] ([PositionId])
GO
ALTER TABLE [dbo].[Position] CHECK CONSTRAINT [FK_Employee_Position]
GO
CREATE TABLE [dbo].[Certification](
[CertificationId] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](512) NOT NULL,
CONSTRAINT [PK_Certification] PRIMARY KEY CLUSTERED
(
[CertificationId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[EmployeeCertification](
[EmployeeCertificationId] [int] IDENTITY(1,1) NOT NULL,
[EmployeeId] [int] NOT NULL,
[CertificationId] [int] NOT NULL,
CONSTRAINT [PK_EmployeeCertification] PRIMARY KEY CLUSTERED
(
[EmployeeId] ASC,
[CertificationId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[EmployeeCertification] WITH CHECK ADD CONSTRAINT [FK_EmpCertification_Certification] FOREIGN KEY([CertificationId])
REFERENCES [dbo].[Certification] ([CertificationId])
GO
ALTER TABLE [dbo].[EmployeeCertification] CHECK CONSTRAINT [FK_EmpCertification_Certification]
GO
ALTER TABLE [dbo].[EmployeeCertification] WITH CHECK ADD CONSTRAINT [FK_EmpCertification_Employee] FOREIGN KEY([EmployeeId])
REFERENCES [dbo].[Employee] ([EmployeeId])
GO
ALTER TABLE [dbo].[EmployeeCertification] CHECK CONSTRAINT [FK_EmpCertification_Employee]
GO
CREATE TABLE [dbo].[PositionCertification](
[PositionCertificationId] [int] IDENTITY(1,1) NOT NULL,
[PositionId] [int] NOT NULL,
[CertificationId] [int] NOT NULL,
CONSTRAINT [PK_PositionCertification] PRIMARY KEY CLUSTERED
(
[PositionId] ASC,
[CertificationId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

How to properly create table with 3 columns as primary key?

I have been working with SQL for 3 month, I would like to know create a table with 3 columns as primary key, Any help would be great thank you.
This code below is a Scrip to Create table generated my SQL Server Management Studio of the table
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[REP]
(
[id] [UNIQUEIDENTIFIER] ROWGUIDCOL NOT NULL,
[id_client] [NCHAR](30) NULL,
[id_representant] [UNIQUEIDENTIFIER] NULL,
[date_debut] [DATE] NULL,
[date_fin] [DATE] NULL,
CONSTRAINT [PK_REP]
PRIMARY KEY CLUSTERED ([id] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[REP]
ADD CONSTRAINT [DF_REP_id] DEFAULT (NEWID()) FOR [id]
GO
First all the columns that are part of the primary key have to be not null.
then defining the key just add all columns as comma separated list.
CREATE TABLE [dbo].[REP](
[id] [uniqueidentifier] ROWGUIDCOL NOT NULL,
[id_client] [nchar](30) NOT NULL,
[id_representant] [uniqueidentifier] NULL,
[date_debut] [date] NULL,
[date_fin] [date] NULL,
CONSTRAINT [PK_REP] PRIMARY KEY CLUSTERED
( [id] ASC,id_client ASC )
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]
In the code above I changed the column id_client to be not null and added it to the definition of the primary key ... ([id] ASC, id_client ASC).
To add more columns to a primary Key contraint you'd add the additional fields after the first field.
[id] ASC, nextfield ASC, nextfield2 asc
You can have a one composite key that comprise of more than one primary key.
CREATE TABLE xyz (
primary key (id,id_client,id_representer)
);

Foreign Keys for 4 tables in a GrandParent, Parent, Child, GrandChild so change the Parent Field cascades to GrandChild

I need to be able to change the Chain_ID in the parent table and have it cascade down to the assemblies table through use of foreign keys.
Currently there are foreign keys between the Stores and Machines which does work as when I change the Chain_ID in the Store table it does change the Chain_ID in the Machines table.
However the foreign key on the Machine and Assemblies tables does not update the Assemblies table Chain_ID.
Any help would be greatly appreciated
Database layout
USE [Test_DB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Chains]
(
**[Chain_ID]** [INT] IDENTITY(1001,1) NOT NULL,
[Chain_Name] [NVARCHAR](50) NOT NULL,
CONSTRAINT [PK__ChainID]
PRIMARY KEY CLUSTERED ([Chain_ID] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = ON, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE TABLE [dbo].[Stores]
(
**[Chain_ID]** [INT] NOT NULL,
**[Store_ID]** [INT] NOT NULL,
[Store_Name] [NVARCHAR](50) NULL,
CONSTRAINT [PK__ChainID_StoreID]
PRIMARY KEY CLUSTERED ([Chain_ID] ASC, [Store_ID] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = ON, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE TABLE [dbo].[Machines]
(
[Machine_ID] [INT] IDENTITY(10001,1) NOT NULL,
**[Chain_ID]** [INT] NOT NULL,
**[Store_ID]** [INT] NOT NULL,
[Status] [NVARCHAR](10) NULL,
[Comments] [NVARCHAR](200) NULL,
CONSTRAINT [PK__MachineID]
PRIMARY KEY CLUSTERED ([Machine_ID] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Machines] WITH CHECK
ADD CONSTRAINT [FK__Machines_Stores]
FOREIGN KEY ([Chain_ID], [Store_ID])
REFERENCES [dbo].[Stores] ([Chain_ID], [Store_ID])
ON UPDATE CASCADE
GO
ALTER TABLE [dbo].[Machines] CHECK CONSTRAINT [FK__Machines_Stores]
GO
CREATE TABLE [dbo].[Assemblies]
(
[Serial_Number] [NVARCHAR](11) NOT NULL,
[Machine_ID] [INT] NOT NULL,
**[Chain_ID]** [INT] NOT NULL,
**[Store_ID]** [INT] NOT NULL,
[Equipment_Type] [NVARCHAR](42) NULL,
[Status] [NVARCHAR](10) NULL,
CONSTRAINT [PK__Serial_Number]
PRIMARY KEY CLUSTERED ([Serial_Number] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = ON, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Assemblies] WITH CHECK
ADD CONSTRAINT [FK__Assemblies_Machines]
FOREIGN KEY([Machine_ID])
REFERENCES [dbo].[Test_DB] ([Machine_ID])
ON UPDATE CASCADE
GO
ALTER TABLE [dbo].[Assemblies] CHECK CONSTRAINT [FK__Assemblies_Machines]
GO

Insert data to column which has two relationships

So, what I want to do in here is I want to insert the data to table1 who has 'username' field, and this 'username' field has two relationships, which is go to student table and teacher table. But, when I insert the data to user table, I have a problem, and the problem caused by this 'username' field. It caused because the data of 'username' field is not the same as a unique key in student table, and when I change it and made the data same, I get an error too, but this time the data is not the same as a unique in teacher table. So, is it possible to make this 'username' field just get one of the table, like if one of student table and teacher table's data is in 'username', it still can be used. Or maybe this is wrong because of bad ERD? Here's my ERD, if you're asking it:
And well, I know this is really, really bad idea but I made the name in teacher table and student table become a unique key, because I can't create foreign key if I didn't do that. Please, I'm really thankful to your answer.
Here's the ddl for that 3 tables :
Student table :
CREATE TABLE [dbo].[student](
[studentid] [int] IDENTITY(2016000001,1) NOT NULL,
[name] [varchar](50) NOT NULL,
[address] [text] NOT NULL,
[gender] [varchar](7) NOT NULL,
[dateofbirth] [date] NOT NULL,
[nohp] [varchar](13) NOT NULL,
CONSTRAINT [PK_student] PRIMARY KEY CLUSTERED
(
[studentid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [IX_student] UNIQUE NONCLUSTERED
(
[name] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
Teacher Table :
CREATE TABLE [dbo].[teacher](
[teacherid] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](50) NOT NULL,
[gender] [varchar](7) NOT NULL,
CONSTRAINT [PK_teacher] PRIMARY KEY CLUSTERED
(
[teacherid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [IX_teacher] UNIQUE NONCLUSTERED
(
[name] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
User table:
CREATE TABLE [dbo].[user](
[userid] [int] IDENTITY(1,1) NOT NULL,
[username] [varchar](50) NOT NULL,
[password] [varchar](20) NOT NULL,
[role] [varchar](10) NOT NULL,
CONSTRAINT [PK_user] PRIMARY KEY CLUSTERED
(
[userid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[user] WITH CHECK ADD CONSTRAINT [FK_user_student] FOREIGN KEY([username])
REFERENCES [dbo].[student] ([name])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[user] CHECK CONSTRAINT [FK_user_student]
GO
ALTER TABLE [dbo].[user] WITH CHECK ADD CONSTRAINT [FK_user_teacher] FOREIGN KEY([username])
REFERENCES [dbo].[teacher] ([name])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[user] CHECK CONSTRAINT [FK_user_teacher]
GO
The main issue you faced is because your data structure was less than ideal. It did things like prevent you from changing somebody's name. I tossed together a quick example of a cleaner design. There are many assumptions here. I assumed that people have both a first and last name. I would be remiss if I didn't at least point out that assumption is not something you can always make. https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/ But for a school project it is more than sufficient. I also made the assumption that the addresses are US addresses. Again, this would not work in many real world scenarios. And the last assumption is that everybody can be either Male or Female. In the world today this is not always the case but demonstrates the technique well enough.
Here is how I would probably do this type of design. I would suggest you not just blindly copy this but use it as an idea to get your design more properly normalized.
create table Users
(
UserID int identity not null
, FirstName varchar(50) not null
, LastName varchar(50) not null
, AddressLine1 varchar(50)
, AddressLine2 varchar(50)
, City varchar(50)
, ST char(2)
, ZipCode varchar(9)
, Gender char(1)
, constraint PK_Users primary key clustered
(
UserID
)
, constraint CHK_Users_Gender
CHECK (Gender in ('M', 'F'))
, constraint CHK_Users_ZipCode
CHECK (LEN(ZipCode) in (5,9)) --This ensures you have either the 5 or 9 digiti zip code
)
create table Student
(
StudentID int identity not null
, UserID int not null
, BirthDate date
, constraint PK_Student primary key clustered
(
StudentID
)
, constraint FK_Student_Users foreign key (UserID) references Users(UserID)
)
create table Teacher
(
TeacherID int identity not null
, constraint PK_Teacher primary key clustered
(
TeacherID
)
, constraint FK_Teacher_Users foreign key (TeacherID) references Users(UserID)
)

Trigger causing primary key violation

We have a trigger that we are trying to update so that when a record is updated, the trigger will update a second table. When we activate the change, we get an error
Violation of PRIMARY KEY contstraint 'KPRIMARY_SO_SalesOrderHeader". Cannot insert duplicate key in object 'dbo.SO_SalesOrderHeader". The duplicate key is XXXXXX.
I'm really confused by this because I don't see in the trigger where we are trying to insert a key.
ALTER Trigger [dbo].[SO_SALESORDERHEADER_onOrderChange]
ON [dbo].[SO_SalesOrderHeader]
FOR INSERT, UPDATE
AS
SET NOCOUNT ON;
--+
INSERT INTO dbo.OrderUpdateQueue (SourceId, OrderNumber, Action)
SELECT DISTINCT
N'SO', Inserted.SALESORDERNO, N'U'
FROM
Inserted
LEFT JOIN
dbo.OrderUpdateQueue ON (Inserted.SALESORDERNO = OrderUpdateQueue.OrderNumber)
AND (OrderUpdateQueue.SourceID = N'SO')
AND (OrderUpdateQueue.Action = N'U')
WHERE
(OrderUpdateQueue.[Key] IS NULL)
AND (Inserted.SALESORDERNO IS NOT NULL);
--+ New Trigger Stuff
UPDATE MO
SET MO.[ShipDate] = I.ShipExpireDate
FROM [BACKEND_db].[dbo].[Order] MO
INNER JOIN Inserted I ON I.SalesOrderNo = MO.OrderId
WHERE MO.ShipDate <> I.ShipExpireDate;
CREATE TABLE [dbo].[SO_SalesOrderHeader](
[SalesOrderNo] [varchar](7) NOT NULL
CONSTRAINT [KPRIMARY_SO_SalesOrderHeader] PRIMARY KEY CLUSTERED
(
[SalesOrderNo] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[OrderUpdateQueue](
[Key] [int] IDENTITY(1,1) NOT NULL,
[SourceId] [nvarchar](50) NOT NULL,
[OrderNumber] [nvarchar](50) NOT NULL,
[Action] [nchar](1) NOT NULL,
CONSTRAINT [PK_OrderUpdateQueue] PRIMARY KEY CLUSTERED
(
[Key] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
USE [BACKEND_db]
CREATE TABLE [dbo].[Order](
[Key] [int] IDENTITY(1,1) NOT NULL,
[OrderId] [nvarchar](10) NULL,
[ShipDate] [datetime] NULL
CONSTRAINT [PK_MasOrder] PRIMARY KEY CLUSTERED
(
[Key] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
How you do not post the schema of OrderUpdateQueue table, I believe the pk is the OrderNumber field. You can try to change your trigger to:
ALTER Trigger [dbo].[SO_SALESORDERHEADER_onOrderChange]
ON [dbo].[SO_SalesOrderHeader]
FOR INSERT, UPDATE
AS
SET NOCOUNT ON;
--+
INSERT INTO dbo.OrderUpdateQueue (SourceId, OrderNumber, Action)
SELECT DISTINCT
N'SO', Inserted.SALESORDERNO, N'U'
FROM
Inserted
WHERE not exists (SELECT 1 FROM dbo.OrderUpdateQueue where Inserted.SALESORDERNO = OrderUpdateQueue.OrderNumber)
UPDATE MO
SET MO.[ShipDate] = I.ShipExpireDate
FROM [BACKEND_db].[dbo].[Order] MO
INNER JOIN Inserted I ON I.SalesOrderNo = MO.OrderId
WHERE MO.ShipDate <> I.ShipExpireDate;