-1

i have two queries.

1st gets data from different tables in database

result for 1st query looks some thing like this. query1 can return 1 row, 2 rows or 10 rows

acount_ID | product_ID | Color | QTY | Size | Prize | 
2         | 2          | blk   | 1   | md   | 100   |
2         | 1          | red   | 2   | md   | 50    |
2         | 9          | yllw  | 1   | sm   | 10    |
2         | 5          | wht   | 5   | lg   | 10    |


 SELECT 
    DISTINCT b.[Account_ID], bi.[Product_ID],bi.[QTY], bi.[Color],
    bi.[Size], p.[Price]  
 FROM [BAG_TB] b  
 LEFT JOIN [BAG_ITEM_TB] bi 
   on bi.[Bag_ID] = b.[Bag_ID]  
 LEFT JOIN [PRODUCT_TB] p 
   on p.[Product_ID] = bi.[Product_ID]  
 WHERE Account_ID = 2;

2nd query is inset query. in this query, I want to get all result from query1 and insert them in [order_Detail_TB].

INSERT INTO [ORDER_Detail_TB] 
(Product_ID, QTY, Color, Size, Product_Prize, Total_Prize) 
VALUES 
(@Product_ID, @QTY, @Color, @Size, @Product_Prize, @Total_Prize);
wildplasser
  • 43,142
  • 8
  • 66
  • 109
user1924249
  • 540
  • 3
  • 13
  • 38
  • It seems you're using SQL Server, if true, please tag it. As for the loop question, are you using a loop and you want to include the INSERT inside that loop to insert records based on a parameter value ? (query in your question doesn't need a loop, look for @halit answer it's enough). – iSR5 Feb 21 '18 at 04:52
  • Possible duplicate of [SQL Server SELECT into existing table](https://stackoverflow.com/questions/4101739/sql-server-select-into-existing-table) – halit Feb 21 '18 at 22:40

1 Answers1

2
INSERT INTO [ORDER_Detail_TB] (Product_ID, QTY, Color, Size,
Product_Prize, Total_Prize)
SELECT DISTINCT  bi.[Product_ID],bi.[QTY], bi.[Color],
 bi.[Size], p.[Price]  FROM [BAG_TB] b  LEFT JOIN [BAG_ITEM_TB] bi on
 bi.[Bag_ID] = b.[Bag_ID]  LEFT JOIN [PRODUCT_TB] p on p.[Product_ID] =
 bi.[Product_ID]  WHERE Account_ID = 2;

`

Update Dublicate: https://stackoverflow.com/a/4101761/971839

halit
  • 1,128
  • 1
  • 11
  • 27