温馨提示:本文翻译自stackoverflow.com,查看原文请点击:sql server - Correct syntax for INSERT INTO SELECT query
coldfusion sql-server

sql server - INSERT INTO SELECT查询的正确语法

发布于 2020-04-23 15:11:34

我想将从一个表中删除的记录插入到另一个表中,以便可以将已删除的记录归档,就像Windows回收站一样。为此,我使用INSERT INTO SELECT查询:

<cfquery name="archiveDeletion" datasource="#application.dsn#">
    INSERT INTO deletedCylinderContentRecords (air_emissions, barcode, building, capacity, Carcinogen, carcinogen_comment, carcinogen_type, cas, casORmixture, chemicalname, concentration, containermaterial, containertype, cyanide, CylinderID, dotdivision, dothazardclass, dothazardclassSub, expirationdate, flashpoint, formula, grade, HMAR_ID, hydroflouric_acid, HydrostaticTestDate, HydrostaticTestDueDate, initial_quantity, inventoried, isdeleted, lastupdated, LeakTestDate, location, manufacturer, ModifiedBy, msds, MSDS_Scan, MSN, Mutagen, nfpablue, nfpared, nfpawater, nfpayellow, ownerid, PeroxideConcentration, PeroxideFormers, PeroxideTestDate, PeroxideTestDueDate, phaseID, PList, price, Purchase_Scan, Purchase_Scan_Link, purchasedate, remarks, ResearchSampleNo, room, sewer, shelf, ShockSensitive, specificgravity, spillage, spillclass, SPInspect, SPInspectDate, SPInterval, stocknumber, StorageRemarks, Teratogen, Undelete, units, usage_comments, usage_transferred, usage_transferred_to, usedinprocess, Validate, ValidateBy, ValidateDate, vendor, VendorNumber, waste, deleterName, timeDeleted, reasonDeleted)
    SELECT * FROM containers WHERE barcode = '#session.barcode#',
    <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#getDeleterName.last_name#, #getDeleterName.first_name# #getDeleterName.middle_initial#.">,
    SYSDATETIME(),
   <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#Form.deleteReason5#">
</cfquery>

从上面的查询中,您可以看到将三列从原始表插入到归档表中。这些都是最近三:deleterNametimeDeleted,和reasonDeleted因此,在该SELECT *部分之后,我添加了我想要的三列,以及一些<cfqueryparam>标签。我的语法一定是错误的,因为出现此错误:

','附近的语法不正确。

正确的语法是什么?我是否需要另一个SELECT关键字?我找不到任何东西。

查看更多

提问者
Max Voisard
被浏览
49
Max Voisard 2020-02-07 04:02

感谢@SeanLange在评论中所说的,我找到了解决方案。我将三个额外的列(不是containers表的一部分)添加SELECT查询中,并确保它可以正常工作。我还明确指定了列的名称,而不是使用*选择器,因此最终的解决方案如下所示:

<cfquery name="archiveDeletion" datasource="#application.dsn#">
    INSERT INTO deletedCylinderContentRecords (air_emissions, barcode, building, capacity, 
    Carcinogen, carcinogen_comment, carcinogen_type, cas, casORmixture, chemicalname, concentration,
    containermaterial, containertype, cyanide, CylinderID, dotdivision, dothazardclass,
    dothazardclassSub, expirationdate, flashpoint, formula, grade, HMAR_ID, hydroflouric_acid,
    HydrostaticTestDate, HydrostaticTestDueDate, initial_quantity, inventoried, isdeleted, lastupdated, 
    LeakTestDate, location, manufacturer, ModifiedBy, msds, MSDS_Scan, MSN, Mutagen, nfpablue, nfpared,
    nfpawater, nfpayellow, ownerid, PeroxideConcentration, PeroxideFormers, PeroxideTestDate,
    PeroxideTestDueDate, phaseID, PList, price, Purchase_Scan, Purchase_Scan_Link, purchasedate,
    remarks, ResearchSampleNo, room, sewer, shelf, ShockSensitive, specificgravity, spillage,
    spillclass, SPInspect, SPInspectDate, SPInterval, stocknumber, StorageRemarks, Teratogen,
    Undelete, units, usage_comments, usage_transferred, usage_transferred_to, usedinprocess, Validate, 
    ValidateBy, ValidateDate, vendor, VendorNumber, waste, deleterName, timeDeleted, reasonDeleted)
    SELECT air_emissions, barcode, building, capacity, Carcinogen, carcinogen_comment, carcinogen_type,
    cas, casORmixture, chemicalname, concentration, containermaterial, containertype, cyanide, 
    CylinderID, dotdivision, dothazardclass, dothazardclassSub, expirationdate, flashpoint, formula,
    grade, HMAR_ID, hydroflouric_acid, HydrostaticTestDate, HydrostaticTestDueDate, initial_quantity,
    inventoried, isdeleted, lastupdated, LeakTestDate, location, manufacturer, ModifiedBy, msds, 
    MSDS_Scan, MSN, Mutagen, nfpablue, nfpared, nfpawater, nfpayellow, ownerid, PeroxideConcentration,
    PeroxideFormers, PeroxideTestDate, PeroxideTestDueDate, phaseID, PList, price, Purchase_Scan,
    Purchase_Scan_Link, purchasedate, remarks, ResearchSampleNo, room, sewer, shelf, ShockSensitive,
    specificgravity, spillage, spillclass, SPInspect, SPInspectDate, SPInterval, stocknumber,
    StorageRemarks, Teratogen, Undelete, units, usage_comments, usage_transferred, usage_transferred_to, 
    usedinprocess, Validate, ValidateBy, ValidateDate, vendor, VendorNumber, waste,
    <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#getDeleterName.last_name#, #getDeleterName.first_name# #getDeleterName.middle_initial#.">,
    SYSDATETIME(),
    <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#Form.deleteReason5#">
    FROM containers WHERE barcode = '#session.barcode#'
</cfquery>