locked
Insert OrderNo into Order Details Table RRS feed

  • Question

  • User-1520784531 posted

    I've 2 tables, namely Order and Order Details. 

    In my Order Table, I've a primary key name OrderNo with an Identity increment value. Wherelse my Order Details contain an OrderNo from the Order Table.

    How do I insert the OrderNo in the OrderDetails Table? 

    Tuesday, July 19, 2011 1:20 AM

Answers

  • User-654786183 posted

    Try this in your stored procedure

    DECLARE @INSERTETDID INT
    
    INSERT INTO [SearchEngine].[dbo].[searchItems]
    ([searchItem])
    VALUES
    (@searchItem)
    
    SELECT @INSERTETDID=SCOPE_IDENTITY()
    
    INSERT INTO [SearchEngine].[dbo].[urls]
    ([queryId])
    VALUES
    (@INSERTETDID)
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 20, 2011 9:37 PM

All replies

  • User1458583585 posted

    you can create a procedure.

    create procedure orderOpration
    (
    	@name nchar(10),
    	@fname varchar(100)
    )
    as 
    begin
    set 
    insert into ordermaster (name) values(@name)
    insert into orderdetails (fname,orderid)values(@fname,IDENTITY)
    end

    you can change variable for your requierment..
    Tuesday, July 19, 2011 1:52 AM
  • User3866881 posted

    In my Order Table, I've a primary key name OrderNo with an Identity increment value. Wherelse my Order Details contain an OrderNo from the Order Table.

    How do I insert the OrderNo in the OrderDetails Table? 

    Hello:)

    Another way is everytime when you use SqlCommand to insert a record into Order Table successfully, Just use "select top OrderNo from Order order by OrderNo desc" to get the latest single OrderId, and then use it as a value parameter to call OrderDetails with.

    Thx

    Wednesday, July 20, 2011 9:33 PM
  • User-654786183 posted

    Try this in your stored procedure

    DECLARE @INSERTETDID INT
    
    INSERT INTO [SearchEngine].[dbo].[searchItems]
    ([searchItem])
    VALUES
    (@searchItem)
    
    SELECT @INSERTETDID=SCOPE_IDENTITY()
    
    INSERT INTO [SearchEngine].[dbo].[urls]
    ([queryId])
    VALUES
    (@INSERTETDID)
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 20, 2011 9:37 PM