Tables [dbo].[Container]
Properties
PropertyValue
CollationSQL_Latin1_General_CP1_CI_AS
HeapYes
Row Count (~)0
Created10:56:48 AM Monday, May 4, 2015
Last Modified1:55:15 PM Wednesday, April 10, 2024
Columns
NameData TypeMax Length (Bytes)Allow NullsIdentityDefault
Primary Key PK_Container: ContainerIDContainerIDint4
No
1 - 1
Indexes IX_Container_ContainerNumber: OrderFID\ContainerNumberContainerNumbervarchar(32)32
No
Foreign Keys FK_Container_Branch: [dbo].[Branch].BranchFIDBranchFIDint4
No
Foreign Keys FK_Container_Order: [dbo].[Orders].OrderFIDIndexes IX_Container_ContainerNumber: OrderFID\ContainerNumberIndexes IX_Container_OrderFID: OrderFIDOrderFIDint4
Yes
Foreign Keys FK_Container_ContainerType: [dbo].[ContainerType].ContainerTypeFIDContainerTypeFIDint4
No
Lengthdecimal(10,2)9
Yes
Widthdecimal(10,2)9
Yes
Heightdecimal(10,2)9
Yes
TareWeightint4
Yes
Foreign Keys FK_Container_ContainerStatus: [dbo].[ContainerStatus].ContainerStatusFIDIndexes ix_Container_ContainerStatus: ContainerStatusFIDContainerStatusFIDint4
No
Activebit1
No
((1))
Foreign Keys FK_Container_Ownership: [dbo].[Ownership].OwnershipFIDOwnershipFIDint4
Yes
Foreign Keys FK_Container_LastChangedBy: [dbo].[Sysuser].LastChangedByFIDLastChangedByFIDint4
No
CurrentLocationvarchar(256)256
Yes
Costdecimal(15,2)9
Yes
Notevarchar(1024)1024
Yes
Indexes Indexes
NameColumnsUnique
Primary Key PK_Container: ContainerIDPK_ContainerContainerID
Yes
IX_Container_ContainerNumberOrderFID, ContainerNumber
ix_Container_ContainerStatusContainerStatusFID
IX_Container_OrderFIDOrderFID
Triggers Triggers
NameANSI Nulls OnQuoted Identifier OnOn
ContainerInsertTrigger
Yes
Yes
After Insert
ContainerUpdateTrigger
Yes
Yes
After Update
Foreign Keys Foreign Keys
NameColumns
FK_Container_BranchBranchFID->[dbo].[Branch].[BranchPriKey]
FK_Container_ContainerStatusContainerStatusFID->[dbo].[ContainerStatus].[ContainerStatusID]
FK_Container_ContainerTypeContainerTypeFID->[dbo].[ContainerType].[ContainerTypeID]
FK_Container_LastChangedByLastChangedByFID->[dbo].[Sysuser].[SysUserID]
FK_Container_OrderOrderFID->[dbo].[Orders].[PriKey]
FK_Container_OwnershipOwnershipFID->[dbo].[Ownership].[OwnershipID]
Permissions
TypeActionOwning Principal
GrantDeleteMssExec
GrantInsertMssExec
GrantSelectMssExec
GrantUpdateMssExec
SQL Script
CREATE TABLE [dbo].[Container]
(
[ContainerID] [int] NOT NULL IDENTITY(1, 1),
[ContainerNumber] [varchar] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[BranchFID] [int] NOT NULL,
[OrderFID] [int] NULL,
[ContainerTypeFID] [int] NOT NULL,
[Length] [decimal] (10, 2) NULL,
[Width] [decimal] (10, 2) NULL,
[Height] [decimal] (10, 2) NULL,
[TareWeight] [int] NULL,
[ContainerStatusFID] [int] NOT NULL,
[Active] [bit] NOT NULL CONSTRAINT [DF_Container_Active] DEFAULT ((1)),
[OwnershipFID] [int] NULL,
[LastChangedByFID] [int] NOT NULL,
[CurrentLocation] [varchar] (256) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Cost] [decimal] (15, 2) NULL,
[Note] [varchar] (1024) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
GO

/**
*    This "for insert" trigger inserts a Container History record telling us that the container was created.
*/

create trigger [dbo].[ContainerInsertTrigger] on [dbo].[Container] for insert
as
if 0 < ( select count(*) from inserted )
begin
    insert into ContainerHistory
    (
        ContainerFID,
        ActivityDescription,
        SysuserFID,
        DateEntered
    )
    select
        ContainerFID = ContainerID,
        ActivityDescription = 'Created',
        SysuserFID = LastChangedByFID,
        DateEntered = getdate()
    from inserted
end
GO

/**
*    This "for update" trigger logs changes to the Container table.
*/

CREATE trigger [dbo].[ContainerUpdateTrigger] on [dbo].[Container] for update
as
    if update( ContainerNumber ) and exists ( select 1 from inserted inner join deleted on inserted.ContainerID = deleted.ContainerID where inserted.ContainerNumber != deleted.ContainerNumber )
    begin
        insert into ContainerHistory
        (
            ContainerFID,
            ActivityDescription,
            OldValue,
            NewValue,
            SysuserFID,
            DateEntered
        )
        select
            ContainerFID = inserted.ContainerID,
            ActivityDescription = 'Container',
            OldValue = deleted.ContainerNumber,
            NewValue = inserted.ContainerNumber,
            SysuserFID = inserted.LastChangedByFID,
            DateEntered = getdate()
        from inserted
        inner join deleted on inserted.ContainerID = deleted.ContainerID     
    end  
    if update( BranchFID ) and exists ( select 1 from inserted inner join deleted on inserted.ContainerID = deleted.ContainerID where inserted.BranchFID != deleted.BranchFID )
    begin
        insert into ContainerHistory
        (
            ContainerFID,
            ActivityDescription,
            OldValue,
            NewValue,
            SysuserFID,
            DateEntered
        )
        select
            ContainerFID = inserted.ContainerID,
            ActivityDescription = 'Branch',
            OldValue = OldBranch.BranchID,
            NewValue = NewBranch.BranchID,
            SysuserFID = inserted.LastChangedByFID,
            DateEntered = getdate()
        from inserted
        inner join deleted on inserted.ContainerID = deleted.ContainerID
        inner join Branch OldBranch on deleted.BranchFID = OldBranch.BranchPriKey
        inner join Branch NewBranch on inserted.BranchFID = NewBranch.BranchPriKey     
    end  
    if update( OrderFID ) and exists ( select 1 from inserted inner join deleted on inserted.ContainerID = deleted.ContainerID where inserted.OrderFID is not null and deleted.OrderFID is null )
    begin
        insert into ContainerHistory
        (
            ContainerFID,
            ActivityDescription,
            OldValue,
            NewValue,
            SysuserFID,
            DateEntered,
            OrderFID
        )
        select
            ContainerFID = inserted.ContainerID,
            ActivityDescription = 'Assigned',
            OldValue = 'Not set',
            NewValue = Orders.OrderNo,
            SysuserFID = inserted.LastChangedByFID,
            DateEntered = getdate(),
            OrderFID = inserted.OrderFID
        from inserted
        inner join deleted on inserted.ContainerID = deleted.ContainerID
        inner join Orders on inserted.OrderFID = Orders.PriKey
    end  
    if update( OrderFID ) and exists ( select 1 from inserted inner join deleted on inserted.ContainerID = deleted.ContainerID where inserted.OrderFID is null and deleted.OrderFID is not null )
    begin
        insert into ContainerHistory
        (
            ContainerFID,
            ActivityDescription,
            OldValue,
            NewValue,
            SysuserFID,
            DateEntered
        )
        select
            ContainerFID = inserted.ContainerID,
            ActivityDescription = 'Released',
            OldValue = Orders.OrderNo,
            NewValue = 'Not set',
            SysuserFID = inserted.LastChangedByFID,
            DateEntered = getdate()
        from inserted
        inner join deleted on inserted.ContainerID = deleted.ContainerID
        inner join Orders on deleted.OrderFID = Orders.PriKey
    end  
    if update( ContainerTypeFID ) and exists ( select 1 from inserted inner join deleted on inserted.ContainerID = deleted.ContainerID where inserted.ContainerTypeFID != deleted.ContainerTypeFID )
    begin
        insert into ContainerHistory
        (
            ContainerFID,
            ActivityDescription,
            OldValue,
            NewValue,
            SysuserFID,
            DateEntered
        )
        select
            ContainerFID = inserted.ContainerID,
            ActivityDescription = 'Type',
            OldValue = OldContainerType.description,
            NewValue = NewContainerType.description,
            SysuserFID = inserted.LastChangedByFID,
            DateEntered = getdate()
        from inserted
        inner join deleted on inserted.ContainerID = deleted.ContainerID
        inner join ContainerType OldContainerType on deleted.ContainerTypeFID = OldContainerType.ContainerTypeID
        inner join ContainerType NewContainerType on inserted.ContainerTypeFID = NewContainerType.ContainerTypeID
    end  
    if update( Length ) and exists ( select 1 from inserted inner join deleted on inserted.ContainerID = deleted.ContainerID where inserted.Length != deleted.Length )
    begin
        insert into ContainerHistory
        (
            ContainerFID,
            ActivityDescription,
            OldValue,
            NewValue,
            SysuserFID,
            DateEntered
        )
        select
            ContainerFID = inserted.ContainerID,
            ActivityDescription = 'Length',
            OldValue = isnull( deleted.Length, 'Not set' ),
            NewValue = isnull( inserted.Length, 'Not set' ),
            SysuserFID = inserted.LastChangedByFID,
            DateEntered = getdate()
        from inserted
        inner join deleted on inserted.ContainerID = deleted.ContainerID
    end  
    if update( Width ) and exists ( select 1 from inserted inner join deleted on inserted.ContainerID = deleted.ContainerID where inserted.Width != deleted.Width )
    begin
        insert into ContainerHistory
        (
            ContainerFID,
            ActivityDescription,
            OldValue,
            NewValue,
            SysuserFID,
            DateEntered
        )
        select
            ContainerFID = inserted.ContainerID,
            ActivityDescription = 'Width',
            OldValue = isnull( deleted.Width, 'Not set' ),
            NewValue = isnull( inserted.Width, 'Not set' ),
            SysuserFID = inserted.LastChangedByFID,
            DateEntered = getdate()
        from inserted
        inner join deleted on inserted.ContainerID = deleted.ContainerID
    end  
    if update( Height ) and exists ( select 1 from inserted inner join deleted on inserted.ContainerID = deleted.ContainerID where inserted.Height != deleted.Height )
    begin
        insert into ContainerHistory
        (
            ContainerFID,
            ActivityDescription,
            OldValue,
            NewValue,
            SysuserFID,
            DateEntered
        )
        select
            ContainerFID = inserted.ContainerID,
            ActivityDescription = 'Height',
            OldValue = isnull( deleted.Height, 'Not set' ),
            NewValue = isnull( inserted.Height, 'Not set' ),
            SysuserFID = inserted.LastChangedByFID,
            DateEntered = getdate()
        from inserted
        inner join deleted on inserted.ContainerID = deleted.ContainerID
    end  
    if update( TareWeight ) and exists ( select 1 from inserted inner join deleted on inserted.ContainerID = deleted.ContainerID where inserted.TareWeight != deleted.TareWeight )
    begin
        insert into ContainerHistory
        (
            ContainerFID,
            ActivityDescription,
            OldValue,
            NewValue,
            SysuserFID,
            DateEntered
        )
        select
            ContainerFID = inserted.ContainerID,
            ActivityDescription = 'Tare Weight',
            OldValue = isnull( deleted.TareWeight, 'Not set' ),
            NewValue = isnull( inserted.TareWeight, 'Not set' ),
            SysuserFID = inserted.LastChangedByFID,
            DateEntered = getdate()
        from inserted
        inner join deleted on inserted.ContainerID = deleted.ContainerID
    end  
    if update( ContainerStatusFID ) and exists ( select 1 from inserted inner join deleted on inserted.ContainerID = deleted.ContainerID where inserted.ContainerStatusFID != deleted.ContainerStatusFID )
    begin
        insert into ContainerHistory
        (
            ContainerFID,
            ActivityDescription,
            OldValue,
            NewValue,
            SysuserFID,
            DateEntered
        )
        select
            ContainerFID = inserted.ContainerID,
            ActivityDescription = 'Status',
            OldValue = OldContainerStatus.Description,
            NewValue = NewContainerStatus.Description,
            SysuserFID = inserted.LastChangedByFID,
            DateEntered = getdate()
        from inserted
        inner join deleted on inserted.ContainerID = deleted.ContainerID
        inner join ContainerStatus OldContainerStatus on deleted.ContainerStatusFID = OldContainerStatus.ContainerStatusID
        inner join ContainerStatus NewContainerStatus on inserted.ContainerStatusFID = NewContainerStatus.ContainerStatusID
    end  
    if update( Active ) and exists ( select 1 from inserted inner join deleted on inserted.ContainerID = deleted.ContainerID where inserted.Active != deleted.Active )
    begin
        insert into ContainerHistory
        (
            ContainerFID,
            ActivityDescription,
            OldValue,
            NewValue,
            SysuserFID,
            DateEntered
        )
        select
            ContainerFID = inserted.ContainerID,
            ActivityDescription = 'Active',
            OldValue = case deleted.active when 1 then 'Active' else 'Inactive' end,
            NewValue = case inserted.Active when 1 then 'Active' else 'Inactive' end,
            SysuserFID = inserted.LastChangedByFID,
            DateEntered = getdate()
        from inserted
        inner join deleted on inserted.ContainerID = deleted.ContainerID
    end  
    if update( OwnershipFID ) and exists ( select 1 from inserted inner join deleted on inserted.ContainerID = deleted.ContainerID where isnull( inserted.OwnershipFID, 0 ) != isnull( deleted.OwnershipFID, 0 ) )
    begin
        insert into ContainerHistory
        (
            ContainerFID,
            ActivityDescription,
            OldValue,
            NewValue,
            SysuserFID,
            DateEntered
        )
        select
            ContainerFID = inserted.ContainerID,
            ActivityDescription = 'Ownership',
            OldValue = isnull( OldOwnership.description, 'Not set' ),
            NewValue = isnull( NewOwnership.Description, 'Not set' ),
            SysuserFID = inserted.LastChangedByFID,
            DateEntered = getdate()
        from inserted
        inner join deleted on inserted.ContainerID = deleted.ContainerID
        left outer join Ownership OldOwnership on deleted.OwnershipFID = OldOwnership.OwnershipID
        left outer join Ownership NewOwnership on inserted.OwnershipFID = NewOwnership.OwnershipID
    end  
    if update( CurrentLocation ) and exists ( select 1 from inserted inner join deleted on inserted.ContainerID = deleted.ContainerID where isnull( inserted.CurrentLocation, '' ) != isnull( deleted.CurrentLocation, '' ) )
    begin
        insert into ContainerHistory
        (
            ContainerFID,
            ActivityDescription,
            OldValue,
            NewValue,
            SysuserFID,
            DateEntered
        )
        select
            ContainerFID = inserted.ContainerID,
            ActivityDescription = 'Location',
            OldValue = isnull( deleted.CurrentLocation, 'Not set' ),
            NewValue = isnull( inserted.CurrentLocation, 'Not set' ),
            SysuserFID = inserted.LastChangedByFID,
            DateEntered = getdate()
        from inserted
        inner join deleted on inserted.ContainerID = deleted.ContainerID
    end  
    if update( Cost ) and exists ( select 1 from inserted inner join deleted on inserted.ContainerID = deleted.ContainerID where inserted.Cost !=  deleted.Cost )
    begin
        insert into ContainerHistory
        (
            ContainerFID,
            ActivityDescription,
            OldValue,
            NewValue,
            SysuserFID,
            DateEntered
        )
        select
            ContainerFID = inserted.ContainerID,
            ActivityDescription = 'Cost',
            OldValue = isnull( deleted.Cost, 'Not set' ),
            NewValue = isnull( inserted.Cost, 'Not set' ),
            SysuserFID = inserted.LastChangedByFID,
            DateEntered = getdate()
        from inserted
        inner join deleted on inserted.ContainerID = deleted.ContainerID
    end
    if update( Note ) and exists ( select 1 from inserted inner join deleted on inserted.ContainerID = deleted.ContainerID where isnull( inserted.Note, '' ) != isnull( deleted.Note, '' ) )
    begin
        insert into ContainerHistory
        (
            ContainerFID,
            ActivityDescription,
            OldValue,
            NewValue,
            SysuserFID,
            DateEntered
        )
        select
            ContainerFID = inserted.ContainerID,
            ActivityDescription = 'Note',
            OldValue = isnull( deleted.Note, 'Not set' ),
            NewValue = isnull( inserted.Note, 'Not set' ),
            SysuserFID = inserted.LastChangedByFID,
            DateEntered = getdate()
        from inserted
        inner join deleted on inserted.ContainerID = deleted.ContainerID
    end
GO
ALTER TABLE [dbo].[Container] ADD CONSTRAINT [PK_Container] PRIMARY KEY NONCLUSTERED  ([ContainerID]) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_Container_ContainerNumber] ON [dbo].[Container] ([ContainerNumber]) INCLUDE ([OrderFID]) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [ix_Container_ContainerStatus] ON [dbo].[Container] ([ContainerStatusFID]) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_Container_OrderFID] ON [dbo].[Container] ([OrderFID]) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Container] ADD CONSTRAINT [FK_Container_Branch] FOREIGN KEY ([BranchFID]) REFERENCES [dbo].[Branch] ([BranchPriKey])
GO
ALTER TABLE [dbo].[Container] ADD CONSTRAINT [FK_Container_ContainerStatus] FOREIGN KEY ([ContainerStatusFID]) REFERENCES [dbo].[ContainerStatus] ([ContainerStatusID])
GO
ALTER TABLE [dbo].[Container] ADD CONSTRAINT [FK_Container_ContainerType] FOREIGN KEY ([ContainerTypeFID]) REFERENCES [dbo].[ContainerType] ([ContainerTypeID])
GO
ALTER TABLE [dbo].[Container] ADD CONSTRAINT [FK_Container_LastChangedBy] FOREIGN KEY ([LastChangedByFID]) REFERENCES [dbo].[Sysuser] ([SysUserID])
GO
ALTER TABLE [dbo].[Container] ADD CONSTRAINT [FK_Container_Order] FOREIGN KEY ([OrderFID]) REFERENCES [dbo].[Orders] ([PriKey])
GO
ALTER TABLE [dbo].[Container] ADD CONSTRAINT [FK_Container_Ownership] FOREIGN KEY ([OwnershipFID]) REFERENCES [dbo].[Ownership] ([OwnershipID])
GO
GRANT SELECT ON  [dbo].[Container] TO [MssExec]
GRANT INSERT ON  [dbo].[Container] TO [MssExec]
GRANT DELETE ON  [dbo].[Container] TO [MssExec]
GRANT UPDATE ON  [dbo].[Container] TO [MssExec]
GO
Uses
Used By