DECLARE @UserId INT DECLARE @PropertyId INT DECLARE @PropertyValue [NVARCHAR] (MAX) SELECT @UserId = ID FROM Users WHERE UserName = 'TSRAIRUser' IF (@UserId IS NOT NULL) BEGIN SELECT @PropertyId = PropertyId FROM [dbo].[DefaultProperties] WHERE PropertyName = 'ExportCSVUnfiltered' IF (@PropertyId IS NULL) BEGIN INSERT INTO [dbo].[DefaultProperties] (PropertyName, DefaultValue) VALUES ('ExportCSVUnfiltered', 'False') SELECT @PropertyId = PropertyId FROM [dbo].[DefaultProperties] WHERE PropertyName = 'ExportCSVUnfiltered' END IF (@PropertyId IS NOT NULL) BEGIN SELECT @PropertyValue = PropertyValue FROM [dbo].[UserProperties] WHERE UserId = @UserId AND PropertyId = @PropertyId IF (@PropertyValue IS NULL) BEGIN INSERT INTO [dbo].[UserProperties] VALUES (@UserId, @PropertyId, 'True') END ELSE BEGIN UPDATE [dbo].[UserProperties] SET PropertyValue = 'True' WHERE UserId = @UserId AND PropertyId = @PropertyId; END END END