1

I have a table ImportHistory in which I store history of importation. (Each time the user upload a file I store a row).

CREATE TABLE [dbo].[ImportHistory]
(
    [Id]   INT  IDENTITY (1, 1) NOT NULL,
    [Date] TIMESTAMP NOT NULL, 
    CONSTRAINT [PK_ImportHistory] PRIMARY KEY ([Id])
)

And I have also

CREATE TABLE [dbo].[Sales] (
    [Id]              VARCHAR (150)   NOT NULL,
  ...
    [ImportHistoryId] INT             NOT NULL,
  ...
    CONSTRAINT [FK_Sales_ImportHistory] FOREIGN KEY ([ImportHistoryId]) REFERENCES [dbo].[ImportHistory] ([Id])
);

The question is how to properly take the ID of ImportHistory and store it each time I insert a line in SALES for this import session ?

Aditi Parikh
  • 1,522
  • 3
  • 13
  • 34
Wafou Z
  • 85
  • 10
  • I believe for every sale insert,you have 2 make to inserts, 1 in ImportHistiry and 1 in Sales. Once you insert in ImportHistory you acquire the id and use as ImportHistoryId in sales insert – apomene May 31 '16 at 10:11

1 Answers1

1
  1. You insert a row in ImportHistory.
  2. You SELECT SCOPE_IDENTITY() to get the ID of the newly created record.
  3. You insert your Sales records, using the value acquired in Step 2 as the ImportHistoryID.

PS: The timestamp data type is not what you think it is. You probably want to use date or datetime2 instead.

Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519