-- Drop the view LD0VAA0ParticipantRFID if it exists
IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0VAA0ParticipantRFID')
BEGIN
  DROP VIEW LD0VAA0ParticipantRFID
END
GO

-- Drop the table LD0P0ParticipantRFID if it exists
IF EXISTS (SELECT 1 FROM SYS.TABLES WHERE NAME='LD0P0ParticipantRFID')
BEGIN
  DROP TABLE LD0P0ParticipantRFID
END
GO

-- Drop the view LD0VAA0CheckInOutTag if it exists
IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0VAA0CheckInOutTag')
BEGIN
  DROP VIEW LD0VAA0CheckInOutTag
END

-- Create the table LD0P0CheckInOutTag if it does not exist, otherwise drop the columns PLATSTYP and PLATSNR if they exist
IF NOT EXISTS (SELECT 1 FROM SYS.TABLES WHERE NAME='LD0P0CheckInOutTag')
BEGIN
  CREATE TABLE LD0P0CheckInOutTag(
    [LOPNR] [decimal](9, 0) IDENTITY(1,1) NOT NULL,
    [PERSONNR] [varchar](12) NOT NULL,
    [SERIENR] [varchar](20) NOT NULL,
    [TAGGNAMN] [varchar](20) NOT NULL,
  CONSTRAINT PK_LD0P0CheckInOutTag PRIMARY KEY (LOPNR)
) ON [PRIMARY]
END
ELSE
BEGIN
    -- Drop the column PLATSTYP if it exists
    IF EXISTS (SELECT 1 FROM sys.columns WHERE object_id = OBJECT_ID(N'LD0P0CheckInOutTag') AND name = 'PLATSTYP')
      ALTER TABLE LD0P0CheckInOutTag DROP COLUMN PLATSTYP;
    -- Drop the column PLATSNR if it exists
    IF EXISTS (SELECT 1 FROM sys.columns WHERE object_id = OBJECT_ID(N'LD0P0CheckInOutTag') AND name = 'PLATSNR')
      ALTER TABLE LD0P0CheckInOutTag DROP COLUMN PLATSNR;
END
GO

-- Create the view LD0VAA0CheckInOutTag based on the table LD0P0CheckInOutTag
CREATE VIEW LD0VAA0CheckInOutTag
  AS SELECT * FROM LD0P0CheckInOutTag
GO

GRANT ALL ON LD0VAA0CheckInOutTag TO LD0ROANV
GO