Question: i got two table in SQL, one is user table and another is status table,i want to get the latest status to display using LINQ.

```
 /// User Table//
userID (Pk) userName telephonenumber 
 1             xx1       xxxxxxxx
 2             xx2       xxxxxxxx2
 3             xx3       xxxxxxxx3
 4             xx4       xxxxxxxx4

statusID(pk)  userID   statusMessage       createdAt    
   1           2        ddddddddx      2012-09-18 13:30:08
   2           4        zzzzddddd      2012-09-18 13:30:10 
   3           1        ssswessss      2012-09-18 16:30:10 
   4           2        ddddddddd      2012-09-18 16:30:15
   5           4        xxxxxweew      2012-09-18 17:30:18

```

Answer: 
In sql, you can get the inserted row details from `INSERTED` tables.

```
CREATE PROCEDURE InsertNewSkuWithInventory
        @description varchar (40),
        @department varchar(30),
        @sellingPrice numeric (7,2)
    AS

    BEGIN

        DECLARE @Output TABLE (SKU INT) 

        INSERT INTO  dbo.SKU_DATA ([DESCRIPTION], DEPARTMENT, SELLINGPRICE)
            OUTPUT INSERTED.SKU INTO @Output(SKU)--getting Inserted ID from SKU_DATA to @Output table
        VALUES (@description, @department, @sellingPrice);

        INSERT INTO INVENTORY (SKU ,branch ,quantityOnHand ,quantityOnOrder ) 
                 --Loading SKU information against all branches to INVENTORY
        SELECT t.SKU,b.name,2, 0
        FROM Branch b
        CROSS APPLY (SELECT SKU FROM @Output)t
    END

```

If you wanted to write a trigger , remove the inventory insert part from the above procedure and write a trigger like below.
For every insert in your SKU\_Data table this trigger will get fired and will insert data to the Inventory table.

Procedure :

```
  CREATE PROCEDURE InsertNewSkuWithInventory
        @description varchar (40),
        @department varchar(30),
        @sellingPrice numeric (7,2)
    AS

    BEGIN 

        INSERT INTO  dbo.SKU_DATA ([DESCRIPTION], DEPARTMENT, SELLINGPRICE)
        VALUES (@description, @department, @sellingPrice);
END

```

Trigger:

```
  CREATE TRIGGER UpdateInventory
   ON dbo.SKU_DATA
   AFTER INSERT
  AS

  INSERT INTO INVENTORY (SKU ,branch ,quantityOnHand ,quantityOnOrder ) 
         --Loading SKU information against all branches to INVENTORY
 SELECT t.SKU,b.name,2, 0
 FROM Branch b
 CROSS APPLY (SELECT SKU FROM INSERTED)t

```
You don't need a cursor. Just insert all branches to #BranchList and rest is as following.

```
Create procedure InsertNewSkuWithInventory
        @description varchar (40),
        @department varchar(30),
        @sellingPrice numeric (7,2),
    AS
        Declare @SKU as int

        CREATE TABLE #BranchList
        (
            branch varchar(30)
        )

    BEGIN
        INSERT INTO  dbo.SKU_DATA (description, department, sellingPrice)
        VALUES (@description, @department, @sellingPrice);

        Select @SKU =SKU
        From dbo.SKU_DATA
        Where description = @description 
        And department = @department 
        And sellingPrice = @sellingPrice;

        INSERT INTO #BranchList
        -- Your code to fetch all branches

        INSERT INTO INVENTORY
        SELECT @SKU, branch, 2, 0
        FROM #BranchList
    END

```