Check Foreign Key values

  • HI all,

    I have a scenario...

    For Example, I have a ITEM table with structure -

    CREATE TABLE ITEM

    (

    ItemID INT NOT NULL Primary Key,

    ItemName varchar(100)

    )

    I have another table Sales which contains ItemID of table Item

    Create table Sales

    (

    SalesID int not null Primary Key,

    ItemID Int refrences Item(ItemID),

    SalesDate datetime

    )

    During bulk data insertion, first I insert data in Item table then afterwards I insert data in Sales table,now while inseting data in sales table how can I check that itemID present in Sales table is present in Item table too...

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • If you have enforced referential integrity between the two tables, you won't have to check, SQL Server will do that for you. If that value does not exist, you will get an error.

    But the bigger question for me would be how do you get the ID values out of the first table in order to use them in the second. In that case, I usually look to the OUTPUT clause of the INSERT statement to get the new values in order to use them in the second set of inserts.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply