USE UT_LD0
GO
CREATE VIEW UT_LD0.LD0V1C
      AS SELECT * 
      FROM UT_LD0.LD0P0C C
      WHERE EXISTS(SELECT 'X'
            FROM UT_LD0.LD0V0AJD AJD
            WHERE AJD.VYNAMN = 'FUTRED'
            AND C.TMARK = 0
            AND C.AEVERKTYP >= AJD.JFRDTA1
            AND C.AEVERKTYP <= AJD.JFRDTA2
            AND C.ORGID = AJD.JFRDTA3
            AND C.ORGENHNR LIKE AJD.JFRDTA10)
GO		
CREATE VIEW UT_LD0.LD0V1C20
      AS SELECT * 
      FROM UT_LD0.LD0P0C20 C20
      WHERE EXISTS(SELECT 'X'
            FROM UT_LD0.LD0V0AJD AJD
            WHERE AJD.VYNAMN = 'FINSATS'
            AND C20.INSTYP >= AJD.JFRDTA1
            AND C20.INSTYP <= AJD.JFRDTA2
            AND C20.ORGID = AJD.JFRDTA3
            AND C20.ORGENHNR LIKE AJD.JFRDTA10)
GO				
CREATE VIEW UT_LD0.LD0V0C
      AS SELECT * 
      FROM UT_LD0.LD0P0C C
      WHERE EXISTS(SELECT 'X'
            FROM UT_LD0.LD0V0AJD AJD
            WHERE AJD.VYNAMN = 'UTREDNING'
            AND C.TMARK = 0
            AND C.AEVERKTYP >= AJD.JFRDTA1
            AND C.AEVERKTYP <= AJD.JFRDTA2
            AND C.ORGID = AJD.JFRDTA3
            AND C.ORGENHNR LIKE AJD.JFRDTA10)
      OR EXISTS(SELECT 'X'
            FROM UT_LD0.LD0V0AJD AJD
            WHERE AJD.VYNAMN = 'VERKSTÄLL'
            AND C.TMARK = 1
            AND C.AEVERKTYP >= AJD.JFRDTA1
            AND C.AEVERKTYP <= AJD.JFRDTA2
            AND C.ORGID = AJD.JFRDTA3
            AND C.ORGENHNR LIKE AJD.JFRDTA10)
      WITH CHECK OPTION
GO
CREATE VIEW UT_LD0.LD0V0C20
      AS SELECT * 
      FROM UT_LD0.LD0P0C20 C20
      WHERE EXISTS(SELECT 'X'
            FROM LD0V0AJD AJD
            WHERE AJD.VYNAMN = 'INSATS'
            AND C20.INSTYP >= AJD.JFRDTA1
            AND C20.INSTYP <= AJD.JFRDTA2
            AND C20.ORGID = AJD.JFRDTA3
            AND C20.ORGENHNR LIKE AJD.JFRDTA10)
	    WITH CHECK OPTION
GO
CREATE VIEW UT_LD0.LD0V0C10 AS
SELECT * 
      FROM UT_LD0.LD0P0C10 C10
      WHERE EXISTS (SELECT 'X'
            FROM UT_LD0.LD0V0C C
            WHERE C.AEVERKNR = C10.ARENDENR
            OR    C.AEVERKNR = C10.VERKNR)
GO
CREATE VIEW UT_LD0.LD0V0C11 AS
SELECT * 
      FROM UT_LD0.LD0P0C11 C11
      WHERE EXISTS (SELECT 'X'
            FROM UT_LD0.LD0V0C C
            WHERE C.AEVERKNR = C11.AEVERKNR
            AND   C11.INSNR = 0)
      OR    EXISTS (SELECT 'X' 
            FROM UT_LD0.LD0V0C20 C20
            WHERE C20.INSNR = C11.INSNR)
GO
CREATE VIEW UT_LD0.LD0V0B
	AS SELECT * FROM UT_LD0.LD0P0B B
	WHERE EXISTS
	(SELECT 'X' FROM UT_LD0.LD0V0C C
	WHERE B.AKTNR = C.AKTNR)
GO
CREATE VIEW UT_LD0.LD0V0D AS
SELECT * 
      FROM UT_LD0.LD0P0D D
      WHERE EXISTS (SELECT 'X'
            FROM UT_LD0.LD0V0C C
            WHERE C.AEVERKNR = D.AEVERKNR)
      OR EXISTS (SELECT 'X' 
          FROM UT_LD0.LD0V0C20 C20
          WHERE C20.INSNR = D.INSNR)
GO
CREATE VIEW UT_LD0.LD0V0E AS
SELECT * 
      FROM UT_LD0.LD0P0E E
      WHERE EXISTS (SELECT 'X'
            FROM UT_LD0.LD0V0C C
            WHERE C.AEVERKNR = E.AEVERKNR 
		AND E.MAKDAT = ' ')
      OR EXISTS (SELECT 'X' 
          FROM UT_LD0.LD0V0C20 C20
          WHERE C20.INSNR = E.INSNR 
	     AND E.MAKDAT = ' ')
      OR (E.AEVERKNR = 0
	AND E.INSNR  = 0
	AND E.MAKDAT = ' ')
GO	      
CREATE VIEW UT_LD0.LD0V0E07 AS
SELECT * 
      FROM UT_LD0.LD0P0E07 E07
      WHERE EXISTS (SELECT 'X'
            FROM UT_LD0.LD0V0E E
            WHERE E.BESLUTNR = E07.BESLUTNR)
GO
CREATE VIEW UT_LD0.LD0V0E40 AS
SELECT * 
      FROM UT_LD0.LD0P0E40 E40
      WHERE EXISTS (SELECT 'X'
            FROM UT_LD0.LD0V0E E
            WHERE E.BESLUTNR = E40.BESLUTNR)
GO
CREATE VIEW UT_LD0.LD0V0F AS
SELECT * 
      FROM UT_LD0.LD0P0F F
      WHERE EXISTS (SELECT 'X'FROM UT_LD0.LD0V0C20 C20
                    WHERE C20.INSNR = F.INSNR)
GO
CREATE VIEW UT_LD0.LD0V0G AS
SELECT * 
      FROM UT_LD0.LD0P0G G
      WHERE EXISTS (SELECT 'X'FROM UT_LD0.LD0V0C20 C20
                    WHERE C20.INSNR = G.INSNR)
GO
CREATE VIEW UT_LD0.LD0V0H AS
SELECT * 
      FROM UT_LD0.LD0P0H H
      WHERE EXISTS (SELECT 'X'
            FROM UT_LD0.LD0V0C C
            WHERE C.AEVERKNR = H.AEVERKNR)          
      OR    EXISTS (SELECT 'X' 
            FROM UT_LD0.LD0V0C20 C20
            WHERE C20.INSNR = H.INSNR)
      OR    EXISTS(SELECT 'X'
            FROM UT_LD0.LD0V0AJD AJD
            WHERE AJD.VYNAMN = 'AKTUAL'
            AND H.AKTUAL >= AJD.JFRDTA1
            AND H.AKTUAL <= AJD.JFRDTA2
            AND H.ORGID = AJD.JFRDTA3
            AND H.ORGENHNR LIKE AJD.JFRDTA10
            AND H.AEVERKNR = 0
            AND H.INSNR = 0)
      WITH CHECK OPTION
GO
CREATE VIEW UT_LD0.LD0V0H10 AS
SELECT * 
      FROM UT_LD0.LD0P0H10 H10
      WHERE EXISTS (SELECT 'X'
            FROM UT_LD0.LD0V0AJD AJD
            WHERE AJD.VYNAMN='SAMTYCKE'
            AND H10.ORGID = AJD.JFRDTA3
            AND H10.ORGENHNR LIKE AJD.JFRDTA10
            AND H10.SAMTAVSER >= AJD.JFRDTA1
            AND H10.SAMTAVSER <= AJD.JFRDTA2)          
WITH CHECK OPTION
GO
CREATE VIEW UT_LD0.LD0V0H20 AS
SELECT * 
      FROM UT_LD0.LD0P0H20 H20
      WHERE EXISTS (SELECT 'X'
            FROM UT_LD0.LD0V0C C
            WHERE C.AEVERKNR = H20.AEVERKNR)
      OR EXISTS (SELECT 'X' 
          FROM UT_LD0.LD0V0C20 C20
          WHERE C20.INSNR = H20.INSNR)
GO
CREATE VIEW UT_LD0.LD0V0N AS
SELECT * 
      FROM UT_LD0.LD0P0N N
      WHERE EXISTS (SELECT 'X'FROM UT_LD0.LD0V0C20 C20
                    WHERE C20.INSNR = N.INSNR)
GO
CREATE VIEW UT_LD0.LD0V0N60 AS
SELECT * 
      FROM UT_LD0.LD0P0N60 N60
      WHERE EXISTS (SELECT 'X'FROM UT_LD0.LD0V0C20 C20
                    WHERE C20.INSNR = N60.INSNR)
GO
CREATE VIEW UT_LD0.LD0V0N70 AS
SELECT * 
      FROM UT_LD0.LD0P0N70 N70
      WHERE EXISTS (SELECT 'X'FROM UT_LD0.LD0V0C20 C20
                    WHERE C20.INSNR = N70.INSNR)
GO		
CREATE VIEW UT_LD0.LD0V0P
      AS SELECT * 
      FROM UT_LD0.LD0P0P P
      WHERE EXISTS(SELECT 'X'
            FROM UT_LD0.LD0V1AJD AJD
            WHERE AJD.VYNAMN = 'BEVAKNING'
            AND P.HAND LIKE AJD.JFRDTA10)
      OR EXISTS(SELECT 'X'
            FROM UT_LD0.LD0V1AJD AJD
            WHERE AJD.VYNAMN = 'BEVAKNING'
            AND P.MEDHAND LIKE AJD.JFRDTA10)
      WITH CHECK OPTION	
GO	      
CREATE VIEW UT_LD0.LD0V0O12
	AS SELECT * FROM UT_LD0.LD0P0O12 O12
	WHERE EXISTS
	(SELECT 'X' FROM UT_LD0.LD0V0C20 C20
        WHERE C20.INSNR = O12.INSNR)
GO
CREATE VIEW UT_LD0.LD0V0O10
	 AS SELECT * FROM UT_LD0.LD0P0O10 O10
 	 WHERE EXISTS
	 (SELECT 'X' FROM UT_LD0.LD0V0O12 O12
         WHERE O10.SAMFAKTNR = O12.SAMFAKTNR)
GO
CREATE VIEW UT_LD0.LD0V1T41
AS SELECT * FROM UT_LD0.LD0P0T41 T41
WHERE EXISTS (SELECT 'X' FROM UT_LD0.LD0V0C C
		WHERE C.AEVERKNR = T41.AGARNR AND
		      T41.AGARTYP = 21)
   OR EXISTS (SELECT 'X' FROM UT_LD0.LD0V0C C 
		WHERE C.AEVERKNR = T41.AGARNR AND
		      T41.AGARTYP = 22)
   OR EXISTS (SELECT 'X' FROM UT_LD0.LD0V0C20 C20 
		WHERE C20.INSNR = T41.AGARNR AND
		      T41.AGARTYP = 23)
   OR EXISTS (SELECT 'X' FROM UT_LD0.LD0V0H H 
		WHERE H.AKTUALNR = T41.AGARNR AND
		      T41.AGARTYP = 20)
   OR EXISTS (SELECT 'X' FROM UT_LD0.LD0V0L20 L20 
		WHERE L20.UPPDRNR = T41.AGARNR AND
		      T41.AGARTYP = 25)
   OR EXISTS (SELECT 'X' FROM UT_LD0.LD0V0A70 A70 
		WHERE A70.FLYKTNR = T41.AGARNR AND
		      T41.AGARTYP = 40)
GO
CREATE VIEW UT_LD0.GA1V0T41 
	AS SELECT * FROM UT_LD0.LD0P0T41 T41
		WHERE EXISTS (SELECT 'X' FROM UT_LD0.LD0V0C C
		WHERE C.AEVERKNR = T41.AGARNR AND T41.AGARTYP = 1)
	OR EXISTS (SELECT 'X' FROM UT_LD0.LD0V0C C
		WHERE C.AEVERKNR = T41.AGARNR AND T41.AGARTYP = 10)
	OR EXISTS (SELECT 'X' FROM UT_LD0.LD0V0C20 C20
		WHERE C20.INSNR = T41.AGARNR AND T41.AGARTYP = 7)
	OR EXISTS (SELECT 'X' FROM UT_LD0.LD0V0H H
		WHERE H.AKTUALNR = T41.AGARNR AND T41.AGARTYP = 14)
	OR EXISTS (SELECT 'X' FROM UT_LD0.LD0V0B B
		WHERE B.AKTNR = T41.AGARNR AND T41.AGARTYP = 19)
GO
CREATE VIEW UT_LD0.GA1V1TXT
	AS SELECT * FROM UT_LD0.GA1P0TXT TXT
		WHERE EXISTS (SELECT 'X' FROM UT_LD0.GA1V0T41 T41
				WHERE '7255.'+CAST(T41.DOKUMENTNR AS VARCHAR) = TXT.OBJEKTID)
GO
CREATE VIEW UT_LD0.LD0V0C40 AS 
SELECT *
FROM UT_LD0.LD0P0C40 C40
WHERE EXISTS (SELECT 'X'
      FROM UT_LD0.LD0V0C C
      WHERE C.FAMRATTNR = C40.FAMRATTNR)
   OR EXISTS (SELECT 'X'
      FROM UT_LD0.LD0V0H H
      WHERE H.FAMRATTNR = C40.FAMRATTNR)
GO
CREATE VIEW UT_LD0.LD0V0BVK AS SELECT * 
      FROM UT_LD0.LD0P0BVK BVK
      WHERE EXISTS(SELECT 'X'
            FROM UT_LD0.LD0V1AJD AJD
            WHERE AJD.VYNAMN = 'BEVAKTYP'
            AND BVK.BEVAKTYP >= AJD.JFRDTA1
            AND BVK.BEVAKTYP <= AJD.JFRDTA2
            AND BVK.HAND LIKE AJD.JFRDTA10)
GO
CREATE VIEW UT_LD0.PCCV0BVK AS SELECT * 
      FROM UT_LD0.LD0P0BVK BVK
      WHERE EXISTS(SELECT 'X'
            FROM UT_LD0.LD0V1AJD AJD
            WHERE AJD.VYNAMN = 'BEVAKTYP'
            AND BVK.BEVAKTYP >= AJD.JFRDTA1
            AND BVK.BEVAKTYP <= AJD.JFRDTA2
            AND BVK.HAND LIKE AJD.JFRDTA10)
GO
CREATE VIEW UT_LD0.LD0V0T60 AS 
SELECT * 
FROM UT_LD0.LD0P0T60 T60
WHERE EXISTS(SELECT 'X'
             FROM UT_LD0.LD0V0AJD AJD
             WHERE AJD.VYNAMN = 'SCANNING'
               AND T60.DOKTYP >= AJD.JFRDTA1
               AND T60.DOKTYP <= AJD.JFRDTA2
               AND T60.ORGID = AJD.JFRDTA3
               AND T60.ORGENHNR LIKE AJD.JFRDTA10) OR
      EXISTS(SELECT 'X'
             FROM UT_LD0.LD0V0H H
             WHERE T60.AGARNR = H.AKTUALNR
               AND T60.AEVERKNR = 0 
               AND T60.INSNR = 0) OR
      EXISTS(SELECT 'X'
             FROM UT_LD0.LD0V0C C
             WHERE T60.AEVERKNR = C.AEVERKNR) OR
      EXISTS(SELECT 'X'
             FROM UT_LD0.LD0V0C20 C20
             WHERE T60.INSNR = C20.INSNR)
GO
USE IN_LD0
GO
CREATE VIEW IN_LD0.LD0V1C
      AS SELECT * 
      FROM IN_LD0.LD0P0C C
      WHERE EXISTS(SELECT 'X'
            FROM IN_LD0.LD0V0AJD AJD
            WHERE AJD.VYNAMN = 'FUTRED'
            AND C.TMARK = 0
            AND C.AEVERKTYP >= AJD.JFRDTA1
            AND C.AEVERKTYP <= AJD.JFRDTA2
            AND C.ORGID = AJD.JFRDTA3
            AND C.ORGENHNR LIKE AJD.JFRDTA10)
GO		
CREATE VIEW IN_LD0.LD0V1C20
      AS SELECT * 
      FROM IN_LD0.LD0P0C20 C20
      WHERE EXISTS(SELECT 'X'
            FROM IN_LD0.LD0V0AJD AJD
            WHERE AJD.VYNAMN = 'FINSATS'
            AND C20.INSTYP >= AJD.JFRDTA1
            AND C20.INSTYP <= AJD.JFRDTA2
            AND C20.ORGID = AJD.JFRDTA3
            AND C20.ORGENHNR LIKE AJD.JFRDTA10)
GO				
CREATE VIEW IN_LD0.LD0V0C
      AS SELECT * 
      FROM IN_LD0.LD0P0C C
      WHERE EXISTS(SELECT 'X'
            FROM IN_LD0.LD0V0AJD AJD
            WHERE AJD.VYNAMN = 'UTREDNING'
            AND C.TMARK = 0
            AND C.AEVERKTYP >= AJD.JFRDTA1
            AND C.AEVERKTYP <= AJD.JFRDTA2
            AND C.ORGID = AJD.JFRDTA3
            AND C.ORGENHNR LIKE AJD.JFRDTA10)
      OR EXISTS(SELECT 'X'
            FROM IN_LD0.LD0V0AJD AJD
            WHERE AJD.VYNAMN = 'VERKSTÄLL'
            AND C.TMARK = 1
            AND C.AEVERKTYP >= AJD.JFRDTA1
            AND C.AEVERKTYP <= AJD.JFRDTA2
            AND C.ORGID = AJD.JFRDTA3
            AND C.ORGENHNR LIKE AJD.JFRDTA10)
      WITH CHECK OPTION
GO
CREATE VIEW IN_LD0.LD0V0C20
      AS SELECT * 
      FROM IN_LD0.LD0P0C20 C20
      WHERE EXISTS(SELECT 'X'
            FROM LD0V0AJD AJD
            WHERE AJD.VYNAMN = 'INSATS'
            AND C20.INSTYP >= AJD.JFRDTA1
            AND C20.INSTYP <= AJD.JFRDTA2
            AND C20.ORGID = AJD.JFRDTA3
            AND C20.ORGENHNR LIKE AJD.JFRDTA10)
	    WITH CHECK OPTION
GO
CREATE VIEW IN_LD0.LD0V0C10 AS
SELECT * 
      FROM IN_LD0.LD0P0C10 C10
      WHERE EXISTS (SELECT 'X'
            FROM IN_LD0.LD0V0C C
            WHERE C.AEVERKNR = C10.ARENDENR
            OR    C.AEVERKNR = C10.VERKNR)
GO
CREATE VIEW IN_LD0.LD0V0C11 AS
SELECT * 
      FROM IN_LD0.LD0P0C11 C11
      WHERE EXISTS (SELECT 'X'
            FROM IN_LD0.LD0V0C C
            WHERE C.AEVERKNR = C11.AEVERKNR
            AND   C11.INSNR = 0)
      OR    EXISTS (SELECT 'X' 
            FROM IN_LD0.LD0V0C20 C20
            WHERE C20.INSNR = C11.INSNR)
GO
CREATE VIEW IN_LD0.LD0V0B
	AS SELECT * FROM IN_LD0.LD0P0B B
	WHERE EXISTS
	(SELECT 'X' FROM IN_LD0.LD0V0C C
	WHERE B.AKTNR = C.AKTNR)
GO
CREATE VIEW IN_LD0.LD0V0D AS
SELECT * 
      FROM IN_LD0.LD0P0D D
      WHERE EXISTS (SELECT 'X'
            FROM IN_LD0.LD0V0C C
            WHERE C.AEVERKNR = D.AEVERKNR)
      OR EXISTS (SELECT 'X' 
          FROM IN_LD0.LD0V0C20 C20
          WHERE C20.INSNR = D.INSNR)
GO
CREATE VIEW IN_LD0.LD0V0E AS
SELECT * 
      FROM IN_LD0.LD0P0E E
      WHERE EXISTS (SELECT 'X'
            FROM IN_LD0.LD0V0C C
            WHERE C.AEVERKNR = E.AEVERKNR 
		AND E.MAKDAT = ' ')
      OR EXISTS (SELECT 'X' 
          FROM IN_LD0.LD0V0C20 C20
          WHERE C20.INSNR = E.INSNR 
	     AND E.MAKDAT = ' ')
      OR (E.AEVERKNR = 0
	AND E.INSNR  = 0
	AND E.MAKDAT = ' ')
GO	      
CREATE VIEW IN_LD0.LD0V0E07 AS
SELECT * 
      FROM IN_LD0.LD0P0E07 E07
      WHERE EXISTS (SELECT 'X'
            FROM IN_LD0.LD0V0E E
            WHERE E.BESLUTNR = E07.BESLUTNR)
GO
CREATE VIEW IN_LD0.LD0V0E40 AS
SELECT * 
      FROM IN_LD0.LD0P0E40 E40
      WHERE EXISTS (SELECT 'X'
            FROM IN_LD0.LD0V0E E
            WHERE E.BESLUTNR = E40.BESLUTNR)
GO
CREATE VIEW IN_LD0.LD0V0F AS
SELECT * 
      FROM IN_LD0.LD0P0F F
      WHERE EXISTS (SELECT 'X'FROM IN_LD0.LD0V0C20 C20
                    WHERE C20.INSNR = F.INSNR)
GO
CREATE VIEW IN_LD0.LD0V0G AS
SELECT * 
      FROM IN_LD0.LD0P0G G
      WHERE EXISTS (SELECT 'X'FROM IN_LD0.LD0V0C20 C20
                    WHERE C20.INSNR = G.INSNR)
GO
CREATE VIEW IN_LD0.LD0V0H AS
SELECT * 
      FROM IN_LD0.LD0P0H H
      WHERE EXISTS (SELECT 'X'
            FROM IN_LD0.LD0V0C C
            WHERE C.AEVERKNR = H.AEVERKNR)          
      OR    EXISTS (SELECT 'X' 
            FROM IN_LD0.LD0V0C20 C20
            WHERE C20.INSNR = H.INSNR)
      OR    EXISTS(SELECT 'X'
            FROM IN_LD0.LD0V0AJD AJD
            WHERE AJD.VYNAMN = 'AKTUAL'
            AND H.AKTUAL >= AJD.JFRDTA1
            AND H.AKTUAL <= AJD.JFRDTA2
            AND H.ORGID = AJD.JFRDTA3
            AND H.ORGENHNR LIKE AJD.JFRDTA10
            AND H.AEVERKNR = 0
            AND H.INSNR = 0)
      WITH CHECK OPTION
GO
CREATE VIEW IN_LD0.LD0V0H10 AS
SELECT * 
      FROM IN_LD0.LD0P0H10 H10
      WHERE EXISTS (SELECT 'X'
            FROM IN_LD0.LD0V0AJD AJD
            WHERE AJD.VYNAMN='SAMTYCKE'
            AND H10.ORGID = AJD.JFRDTA3
            AND H10.ORGENHNR LIKE AJD.JFRDTA10
            AND H10.SAMTAVSER >= AJD.JFRDTA1
            AND H10.SAMTAVSER <= AJD.JFRDTA2)          
WITH CHECK OPTION
GO
CREATE VIEW IN_LD0.LD0V0H20 AS
SELECT * 
      FROM IN_LD0.LD0P0H20 H20
      WHERE EXISTS (SELECT 'X'
            FROM IN_LD0.LD0V0C C
            WHERE C.AEVERKNR = H20.AEVERKNR)
      OR EXISTS (SELECT 'X' 
          FROM IN_LD0.LD0V0C20 C20
          WHERE C20.INSNR = H20.INSNR)
GO
CREATE VIEW IN_LD0.LD0V0N AS
SELECT * 
      FROM IN_LD0.LD0P0N N
      WHERE EXISTS (SELECT 'X'FROM IN_LD0.LD0V0C20 C20
                    WHERE C20.INSNR = N.INSNR)
GO
CREATE VIEW IN_LD0.LD0V0N60 AS
SELECT * 
      FROM IN_LD0.LD0P0N60 N60
      WHERE EXISTS (SELECT 'X'FROM IN_LD0.LD0V0C20 C20
                    WHERE C20.INSNR = N60.INSNR)
GO
CREATE VIEW IN_LD0.LD0V0N70 AS
SELECT * 
      FROM IN_LD0.LD0P0N70 N70
      WHERE EXISTS (SELECT 'X'FROM IN_LD0.LD0V0C20 C20
                    WHERE C20.INSNR = N70.INSNR)
GO		
CREATE VIEW IN_LD0.LD0V0P
      AS SELECT * 
      FROM IN_LD0.LD0P0P P
      WHERE EXISTS(SELECT 'X'
            FROM IN_LD0.LD0V1AJD AJD
            WHERE AJD.VYNAMN = 'BEVAKNING'
            AND P.HAND LIKE AJD.JFRDTA10)
      OR EXISTS(SELECT 'X'
            FROM IN_LD0.LD0V1AJD AJD
            WHERE AJD.VYNAMN = 'BEVAKNING'
            AND P.MEDHAND LIKE AJD.JFRDTA10)
      WITH CHECK OPTION	
GO	      
CREATE VIEW IN_LD0.LD0V0O12
	AS SELECT * FROM IN_LD0.LD0P0O12 O12
	WHERE EXISTS
	(SELECT 'X' FROM IN_LD0.LD0V0C20 C20
        WHERE C20.INSNR = O12.INSNR)
GO
CREATE VIEW IN_LD0.LD0V0O10
	 AS SELECT * FROM IN_LD0.LD0P0O10 O10
 	 WHERE EXISTS
	 (SELECT 'X' FROM IN_LD0.LD0V0O12 O12
         WHERE O10.SAMFAKTNR = O12.SAMFAKTNR)
GO
CREATE VIEW IN_LD0.LD0V1T41
AS SELECT * FROM IN_LD0.LD0P0T41 T41
WHERE EXISTS (SELECT 'X' FROM IN_LD0.LD0V0C C
		WHERE C.AEVERKNR = T41.AGARNR AND
		      T41.AGARTYP = 21)
   OR EXISTS (SELECT 'X' FROM IN_LD0.LD0V0C C 
		WHERE C.AEVERKNR = T41.AGARNR AND
		      T41.AGARTYP = 22)
   OR EXISTS (SELECT 'X' FROM IN_LD0.LD0V0C20 C20 
		WHERE C20.INSNR = T41.AGARNR AND
		      T41.AGARTYP = 23)
   OR EXISTS (SELECT 'X' FROM IN_LD0.LD0V0H H 
		WHERE H.AKTUALNR = T41.AGARNR AND
		      T41.AGARTYP = 20)
   OR EXISTS (SELECT 'X' FROM IN_LD0.LD0V0L20 L20 
		WHERE L20.UPPDRNR = T41.AGARNR AND
		      T41.AGARTYP = 25)
   OR EXISTS (SELECT 'X' FROM IN_LD0.LD0V0A70 A70 
		WHERE A70.FLYKTNR = T41.AGARNR AND
		      T41.AGARTYP = 40)
GO
CREATE VIEW IN_LD0.GA1V0T41 
	AS SELECT * FROM IN_LD0.LD0P0T41 T41
		WHERE EXISTS (SELECT 'X' FROM IN_LD0.LD0V0C C
		WHERE C.AEVERKNR = T41.AGARNR AND T41.AGARTYP = 1)
	OR EXISTS (SELECT 'X' FROM IN_LD0.LD0V0C C
		WHERE C.AEVERKNR = T41.AGARNR AND T41.AGARTYP = 10)
	OR EXISTS (SELECT 'X' FROM IN_LD0.LD0V0C20 C20
		WHERE C20.INSNR = T41.AGARNR AND T41.AGARTYP = 7)
	OR EXISTS (SELECT 'X' FROM IN_LD0.LD0V0H H
		WHERE H.AKTUALNR = T41.AGARNR AND T41.AGARTYP = 14)
	OR EXISTS (SELECT 'X' FROM IN_LD0.LD0V0B B
		WHERE B.AKTNR = T41.AGARNR AND T41.AGARTYP = 19)
GO
CREATE VIEW IN_LD0.GA1V1TXT
	AS SELECT * FROM IN_LD0.GA1P0TXT TXT
		WHERE EXISTS (SELECT 'X' FROM IN_LD0.GA1V0T41 T41
				WHERE '7255.'+CAST(T41.DOKUMENTNR AS VARCHAR) = TXT.OBJEKTID)
GO
CREATE VIEW IN_LD0.LD0V0C40 AS 
SELECT *
FROM IN_LD0.LD0P0C40 C40
WHERE EXISTS (SELECT 'X'
      FROM IN_LD0.LD0V0C C
      WHERE C.FAMRATTNR = C40.FAMRATTNR)
   OR EXISTS (SELECT 'X'
      FROM IN_LD0.LD0V0H H
      WHERE H.FAMRATTNR = C40.FAMRATTNR)
GO
CREATE VIEW IN_LD0.LD0V0BVK AS SELECT * 
      FROM IN_LD0.LD0P0BVK BVK
      WHERE EXISTS(SELECT 'X'
            FROM IN_LD0.LD0V1AJD AJD
            WHERE AJD.VYNAMN = 'BEVAKTYP'
            AND BVK.BEVAKTYP >= AJD.JFRDTA1
            AND BVK.BEVAKTYP <= AJD.JFRDTA2
            AND BVK.HAND LIKE AJD.JFRDTA10)
GO
CREATE VIEW IN_LD0.PCCV0BVK AS SELECT * 
      FROM IN_LD0.LD0P0BVK BVK
      WHERE EXISTS(SELECT 'X'
            FROM IN_LD0.LD0V1AJD AJD
            WHERE AJD.VYNAMN = 'BEVAKTYP'
            AND BVK.BEVAKTYP >= AJD.JFRDTA1
            AND BVK.BEVAKTYP <= AJD.JFRDTA2
            AND BVK.HAND LIKE AJD.JFRDTA10)
GO
CREATE VIEW IN_LD0.LD0V0T60 AS 
SELECT * 
FROM IN_LD0.LD0P0T60 T60
WHERE EXISTS(SELECT 'X'
             FROM IN_LD0.LD0V0AJD AJD
             WHERE AJD.VYNAMN = 'SCANNING'
               AND T60.DOKTYP >= AJD.JFRDTA1
               AND T60.DOKTYP <= AJD.JFRDTA2
               AND T60.ORGID = AJD.JFRDTA3
               AND T60.ORGENHNR LIKE AJD.JFRDTA10) OR
      EXISTS(SELECT 'X'
             FROM IN_LD0.LD0V0H H
             WHERE T60.AGARNR = H.AKTUALNR
               AND T60.AEVERKNR = 0 
               AND T60.INSNR = 0) OR
      EXISTS(SELECT 'X'
             FROM IN_LD0.LD0V0C C
             WHERE T60.AEVERKNR = C.AEVERKNR) OR
      EXISTS(SELECT 'X'
             FROM IN_LD0.LD0V0C20 C20
             WHERE T60.INSNR = C20.INSNR)
GO

USE AC_LD0
GO
CREATE VIEW AC_LD0.LD0V1C
      AS SELECT * 
      FROM AC_LD0.LD0P0C C
      WHERE EXISTS(SELECT 'X'
            FROM AC_LD0.LD0V0AJD AJD
            WHERE AJD.VYNAMN = 'FUTRED'
            AND C.TMARK = 0
            AND C.AEVERKTYP >= AJD.JFRDTA1
            AND C.AEVERKTYP <= AJD.JFRDTA2
            AND C.ORGID = AJD.JFRDTA3
            AND C.ORGENHNR LIKE AJD.JFRDTA10)
GO		
CREATE VIEW AC_LD0.LD0V1C20
      AS SELECT * 
      FROM AC_LD0.LD0P0C20 C20
      WHERE EXISTS(SELECT 'X'
            FROM AC_LD0.LD0V0AJD AJD
            WHERE AJD.VYNAMN = 'FINSATS'
            AND C20.INSTYP >= AJD.JFRDTA1
            AND C20.INSTYP <= AJD.JFRDTA2
            AND C20.ORGID = AJD.JFRDTA3
            AND C20.ORGENHNR LIKE AJD.JFRDTA10)
GO				
CREATE VIEW AC_LD0.LD0V0C
      AS SELECT * 
      FROM AC_LD0.LD0P0C C
      WHERE EXISTS(SELECT 'X'
            FROM AC_LD0.LD0V0AJD AJD
            WHERE AJD.VYNAMN = 'UTREDNING'
            AND C.TMARK = 0
            AND C.AEVERKTYP >= AJD.JFRDTA1
            AND C.AEVERKTYP <= AJD.JFRDTA2
            AND C.ORGID = AJD.JFRDTA3
            AND C.ORGENHNR LIKE AJD.JFRDTA10)
      OR EXISTS(SELECT 'X'
            FROM AC_LD0.LD0V0AJD AJD
            WHERE AJD.VYNAMN = 'VERKSTÄLL'
            AND C.TMARK = 1
            AND C.AEVERKTYP >= AJD.JFRDTA1
            AND C.AEVERKTYP <= AJD.JFRDTA2
            AND C.ORGID = AJD.JFRDTA3
            AND C.ORGENHNR LIKE AJD.JFRDTA10)
      WITH CHECK OPTION
GO
CREATE VIEW AC_LD0.LD0V0C20
      AS SELECT * 
      FROM AC_LD0.LD0P0C20 C20
      WHERE EXISTS(SELECT 'X'
            FROM LD0V0AJD AJD
            WHERE AJD.VYNAMN = 'INSATS'
            AND C20.INSTYP >= AJD.JFRDTA1
            AND C20.INSTYP <= AJD.JFRDTA2
            AND C20.ORGID = AJD.JFRDTA3
            AND C20.ORGENHNR LIKE AJD.JFRDTA10)
	    WITH CHECK OPTION
GO
CREATE VIEW AC_LD0.LD0V0C10 AS
SELECT * 
      FROM AC_LD0.LD0P0C10 C10
      WHERE EXISTS (SELECT 'X'
            FROM AC_LD0.LD0V0C C
            WHERE C.AEVERKNR = C10.ARENDENR
            OR    C.AEVERKNR = C10.VERKNR)
GO
CREATE VIEW AC_LD0.LD0V0C11 AS
SELECT * 
      FROM AC_LD0.LD0P0C11 C11
      WHERE EXISTS (SELECT 'X'
            FROM AC_LD0.LD0V0C C
            WHERE C.AEVERKNR = C11.AEVERKNR
            AND   C11.INSNR = 0)
      OR    EXISTS (SELECT 'X' 
            FROM AC_LD0.LD0V0C20 C20
            WHERE C20.INSNR = C11.INSNR)
GO
CREATE VIEW AC_LD0.LD0V0B
	AS SELECT * FROM AC_LD0.LD0P0B B
	WHERE EXISTS
	(SELECT 'X' FROM AC_LD0.LD0V0C C
	WHERE B.AKTNR = C.AKTNR)
GO
CREATE VIEW AC_LD0.LD0V0D AS
SELECT * 
      FROM AC_LD0.LD0P0D D
      WHERE EXISTS (SELECT 'X'
            FROM AC_LD0.LD0V0C C
            WHERE C.AEVERKNR = D.AEVERKNR)
      OR EXISTS (SELECT 'X' 
          FROM AC_LD0.LD0V0C20 C20
          WHERE C20.INSNR = D.INSNR)
GO
CREATE VIEW AC_LD0.LD0V0E AS
SELECT * 
      FROM AC_LD0.LD0P0E E
      WHERE EXISTS (SELECT 'X'
            FROM AC_LD0.LD0V0C C
            WHERE C.AEVERKNR = E.AEVERKNR 
		AND E.MAKDAT = ' ')
      OR EXISTS (SELECT 'X' 
          FROM AC_LD0.LD0V0C20 C20
          WHERE C20.INSNR = E.INSNR 
	     AND E.MAKDAT = ' ')
      OR (E.AEVERKNR = 0
	AND E.INSNR  = 0
	AND E.MAKDAT = ' ')
GO	      
CREATE VIEW AC_LD0.LD0V0E07 AS
SELECT * 
      FROM AC_LD0.LD0P0E07 E07
      WHERE EXISTS (SELECT 'X'
            FROM AC_LD0.LD0V0E E
            WHERE E.BESLUTNR = E07.BESLUTNR)
GO
CREATE VIEW AC_LD0.LD0V0E40 AS
SELECT * 
      FROM AC_LD0.LD0P0E40 E40
      WHERE EXISTS (SELECT 'X'
            FROM AC_LD0.LD0V0E E
            WHERE E.BESLUTNR = E40.BESLUTNR)
GO
CREATE VIEW AC_LD0.LD0V0F AS
SELECT * 
      FROM AC_LD0.LD0P0F F
      WHERE EXISTS (SELECT 'X'FROM AC_LD0.LD0V0C20 C20
                    WHERE C20.INSNR = F.INSNR)
GO
CREATE VIEW AC_LD0.LD0V0G AS
SELECT * 
      FROM AC_LD0.LD0P0G G
      WHERE EXISTS (SELECT 'X'FROM AC_LD0.LD0V0C20 C20
                    WHERE C20.INSNR = G.INSNR)
GO
CREATE VIEW AC_LD0.LD0V0H AS
SELECT * 
      FROM AC_LD0.LD0P0H H
      WHERE EXISTS (SELECT 'X'
            FROM AC_LD0.LD0V0C C
            WHERE C.AEVERKNR = H.AEVERKNR)          
      OR    EXISTS (SELECT 'X' 
            FROM AC_LD0.LD0V0C20 C20
            WHERE C20.INSNR = H.INSNR)
      OR    EXISTS(SELECT 'X'
            FROM AC_LD0.LD0V0AJD AJD
            WHERE AJD.VYNAMN = 'AKTUAL'
            AND H.AKTUAL >= AJD.JFRDTA1
            AND H.AKTUAL <= AJD.JFRDTA2
            AND H.ORGID = AJD.JFRDTA3
            AND H.ORGENHNR LIKE AJD.JFRDTA10
            AND H.AEVERKNR = 0
            AND H.INSNR = 0)
      WITH CHECK OPTION
GO
CREATE VIEW AC_LD0.LD0V0H10 AS
SELECT * 
      FROM AC_LD0.LD0P0H10 H10
      WHERE EXISTS (SELECT 'X'
            FROM AC_LD0.LD0V0AJD AJD
            WHERE AJD.VYNAMN='SAMTYCKE'
            AND H10.ORGID = AJD.JFRDTA3
            AND H10.ORGENHNR LIKE AJD.JFRDTA10
            AND H10.SAMTAVSER >= AJD.JFRDTA1
            AND H10.SAMTAVSER <= AJD.JFRDTA2)          
WITH CHECK OPTION
GO
CREATE VIEW AC_LD0.LD0V0H20 AS
SELECT * 
      FROM AC_LD0.LD0P0H20 H20
      WHERE EXISTS (SELECT 'X'
            FROM AC_LD0.LD0V0C C
            WHERE C.AEVERKNR = H20.AEVERKNR)
      OR EXISTS (SELECT 'X' 
          FROM AC_LD0.LD0V0C20 C20
          WHERE C20.INSNR = H20.INSNR)
GO
CREATE VIEW AC_LD0.LD0V0N AS
SELECT * 
      FROM AC_LD0.LD0P0N N
      WHERE EXISTS (SELECT 'X'FROM AC_LD0.LD0V0C20 C20
                    WHERE C20.INSNR = N.INSNR)
GO
CREATE VIEW AC_LD0.LD0V0N60 AS
SELECT * 
      FROM AC_LD0.LD0P0N60 N60
      WHERE EXISTS (SELECT 'X'FROM AC_LD0.LD0V0C20 C20
                    WHERE C20.INSNR = N60.INSNR)
GO
CREATE VIEW AC_LD0.LD0V0N70 AS
SELECT * 
      FROM AC_LD0.LD0P0N70 N70
      WHERE EXISTS (SELECT 'X'FROM AC_LD0.LD0V0C20 C20
                    WHERE C20.INSNR = N70.INSNR)
GO		
CREATE VIEW AC_LD0.LD0V0P
      AS SELECT * 
      FROM AC_LD0.LD0P0P P
      WHERE EXISTS(SELECT 'X'
            FROM AC_LD0.LD0V1AJD AJD
            WHERE AJD.VYNAMN = 'BEVAKNING'
            AND P.HAND LIKE AJD.JFRDTA10)
      OR EXISTS(SELECT 'X'
            FROM AC_LD0.LD0V1AJD AJD
            WHERE AJD.VYNAMN = 'BEVAKNING'
            AND P.MEDHAND LIKE AJD.JFRDTA10)
      WITH CHECK OPTION	
GO	      
CREATE VIEW AC_LD0.LD0V0O12
	AS SELECT * FROM AC_LD0.LD0P0O12 O12
	WHERE EXISTS
	(SELECT 'X' FROM AC_LD0.LD0V0C20 C20
        WHERE C20.INSNR = O12.INSNR)
GO
CREATE VIEW AC_LD0.LD0V0O10
	 AS SELECT * FROM AC_LD0.LD0P0O10 O10
 	 WHERE EXISTS
	 (SELECT 'X' FROM AC_LD0.LD0V0O12 O12
         WHERE O10.SAMFAKTNR = O12.SAMFAKTNR)
GO
CREATE VIEW AC_LD0.LD0V1T41
AS SELECT * FROM AC_LD0.LD0P0T41 T41
WHERE EXISTS (SELECT 'X' FROM AC_LD0.LD0V0C C
		WHERE C.AEVERKNR = T41.AGARNR AND
		      T41.AGARTYP = 21)
   OR EXISTS (SELECT 'X' FROM AC_LD0.LD0V0C C 
		WHERE C.AEVERKNR = T41.AGARNR AND
		      T41.AGARTYP = 22)
   OR EXISTS (SELECT 'X' FROM AC_LD0.LD0V0C20 C20 
		WHERE C20.INSNR = T41.AGARNR AND
		      T41.AGARTYP = 23)
   OR EXISTS (SELECT 'X' FROM AC_LD0.LD0V0H H 
		WHERE H.AKTUALNR = T41.AGARNR AND
		      T41.AGARTYP = 20)
   OR EXISTS (SELECT 'X' FROM AC_LD0.LD0V0L20 L20 
		WHERE L20.UPPDRNR = T41.AGARNR AND
		      T41.AGARTYP = 25)
   OR EXISTS (SELECT 'X' FROM AC_LD0.LD0V0A70 A70 
		WHERE A70.FLYKTNR = T41.AGARNR AND
		      T41.AGARTYP = 40)
GO
CREATE VIEW AC_LD0.GA1V0T41 
	AS SELECT * FROM AC_LD0.LD0P0T41 T41
		WHERE EXISTS (SELECT 'X' FROM AC_LD0.LD0V0C C
		WHERE C.AEVERKNR = T41.AGARNR AND T41.AGARTYP = 1)
	OR EXISTS (SELECT 'X' FROM AC_LD0.LD0V0C C
		WHERE C.AEVERKNR = T41.AGARNR AND T41.AGARTYP = 10)
	OR EXISTS (SELECT 'X' FROM AC_LD0.LD0V0C20 C20
		WHERE C20.INSNR = T41.AGARNR AND T41.AGARTYP = 7)
	OR EXISTS (SELECT 'X' FROM AC_LD0.LD0V0H H
		WHERE H.AKTUALNR = T41.AGARNR AND T41.AGARTYP = 14)
	OR EXISTS (SELECT 'X' FROM AC_LD0.LD0V0B B
		WHERE B.AKTNR = T41.AGARNR AND T41.AGARTYP = 19)
GO
CREATE VIEW AC_LD0.GA1V1TXT
	AS SELECT * FROM AC_LD0.GA1P0TXT TXT
		WHERE EXISTS (SELECT 'X' FROM AC_LD0.GA1V0T41 T41
				WHERE '7255.'+CAST(T41.DOKUMENTNR AS VARCHAR) = TXT.OBJEKTID)
GO
CREATE VIEW AC_LD0.LD0V0C40 AS 
SELECT *
FROM AC_LD0.LD0P0C40 C40
WHERE EXISTS (SELECT 'X'
      FROM AC_LD0.LD0V0C C
      WHERE C.FAMRATTNR = C40.FAMRATTNR)
   OR EXISTS (SELECT 'X'
      FROM AC_LD0.LD0V0H H
      WHERE H.FAMRATTNR = C40.FAMRATTNR)
GO
CREATE VIEW AC_LD0.LD0V0BVK AS SELECT * 
      FROM AC_LD0.LD0P0BVK BVK
      WHERE EXISTS(SELECT 'X'
            FROM AC_LD0.LD0V1AJD AJD
            WHERE AJD.VYNAMN = 'BEVAKTYP'
            AND BVK.BEVAKTYP >= AJD.JFRDTA1
            AND BVK.BEVAKTYP <= AJD.JFRDTA2
            AND BVK.HAND LIKE AJD.JFRDTA10)
GO
CREATE VIEW AC_LD0.PCCV0BVK AS SELECT * 
      FROM AC_LD0.LD0P0BVK BVK
      WHERE EXISTS(SELECT 'X'
            FROM AC_LD0.LD0V1AJD AJD
            WHERE AJD.VYNAMN = 'BEVAKTYP'
            AND BVK.BEVAKTYP >= AJD.JFRDTA1
            AND BVK.BEVAKTYP <= AJD.JFRDTA2
            AND BVK.HAND LIKE AJD.JFRDTA10)
GO
CREATE VIEW AC_LD0.LD0V0T60 AS 
SELECT * 
FROM AC_LD0.LD0P0T60 T60
WHERE EXISTS(SELECT 'X'
             FROM AC_LD0.LD0V0AJD AJD
             WHERE AJD.VYNAMN = 'SCANNING'
               AND T60.DOKTYP >= AJD.JFRDTA1
               AND T60.DOKTYP <= AJD.JFRDTA2
               AND T60.ORGID = AJD.JFRDTA3
               AND T60.ORGENHNR LIKE AJD.JFRDTA10) OR
      EXISTS(SELECT 'X'
             FROM AC_LD0.LD0V0H H
             WHERE T60.AGARNR = H.AKTUALNR
               AND T60.AEVERKNR = 0 
               AND T60.INSNR = 0) OR
      EXISTS(SELECT 'X'
             FROM AC_LD0.LD0V0C C
             WHERE T60.AEVERKNR = C.AEVERKNR) OR
      EXISTS(SELECT 'X'
             FROM AC_LD0.LD0V0C20 C20
             WHERE T60.INSNR = C20.INSNR)
GO
