Hi @CathyJi-MSFT !
Thank you for your reply. Trying to understand how your links should help me but can't really understand as I've set up my masking in my create table-scripts. I'm including a test setup so that anyone can have the same setup as I have if they want to try and help.
CREATE TABLE dbo.[TestUser]
(
id INT PRIMARY KEY IDENTITY(1,1),
userId INT NOT NULL,
identityNumber NVARCHAR(50) MASKED WITH (FUNCTION = 'partial(4, "0101", 0)') NOT NULL,
firstName NVARCHAR(50) MASKED WITH (FUNCTION = 'partial(2, "xxxxxxxxx", 0)') NOT NULL,
lastName NVARCHAR(50) MASKED WITH (FUNCTION = 'partial(2, "xxxxxxxxx", 0)') NOT NULL
)
CREATE TABLE dbo.TestUserRole
(
id INT PRIMARY KEY IDENTITY(1,1),
userId INT NOT NULL,
userRole NVARCHAR(50) MASKED WITH (FUNCTION = 'partial(2, "xxxxxxxxx", 0)') NOT NULL
)
ALTER TABLE [dbo].[TestUserRole] WITH CHECK ADD CONSTRAINT [FK_TestUser_UserRole] FOREIGN KEY([userId])
REFERENCES [dbo].[User] ([id])
SET IDENTITY_INSERT TestUser ON
INSERT INTO TestUser (id, userId, identityNumber, firstName, lastName)
VALUES (1, 200, N'19520102', N'Paddy', N'Smith'),
(2, 300, N'19500609', N'Trevor', N'Bolder'),
(3, 400, N'19460526', N'Mick', N'Ronson')
SET IDENTITY_INSERT TestUser OFF
INSERT INTO TestUserRole (userId, userRole)
VALUES (1, N'Roadie'),
(2, N'Bass player'),
(3, N'Guitarist'),
(3, N'Pianist')
GO
CREATE PROCEDURE [dbo].TestGetUserRecordXml
AS
WITH XMLNAMESPACES (DEFAULT 'urn:svsys:export:user')
SELECT
u.userId
,u.identityNumber
,u.firstName
,u.lastName
,(SELECT
ur.userRole
FROM dbo.TestUserRole ur
WHERE ur.userId = u.id
FOR XML PATH (''), ROOT ('userRoles'), TYPE, ELEMENTS)
FROM dbo.TestUser u
FOR XML PATH ('user'), ROOT ('users')
GO
CREATE USER [UserForMaskedData] WITHOUT LOGIN;
GRANT EXECUTE ON SCHEMA::dbo TO UserForMaskedData;
GRANT SELECT ON SCHEMA::dbo TO UserForMaskedData;