Groups | Blog | Home
all groups > sql server odbc > january 2006 >

sql server odbc : key violation, general sql error, connectin busy with another hstmt


Doug Stephens
1/12/2006 12:37:00 PM
We are getting this error periodically in a large app we are converting
from Access to SQL Server 2000. It uses BDE and ODBC for data access
and TTable/TQuery as well as TwwTable/TwwQuery components (from
woll2woll) under Delphi 6. It appears to happen when we are executing
TQuery.Open.

From what I've read, this is caused by ODBC not completing a result set
before processing another request. Which might be caused by having
Doug Stephens
1/13/2006 9:08:29 AM
So you can never have 2 open queries? I do that all the time, in one
thread. Maybe I'm not understanding. For example, this code works
fine, which creates 101 open queries:
----------------
procedure TForm1.ManyQueriesClick(Sender: TObject);
var qs : array[0..100] of TQuery ;
var q : Tquery;
var i : Integer;
begin
for i := 0 to 100 do begin
qs[i] := TQuery.Create(self);
q := qs[i];
q.databasename := 'WM';
q.SQL.Clear;
q.SQL.Add('SELECT * FROM CUSTOMERFIELDS');
statusbar1.SimpleText := 'Query ' + IntToStr(i);
q.active := true;
end;
for i := 0 to 100 do
qs[i].Free;
Doug Stephens
1/13/2006 11:06:14 AM
[quoted text, click to view]

Arnie
1/13/2006 11:21:10 AM
[quoted text, click to view]

Basically, you can't have two open (SELECT) statements on the
same connection. For example,

Open query 1
Get some info from a record
Use that to set params in query 2
Open query 2 -- This will fail

This assumes both statements are attached to the same hDBC. It
is a result of using a client-side cursor for the hStmt's.
You'll have to use server-side cursors for the statements to make
this work. See MSDN.

This is one way to force a server-side cursor:

SQLSetStmtAttr( m_hStmt, SQL_ATTR_CURSOR_SCROLLABLE,
(SQLPOINTER) SQL_SCROLLABLE, 0 );


Good luck,
- Arnie


Arnie
1/14/2006 12:52:51 PM
[quoted text, click to view]

You can't. It's an ODBC statement. The equivalent in Delphi
would be CursorLocation := clUseServer for TADO components.
Sorry, but I've totally forgotten about the BDE. It may use
server-side cursors by default.

The problem occurs in ODBC when nesting queries that use the same
connection handle. Open q1, read some field values, use these to
set params in q2 and then open q2. This last open will cause the
error. In theory, SQL Server 2005 has 'fixed' this 'feature',
though I haven't tried it yet.

- Arnie


Doug Stephens
1/16/2006 5:59:10 AM
I found a way to call this function in ODBC32.DLL from Delphi but what
is the hstmt?
Doug Stephens
1/16/2006 6:00:04 AM
Doug Stephens
1/16/2006 6:47:00 AM
Arnie
1/16/2006 9:30:07 AM
[quoted text, click to view]

What DB objects are you using with 2005?

- Arnie

Arnie
1/16/2006 9:31:21 AM
[quoted text, click to view]

As far as I know, you'd have to be using ODBC directly rather
than the BDE. hStmt is the ODBC statement handle.

- Arnie

AddThis Social Bookmark Button