Friday, February 11, 2011

How do I work with Multiple Recordsets in C++ ODBC

I am trying to streamline a complex process of storing information in multiple tables and them linking them to a central table. The linking occurs using IDENTITY values generated in each table to provide the unique linking. I know I can use a combination of SET NOCOUNT ON and SELECT @@identity to get each identity, but that still requires me to call a separate SQLExecute() for each table. I have read dozens of articles saying ADO can handle multiple recordsets using an ODBC driver, so the question is how do I do it without ADO?

I have already encapsulated all of the standard ODBC stuff for similar behavior to ADO. I basically just need to know what ODBC API calls will allow me to recreate ADO's NextRecorset().

I am working on a combination of MS SQL 7 and MS SQL 2005, using either the SQL Server ODBC, or SQL Native Client Drivers as appropriate.

End Goal:

SET NOCOUNT ON;
INSERT INTO TableA (data) VALUES ('a');
SELECT @@identity AS  NewID;
INSERT INTO TableB (data) VALUES ('b');
SELECT @@identity AS  NewID;
INSERT INTO TableC (data) VALUES ('c');
SELECT @@identity AS  NewID;
...
RS = DB.OpenRecordset()
RS.MoveFirst()
A_ID = RS.GetValue("id")
RS.NextRecordset()
RS.MoveFirst()
B_ID = RS.GetValue("id")
RS.NextRecordset()
RS.MoveFirst()
C_ID = RS.GetValue("id")

Thanks in advance!

  • Use the SQLMoreResults() call as the analog to the NextRecordSet() function.

    However you probably don't need that if you are willing to make your executes consist of "INSERT ...; SELECT @@IDENTITY" Since the only result returned from this statement is the identity, you don't need to bother with the SQLMoreResults().

    From Bill
  • Thanks! This is exactly what I was looking for.

    From

0 comments:

Post a Comment