我的MS Access 2007数据库具有以下架构:

主表Object< # Object_PK, ... >
子表Electric_Consumption< $ Object_PK, # Electric_Consumption_PK, ... >
子表Water_Consumption< $ Object_PK, # Water_Consumption_PK, ... >
子表Educational_Object< $ Object_PK, # Educational_Object_PK, ... >具有如下定义的子表:
School< $ Educational_Object_PK, # School_PK, ... >University< $ Educational_Object_PK, # University_PK, ... >
这是应该使情况更清楚的图片:

我使用ADOC++插入数据。

首先,我需要为主表Object输入数据。我可以使用INSERT查询成功地做到这一点。

我的问题如下:

完成上述操作后,我需要将Object主键插入子表,因为它是它们的外键。

请允许我准确描述我的需求,以便社区可以帮助我:

如我所说,首先我将数据插入到主表Object中。

然后,我需要将数据Object的主键插入子表中。

通过Internet浏览,我发现@@IDENTITY可能对我有帮助,但是我不知道它是否适合我的情况。

更麻烦的是,这将在for loop中完成(每个Object_PKINSERT的值都相同,并且等于Object的最后插入记录的的值),如下所示:

for ( //... )
   L"INSERT INTO Electric_Consumption ( Object_PK, field1, field2 ... )
       values ( Object_pk // should I use here @@IDENTITY ? );

然后应该对表Water_ConsumptionEducational_Object重复同样的事情。

完成此操作后,需要在Educational_Object的子表中添加数据。

与上述相同,只是我需要添加Object_PK而不是Educational_Object_PK

这是伪代码,可以更好地说明问题:
L"INSERT INTO Object ( ... ) values ( ... ); //this is OK

for ( ... )
    L" INSERT INTO Electric_Consumption ( Object_PK, ... )
        values ( Object_PK, ... )"; // should I use @@IDENTITY here
                                    // to get Object_PK ??
for ( ... )
    L" INSERT INTO Water_Consumption ( Object_PK, ... )
        values ( Object_PK, ... )"; // should I use @@IDENTITY here
                                    // to get Object_PK ??
for ( ... )
    L" INSERT INTO Educational_Object ( Object_PK, ... )
        values ( Object_PK, ... )"; //  should I use @@IDENTITY here
                                    // to get Object_PK ??
for ( ... )
    L" INSERT INTO School ( Educational_Object_PK, ... )
        values ( Educational_Object_PK, ... )";// should I use @@IDENTITY here
                                    // to get Educational_Object_PK ??
for ( ... )
    L" INSERT INTO University ( Educational_Object_PK, ... )
        values ( Educational_Object_PK, ... )";// should I use @@IDENTITY here
                                    // to get Educational_Object_PK ??

您能否告诉我为此使用哪个SQL statement,并通过提供一个小的伪代码来演示如何使用它?

我了解我对问题的描述可能会令人困惑,因此,如果您需要进一步澄清,请发表评论,我将编辑我的帖子。

谢谢。

最好的祝福。

最佳答案

是的,您想使用SELECT @@IDENTITY作为多用户安全的方法来检索最近创建的AutoNumber(有时称为“IDENTITY”)值。要记住的事情是:

  • 在父表上执行INSERT后,您将立即执行SELECT @@IDENTITY查询。
  • 您将返回的值存储在Long Integer变量中。
  • 您可以使用变量在子表中填充外键值。

  • 以下是VBA代码,但您可以将其视为伪代码:

    Dim lngObject_PK As Long, lngEducational_Object_PK As Long
    
    Set cmd = New ADODB.Command
    cmd.ActiveConnection = con
    cmd.CommandText = "INSERT INTO [Object] ([Description]) VALUES (?)"
    cmd.Parameters.Append cmd.CreateParameter("?", adVarWChar, adParamInput, 255, "my new Object")
    cmd.Execute
    Set cmd = Nothing
    
    Set rst = New ADODB.Recordset
    rst.Open "SELECT @@IDENTITY", con, adOpenStatic, adLockOptimistic
    lngObject_PK = rst(0).Value
    rst.Close
    Set rst = Nothing
    Debug.Print "Object_PK of newly-created Object record: " & lngObject_PK
    
    Set cmd = New ADODB.Command
    cmd.ActiveConnection = con
    cmd.CommandText = "INSERT INTO [Electric_Consumption] ([Object_PK],[Description]) VALUES (?,?)"
    cmd.Parameters.Append cmd.CreateParameter("?", adInteger, adParamInput, , lngObject_PK)
    cmd.Parameters.Append cmd.CreateParameter("?", adVarWChar, adParamInput, 255, "my new Electric_Consumption")
    cmd.Execute
    Set cmd = Nothing
    
    Set cmd = New ADODB.Command
    cmd.ActiveConnection = con
    cmd.CommandText = "INSERT INTO [Educational_Object] ([Object_PK],[Description]) VALUES (?,?)"
    cmd.Parameters.Append cmd.CreateParameter("?", adInteger, adParamInput, , lngObject_PK)
    cmd.Parameters.Append cmd.CreateParameter("?", adVarWChar, adParamInput, 255, "my new Educational_Object")
    cmd.Execute
    Set cmd = Nothing
    
    Set rst = New ADODB.Recordset
    rst.Open "SELECT @@IDENTITY", con, adOpenStatic, adLockOptimistic
    lngEducational_Object_PK = rst(0).Value
    rst.Close
    Set rst = Nothing
    Debug.Print "Educational_Object_PK of newly-created Educational_Object record: " & lngEducational_Object_PK
    
    Set cmd = New ADODB.Command
    cmd.ActiveConnection = con
    cmd.CommandText = "INSERT INTO [School] ([Educational_Object_PK],[Description]) VALUES (?,?)"
    cmd.Parameters.Append cmd.CreateParameter("?", adInteger, adParamInput, , lngEducational_Object_PK)
    cmd.Parameters.Append cmd.CreateParameter("?", adVarWChar, adParamInput, 255, "my new School")
    cmd.Execute
    Set cmd = Nothing
    

    关于c++ - 多次将主表的主键插入子表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21618823/

    10-11 01:10