Suppose I generate the PK for my SQL Server DB table with the help of newid() function. In Java I can do something like this:
...
String query = "DECLARE @newGuid uniqueidentifier "+
"SET @newGuid = newid() "+
"INSERT INTO myTable(id, stringval) "+
"VALUES (@newGuid, "Hello") "+
"SELECT uid FROM @newGuid";
PreparedStatement ps = conn.prepareStatement(query);
ResultSet rs = ps.executeQuery();
String uid = rs.getString("uid");
But when I try to make that with Delphi+ADO I get stuck cause ADO can either get data from DB (Open method of AdoQuery) or put data to DB (ExecSQL method). So I can’t insert new value to the table and get the parameter value afterwards.
You could solve this problem atleast in two ways.
You can put both of your SQL queries into one string (just like you have in your example) and call
TADOQuery.OpenorTADOQuery.Active := True. it doesn’t matter that you haveINSERTstatement there as long as query returns something.You can define parameter’s direction as pdOutput in
ADOQuery.Parameterscollection and read value of that parameter after executing the query.