插入后获取文本框中的主键值

时间:2011-03-23 08:28:49

标签: sql-server vb6

我有2个表, TableA TableB IMAT_PRIORITY_ID是TableA中的主键。

如何在插入TableA后在文本框中获取 IMAT_PRIORITY_ID 的值?

strSQL1 = "INSERT TableA (IMAT_PRIORITY_ID,JOB_NO,BATCH_NO) VALUES (SQ_PRIOTITY_ID.nextval," & JOB_NO.Text & "','" & BATCH_NO.Text & "')"

With adoCommand
    .ActiveConnection = adoconn
    .CommandType = adCmdText
    .CommandText = strSQL1
    .Prepared = True
    .Execute , , adCmdText + adExecuteNoRecords
End With


strSQL1 = "INSERT TableB (ISBN_SERIAL_NO,IMAT_PRIORITY_ID,ISBN) VALUES (ISBN_SERIAL_NO.NEXTVAL,'" & IMAT_PRIORITY_ID.Text & "','" & ISBN.Text & "')"


With adoCommand
    .ActiveConnection = adoconn
    .CommandType = adCmdText
    .CommandText = strSQL1
    .Prepared = True
    .Execute , , adCmdText + adExecuteNoRecords
End With

1 个答案:

答案 0 :(得分:0)

要获取从insert语句生成的最后一个标识(PRIMARY KEY),您可以按如下方式查询它。

strSQL1 =“INSERT TableA(IMAT_PRIORITY_ID,JOB_NO,BATCH_NO)VALUES(SQ_PRIOTITY_ID,JOB_NO,BATCH_NO)VALUES(SQ_PRIOTITY_ID.nextval,”& JOB_NO.Text&“','”& BATCH_NO.Text&“ '); SELECT SCOPE_IDENTITY()

您可以在here

上找到更多详情