Print ''
Print '-------------------------------------------------------------------------'
Print ' using Database: #DB_N#'
Print ' Table         : [Items_tbl]'
Print ' Creation Date : 22.03.2006'
Print ' Autor         : J.Feuerstein'
Print ' '
Print '-------------------------------------------------------------------------'

use [#DB_N#]


Declare @MyActionMsg1 nvarchar (max)
--------------------------------------------------------------------------------
-- verzweigen auf TableCreate: oder TableUpdate:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Items_tbl]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
  BEGIN
   goto TableUpdate
  END
ELSE
  BEGIN
   goto TableCreate
  END

--------------------------------------------------------------------------------
TableCreate:
  set @MyActionMsg1=
      '-- Table: [Items_tbl] dose not exist and will be created new'
  Print (@MyActionMsg1)
  CREATE TABLE [dbo].[Items_tbl] (
         [Id]                             uniqueidentifier NOT NULL,
         [ItemNameRev]                    nvarchar(128) ,
         [ItemUgMasterType]               nvarchar(3) ,  -- 'SP' = Standard Part ; 'AS' = Assemply ;
         [RfJobClass]                     nvarchar(3) ,  -- JobClass = selector für JobControl
         [RefileProcessState]             nvarchar(2) ,  -- 'N' = Non 'P' = in Process 'D' = Done 'X' = eXcludet 'M' = Process in Manuell Mode
         [IRRfLastJobComputer]            nvarchar(64) , -- IR Rf Last Computer an dem der Rf gestartet wurde
         [IRRfLastRefileProcessPaketName] nvarchar(64) , -- IR Rf Last RefileProcessPaketName hinzugefügt 27.04.2007 J.Fes
         [IRRfLastJobDateTimeStart]       datetime,      -- IR Rf Last DateTime an dem der Rf gestartet wurde
         [IRRfLastJobDateTimeFinish]      datetime,      -- IR Rf Last DateTime an dem der Rf beendet wurde
         [ItemUgMasterFs_Counts]          int,           -- anzahl der Hitcounts auf dem Master
         [ItemUgMasterCompCount]          int,           -- anzahl der verbauten Komponenten wenn es eine Baugruppe ist
         [ItemUgMasterAPLevels]           int,           -- = anzahl der verbauung Ebenen wenn es eine Baugruppe ist
         [ItemUgMasterLastModDate]        datetime,
         [ItemVolume]                     nvarchar(50) ,
         [ItemDataSetFromSiteID]          nvarchar(32) ,  -- die Site auf dem die ItemRev gezogen wurde
         [ItemOwningSiteID]               nvarchar(32) ,  -- Owning Site
         [RefileProcessOnSiteID]          nvarchar(32) ,
         [ItemFs_DPNE]                    nvarchar(255) ,
         [ItemPartFamilieMemberName]      nvarchar(32) ,
         [ItemOwner]                      nvarchar(32) ,
         [ItemGroup]                      nvarchar(32) ,
         [ItemLastModUser]                nvarchar(32) ,
         [ItemCreateDate]                 datetime,
         [ItemName]                       nvarchar(128) ,
         [ItemRev]                        nvarchar(50) ,
         [TceDB_puid]                     nvarchar(128) ,
         [SelectByGroup]                  nvarchar(32)  , -- fuer Teile selection
         [Note]                           nvarchar(255) ,
         CONSTRAINT [ItemNameRevItemDataSetFromSiteID_uq] UNIQUE ([ItemNameRev], [ItemDataSetFromSiteID]),
         CONSTRAINT [Items_tbl_pk] PRIMARY KEY NONCLUSTERED ([Id])
  )
  ON [PRIMARY]

  CREATE NONCLUSTERED INDEX [ItemUgMasterType_idx] ON [dbo].[Items_tbl]
    ([ItemUgMasterType])
  WITH (
    PAD_INDEX = OFF,
    DROP_EXISTING = OFF,
    STATISTICS_NORECOMPUTE = OFF,
    SORT_IN_TEMPDB = OFF,
    ONLINE = OFF,
    ALLOW_ROW_LOCKS = ON,
    ALLOW_PAGE_LOCKS = ON)
  ON [PRIMARY]

  CREATE NONCLUSTERED INDEX [ItemNameRev_IDX] ON [dbo].[Items_tbl]
    ([ItemNameRev])
  WITH (
    PAD_INDEX = OFF,
    DROP_EXISTING = OFF,
    STATISTICS_NORECOMPUTE = OFF,
    SORT_IN_TEMPDB = OFF,
    ONLINE = OFF,
    ALLOW_ROW_LOCKS = ON,
    ALLOW_PAGE_LOCKS = ON)
  ON [PRIMARY]

  -- Umgestellt 31.05.2007 14:42:14  ItemDataSetFromSiteID
  CREATE NONCLUSTERED INDEX [Item_NameRev_DataSetFromSiteID_IDX] ON [dbo].[Items_tbl]
    ([ItemNameRev], [ItemDataSetFromSiteID])
  WITH (
    PAD_INDEX = OFF,
    DROP_EXISTING = OFF,
    STATISTICS_NORECOMPUTE = OFF,
    SORT_IN_TEMPDB = OFF,
    ONLINE = OFF,
    ALLOW_ROW_LOCKS = ON,
    ALLOW_PAGE_LOCKS = ON)
  ON [PRIMARY]


  Goto EndeExit

--------------------------------------------------------------------------------
TableUpdate:

  set @MyActionMsg1=
       '-- Table: [Items_tbl] dose exist and will be updated'
  Print (@MyActionMsg1)

  Declare @ColumeName nvarchar (256)
  Declare @ColumeNameOld nvarchar (256)
  Declare @ColumeNameNew nvarchar (256)
  Declare @ExecuteStr nvarchar (max)



--------------------------------------------------------------------------------
EndeExit:
 -- Print '------------------------------------------------------------------------'
 -- Print (@MyActionMsg1)
 Print ('   |-> Done on Database: #DB_N#')
 Print '------------------------------------------------------------------------'

GO