How to loop statements in SQL Server

前端 未结 2 515
有刺的猬
有刺的猬 2021-01-26 02:32

I am new to SQL Server, I am trying to do something as follows.

Sample code :

SELECT ITEM_ID
FROM \'TABLE_NAME_1\'
WHERE ITEM_STATUS = \'ACTIVE\'

SET @I         


        
相关标签:
2条回答
  • 2021-01-26 03:14

    based on your question i have created sample code you can use only one query to insert multiple data if you want to insert common data between table 1 and table 2 then use inner join or left join will be fine.

    Code

    INSERT INTO 'TABLE_NAME_3' (ITEM_ID,PRICE)
    SELECT T1.ITEM_ID , T2.PRICE
    FROM 'TABLE_NAME_1' AS T1
    INNER JOIN 'TABLE_NAME_2' AS T2 ON T2.PRODUCT_ID = T1.ITEM_ID
    WHERE T1.ITEM_STATUS = 'ACTIVE' 
    
    0 讨论(0)
  • 2021-01-26 03:26

    Question would be why not use a straight SQL

    INSERT 
      INTO 'TABLE_NAME_3' 
           (ITEM_ID
            ,PRICE
           )
     SELECT ITEM_ID,ITEM_PRICE
       FROM 'TABLE_NAME_1' A
       JOIN 'TABLE_NAME_2' B
         ON A.ITEM_ID=B.PRODUCT_ID
      WHERE A.ITEM_STATUS = 'ACTIVE'               
    
    0 讨论(0)
提交回复
热议问题