CREATE OR ALTER PROCEDURE LD0SPDisableDataSelectionRefugee 
(
@PersonConf BIT
)
AS 
BEGIN

DECLARE @Schema VARCHAR(100) = (SELECT SCH.name FROM SYS.TABLES TAB JOIN SYS.schemas SCH ON TAB.schema_id=SCH.schema_id WHERE TAB.NAME = 'LD0P0A')
DECLARE @SQL NVARCHAR(MAX)

DELETE FROM LD0P0PRO WHERE FUNKTION='FLYKTURVAL'
INSERT INTO LD0P0PRO
			SELECT 'LD0','DATAURVAL',0,CONVERT(VARCHAR(8), GETDATE(), 112),CONVERT(VARCHAR(8), GETDATE(), 108)
				
IF @PersonConf = 'False'
BEGIN
		--Create all the views with no dataselection for refugee and no person confidentiality!
		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V1AJD
						AS SELECT * FROM LD0P0BJD BJD'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V7AJD
						AS SELECT * FROM LD0P0BJD BJD'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A70
						AS SELECT * FROM LD0P0A70'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A80
						AS SELECT * FROM LD0P0A80'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A81
						AS SELECT * FROM LD0P0A81'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A82
						AS SELECT * FROM LD0P0A82'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A83
						AS SELECT * FROM LD0P0A83'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A84
						AS SELECT * FROM LD0P0A84'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A85
						AS SELECT * FROM LD0P0A85'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A86
						AS SELECT * FROM LD0P0A86'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A87
						AS SELECT * FROM LD0P0A87'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A90
						AS SELECT * FROM LD0P0A90'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A700
						AS SELECT * FROM LD0P0A700'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A770
						AS SELECT * FROM LD0P0A770'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A780
						AS SELECT * FROM LD0P0A780'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A820
						AS SELECT * FROM LD0P0A820'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A880
						AS SELECT * FROM LD0P0A880'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A901
						AS SELECT * FROM LD0P0A901'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A902
						AS SELECT * FROM LD0P0A902'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A903
						AS SELECT * FROM LD0P0A903'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A71
						AS SELECT * FROM LD0P0A71'
		EXEC sp_executesql @SQL
	
END

IF @PersonConf = 'True'
BEGIN 
		--Create all the views with no dataselection for refugee but with person confidentiality!
		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V1AJD
						AS SELECT * FROM LD0P0BJD BJD'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V7AJD
						AS SELECT * FROM LD0P0BJD BJD'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A70 AS 
						SELECT * FROM LD0P0A70
							WHERE EXISTS (SELECT ''X'' FROM LD0P0SEKR SEKR
													  JOIN LD0V0ANVANDARE AJD
													  ON AJD.CSSANV=SEKR.CSSANV
													  AND SEKR.PNR=LD0P0A70.PNR)
								OR NOT EXISTS (SELECT ''X'' FROM LD0P0SEKR SEKR WHERE SEKR.PNR=LD0P0A70.PNR)'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A80 AS
						SELECT * 
							  FROM LD0P0A80 A80
							  WHERE EXISTS (SELECT ''X''
									FROM LD0V0A70 A70
									WHERE A70.FLYKTNR = A80.FLYKTNR)'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A81 AS
						SELECT * 
							  FROM LD0P0A81 A81
							  WHERE EXISTS (SELECT ''X''
									FROM LD0V0A70 A70
									WHERE A70.FLYKTNR = A81.FLYKTNR)'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A82 AS
						SELECT * 
							  FROM LD0P0A82 A82
							  WHERE EXISTS (SELECT ''X''
									FROM LD0V0A70 A70
									WHERE A70.FLYKTNR = A82.FLYKTNR)'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A83 AS
						SELECT * 
							  FROM LD0P0A83 A83
							  WHERE EXISTS (SELECT ''X''
									FROM LD0V0A70 A70
									WHERE A70.FLYKTNR = A83.FLYKTNR)'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A84 AS
						SELECT * 
							  FROM LD0P0A84 A84
							  WHERE EXISTS (SELECT ''X''
									FROM LD0V0A70 A70
									WHERE A70.FLYKTNR = A84.FLYKTNR)'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A85 AS
						SELECT * 
							  FROM LD0P0A85 A85
							  WHERE EXISTS (SELECT ''X''
									FROM LD0V0A70 A70
									WHERE A70.FLYKTNR = A85.FLYKTNR)'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A86 AS
						SELECT * 
							  FROM LD0P0A86 A86
							  WHERE EXISTS (SELECT ''X''
									FROM LD0V0A70 A70
									WHERE A70.FLYKTNR = A86.FLYKTNR)'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A87 AS
						SELECT * 
							  FROM LD0P0A87 A87
							  WHERE EXISTS (SELECT ''X''
									FROM LD0V0A70 A70
									WHERE A70.FLYKTNR = A87.FLYKTNR)'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A90 AS
						SELECT * 
							  FROM LD0P0A90 A90
							  WHERE EXISTS (SELECT ''X''
									FROM LD0V0A70 A70
									WHERE A70.FLYKTNR = A90.FLYKTNR)'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A700 AS 
						SELECT * FROM LD0P0A700
							WHERE EXISTS (SELECT ''X'' FROM LD0P0SEKR SEKR
													  JOIN LD0V0ANVANDARE AJD
													  ON AJD.CSSANV=SEKR.CSSANV
													  AND SEKR.PNR=LD0P0A700.PNR)
								OR NOT EXISTS (SELECT ''X'' FROM LD0P0SEKR SEKR WHERE SEKR.PNR=LD0P0A700.PNR)'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A770 AS
						SELECT * 
							  FROM LD0P0A770 A770
							  WHERE EXISTS (SELECT ''X''
									FROM LD0V0A700 A700
									WHERE A700.FLYKTNR = A770.FLYKTNR)'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A780 AS
						SELECT * 
							  FROM LD0P0A780 A780
							  WHERE EXISTS (SELECT ''X''
									FROM LD0V0A700 A700
									WHERE A700.FLYKTNR = A780.FLYKTNR)'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A820 AS
						SELECT * 
							  FROM LD0P0A820 A820
							  WHERE EXISTS (SELECT ''X''
									FROM LD0V0A700 A700
									WHERE A700.FLYKTNR = A820.FLYKTNR)'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A880 AS
						SELECT * 
							  FROM LD0P0A880 A880
							  WHERE EXISTS (SELECT ''X''
									FROM LD0V0A700 A700
									WHERE A700.FLYKTNR = A880.FLYKTNR)'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A901 AS
						SELECT * 
							  FROM LD0P0A901 A901
							  WHERE EXISTS (SELECT ''X''
									FROM LD0V0A700 A700
									WHERE A700.FLYKTNR = A901.FLYKTNR)'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A902 AS
						SELECT * 
							  FROM LD0P0A902 A902
							  WHERE EXISTS (SELECT ''X''
									FROM LD0V0A820 A820
									WHERE A820.SCHABLNR = A902.SCHABLNR)'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A903 AS
						SELECT * 
							  FROM LD0P0A903 A903
							  WHERE EXISTS (SELECT ''X''
									FROM LD0V0A880 A880
									WHERE A880.ATERSOKTILLFNR = A903.ATERSOKTILLFNR)'
		EXEC sp_executesql @SQL

		SET @SQL = 'CREATE OR ALTER VIEW '+ QUOTENAME(@Schema) +'.LD0V0A71 AS 
						SELECT * 
						  FROM LD0P0A71 A71
						  WHERE EXISTS (SELECT ''X''
								FROM LD0V0A70 A70
								WHERE A70.FLYKTNR = A71.FLYKTNR)
						  OR EXISTS (SELECT ''X''
								FROM LD0V0A700 A700
								WHERE A700.FLYKTNR = A71.FLYKTNR)'
		EXEC sp_executesql @SQL

END
END
 