0

我是一个使用 VBA/FK/SQL 服务器的新手。我正在 Excel 中创建简单的采购订单工作簿用户界面。

我创建了两个表:

CREATE TABLE [dbo].[PurchaseOrder](
[PKPurchaseOrderID] [bigint] IDENTITY(1,1) NOT NULL,
[PurchaseOrderNumber] [bigint] NULL,
[PurchaseOrderDate] [date] NULL,
[PurchaseOrderTime] [int] NULL,
[PurchaseOrderSupplierID] [nvarchar](50) NULL,
[ShipToA1] [nvarchar](50) NULL,
[ShipToA2] [nvarchar](50) NULL,
[ShipToA3] [nvarchar](50) NULL,
[ShipToA4] [nvarchar](50) NULL,
 CONSTRAINT [PK_PurchaseOrder] PRIMARY KEY CLUSTERED 
(
    [PKPurchaseOrderID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,     
ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[PurchaseOrderLines](
[PKPurchaseOrderLineID] [bigint] IDENTITY(1,1) NOT NULL,
[FKPurchaseOrderID] [bigint] NULL,
[Quantity] [smallint] NULL,
[Item] [nchar](25) NULL,
[Description] [nvarchar](50) NULL,
[siteID] [nchar](10) NULL,
[UnitPrice] [money] NULL,
[LineTotal] [money] NULL,

 CONSTRAINT [PK_PurchaseOrderLines] PRIMARY KEY CLUSTERED 

(

    [PKPurchaseOrderLineID] ASC

)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, 
ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

) ON [PRIMARY]



ALTER TABLE [dbo].[PurchaseOrderLines]  WITH CHECK ADD  CONSTRAINT 
[FK_PurchaseOrderLines_PurchaseOrder] FOREIGN KEY([FKPurchaseOrderID])

REFERENCES [dbo].[PurchaseOrder] ([PKPurchaseOrderID])


ALTER TABLE [dbo].[PurchaseOrderLines] CHECK CONSTRAINT 
[FK_PurchaseOrderLines_PurchaseOrder]

GO

当用户单击两个表都更新的按钮时,我想在后台实现这一点。

我不确定如何将 PurchaseOrder 表中创建的全新行与 PurchaseorderLine 表中的 FK 链接起来。

我打算为单用户界面做什么:

  1. 插入新订单
  2. 使用 Max(PKPurchaseOrderID) 作为新订单行表的 FK。

如果多个用户同时工作/提交订单,我如何确定当前插入的订单 ID(PKPurchaseOrderID)。如果我使用我的计划,恐怕订单行可能会分配给不同的订单。例如

请指教。

我使用 SQL Server 2008 和 Excel 2007/2010

非常感谢

4

1 回答 1

1

我将创建一个存储过程,插入新行并返回 IDSCOPE_IDENTITY()

这是一篇关于 SQL Server 中不同“身份”方法的不错的文章。

http://blog.sqlauthority.com/2007/03/25/sql-server-identity-vs-scope_identity-vs-ident_current-retrieve-last-inserted-identity-of-record/

于 2012-05-28T11:42:32.953 回答