【问题标题】:Insert into Linked table in access without the primary key在没有主键的访问中插入链接表
【发布时间】:2017-02-09 07:00:00
【问题描述】:

我试图在 access 2007 中插入链接的 SQL 表,下面是我的查询

INSERT INTO tblProducts ( ProductPrefix, ProductCode, ProductDescription, MadeFrom, MadeFromDescription, SamFamilySort1, SamFamilySort2, SamFamilySort3, SamFamilySort4, SamFamilySort5, Grade, Length, Thickness, fWidth, Factor, CubicMtrs, CubicMtrsFull, [Weight(T)], DrawingFilepath, EFACSProductGrouping, BatchSize, PackSize, Density, createdby, createddate, ProductType, customer, DimA, DimB, DimC, DimD, DimE, DimF, DimG, DimH, DimI, DimJ, DimK, DimL, DimM, DimN, DimO, DimP, DimQ, DimR, DimS, DimT, DimU, DimV, DimW, DimX, DimY, DimZ, TolA, TolB, TolC, TolD, TolE, TolF, TolG, TolH, TolI, TolJ, TolK, TolL, TolM, TolN, TolO, TolP, TolQ, TolR, TolS, TolT, TolU, TolV, TolW, TolX, TolY, TolZ, Dimension, Main, Saws, Moulders, PaintLines, XCut, DET, Wrapper, Blocks, HingeRecess, reorderpolicy, machinedaway, UseOtherM3XC, UseOtherM3MS, ShrinkWrap, ShrinkWrapPackSize, SW, samtype1, vtype1, vtype2, profile, productchamp, UOM, SAMPartGrp, PostingClass, ProductID )
SELECT DISTINCT tblProducts.ProductPrefix, tblProducts.ProductCode, tblProducts.ProductDescription, tblProducts.MadeFrom, tblProducts.MadeFromDescription, tblProducts.SamFamilySort1, tblProducts.SamFamilySort2, tblProducts.SamFamilySort3, tblProducts.SamFamilySort4, tblProducts.SamFamilySort5, tblProducts.Grade, tblProducts.Length, tblProducts.Thickness, tblProducts.fWidth, tblProducts.Factor, tblProducts.CubicMtrs, tblProducts.CubicMtrsFull, tblProducts.[Weight(T)], tblProducts.DrawingFilepath, tblProducts.EFACSProductGrouping, tblProducts.BatchSize, tblProducts.PackSize, tblProducts.Density, tblProducts.createdby, Date() AS Expr1, tblProducts.ProductType, tblProducts.customer, tblProducts.DimA, tblProducts.DimB, tblProducts.DimC, tblProducts.DimD, tblProducts.DimE, tblProducts.DimF, tblProducts.DimG, tblProducts.DimH, tblProducts.DimI, tblProducts.DimJ, tblProducts.DimK, tblProducts.DimL, tblProducts.DimM, tblProducts.DimN, tblProducts.DimO, tblProducts.DimP, tblProducts.DimQ, tblProducts.DimR, tblProducts.DimS, tblProducts.DimT, tblProducts.DimU, tblProducts.DimV, tblProducts.DimW, tblProducts.DimX, tblProducts.DimY, tblProducts.DimZ, tblProducts.TolA, tblProducts.TolB, tblProducts.TolC, tblProducts.TolD, tblProducts.TolE, tblProducts.TolF, tblProducts.TolG, tblProducts.TolH, tblProducts.TolI, tblProducts.TolJ, tblProducts.TolK, tblProducts.TolL, tblProducts.TolM, tblProducts.TolN, tblProducts.TolO, tblProducts.TolP, tblProducts.TolQ, tblProducts.TolR, tblProducts.TolS, tblProducts.TolT, tblProducts.TolU, tblProducts.TolV, tblProducts.TolW, tblProducts.TolX, tblProducts.TolY, tblProducts.TolZ, tblProducts.Dimension, tblProducts.Main, tblProducts.Saws, tblProducts.Moulders, tblProducts.PaintLines, tblProducts.XCut, tblProducts.DET, tblProducts.Wrapper, tblProducts.Blocks, tblProducts.HingeRecess, tblProducts.reorderpolicy, tblProducts.machinedaway, tblProducts.useotherm3XC, tblProducts.useotherm3MS, tblProducts.ShrinkWrap, tblProducts.ShrinkWrapPackSize, tblProducts.SW, tblProducts.samtype1, tblProducts.vtype1, tblProducts.vtype2, tblProducts.profile, tblProducts.productchamp, tblProducts.UOM, tblProducts.SAMPartGrp, tblProducts.PostingClass, tblProducts.ProductID
FROM tblProducts

如果我愿意(我不这样做),这可以正常工作并使用新键上传表中的所有记录。我只想重新创建一种我尝试添加的产品

WHERE (((tblProducts.ProductID)=[tests]));

测试是用户输入的弹出框

下面出现错误

我在表中的主键称为 [ProductID]。是否可以以某种方式在此查询中添加 WHERE [ProductID] = 1234?

【问题讨论】:

  • 什么是测试?您几乎没有提供任何信息让任何人能够提供帮助。尽管我很想帮助你,但我没有足够的信息。
  • 抱歉测试是一个弹出框,用户将在其中输入变量。我不想插入 ProductID 作为它的自动编号,我只想插入用户使用 = ProductID 的弹出框指定的一行。如果这有意义的话。
  • 那么你当然会遇到主键违规。您正在将完全相同的行重新插入表中。您可能还想查看参数化查询,您的示例对 sql 注入开放。

标签: ms-access ms-access-2007


【解决方案1】:

请注意,INSERT INTO 子句的列列表中的最后一项是ProductID。因此,您尝试插入具有现有主键值的新行,但这是行不通的。作为一个简化的例子,

INSERT INTO tblProducts (ProductDescription, ProductID)
SELECT tblProducts.ProductDescription, tblProducts.ProductID
FROM tblProducts
WHERE tblProducts.ProductID=1

将因主键违规而失败。您需要从 INSERT INTO 和 SELECT 子句中删除 ProductID,并且只在 WHERE 子句中使用它:

INSERT INTO tblProducts (ProductDescription)
SELECT tblProducts.ProductDescription
FROM tblProducts
WHERE tblProducts.ProductID=1

【讨论】:

  • 啊,当通过 productId 添加过滤器时再次在字段中添加访问权限.. 我是个白痴。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-24
  • 1970-01-01
  • 2010-09-24
  • 1970-01-01
  • 2014-06-25
  • 2019-01-27
  • 1970-01-01
相关资源
最近更新 更多