如何将记录从一个表插入另一个表?

时间:2013-10-22 09:20:08

标签: sql-server

我有2个具有相同列的表,例如ID,SupplierID,ConditionValue,Status,Deleted,我在代码中插入数据

INSERT INTO SCM_SupplierShippingRateHistory  
SELECT *           
FROM SCM_SupplierShippingRate          
WHERE  Id NOT IN(SELECT ID FROM dbo.GetIDsTableFromIDsList(@NonDeleteShippingIDs))        
AND SupplierId= @SupplierID        
AND ConditionValue IS NULL    
AND Deleted=0     

我想插入相同的数据,但我想设置status = 4我该怎么做

2 个答案:

答案 0 :(得分:1)

明确选择列。

INSERT INTO SCM_SupplierShippingRateHistory 
(
    Col1,
    Col2,
    .
    .
    .
    Status
) 
SELECT  Col1,
        Col2,
        .
        .
        .
        4 AS [Status]

FROM    SCM_SupplierShippingRate          
WHERE   Id NOT IN
        (
            SELECT ID 
            FROM   dbo.GetIDsTableFromIDsList(@NonDeleteShippingIDs)
        )        
        AND SupplierId= @SupplierID        
        AND ConditionValue IS NULL    
        AND Deleted=0     

答案 1 :(得分:0)

您可以设置这样的状态。

INSERT INTO SCM_SupplierShippingRateHistory  
SELECT ID,SupplierID,ConditionValue,4 AS Status,Deleted           
FROM SCM_SupplierShippingRate          
WHERE  Id NOT IN(SELECT ID FROM dbo.GetIDsTableFromIDsList(@NonDeleteShippingIDs))        
AND SupplierId= @SupplierID        
AND ConditionValue IS NULL    
AND Deleted=0