runfastman
runfastman

Reputation: 947

Stored Procedure insert problems

I have a stored procedure I am calling to insert items in a table. If it only inserts an item into the table I get a 'Operation aborted' exception. If I add a select after the insert, it works fine.

What do I need to do different so I don't get the exception with only the insert?

Delphi code

procedure AddItem(dbCon : TADOConnection; sourcePath : String);
var
  addProc    : TADOStoredProc;


begin
  if FileExists(sourcePath) then
  begin
    try
      addProc := TADOStoredProc.Create(nil);
      addProc.Connection    := dbCon;
      addProc.ProcedureName := 'spTest';
      addProc.Open;

    finally
      addProc.Free();
    end;
  end;
end;

Stored Procedure

ALTER PROCEDURE [dbo].[spTest]
AS
BEGIN
  INSERT INTO dbo.ToSolve (Data, SolveStatus)
  VALUES (null, 1)

  --SELECT * from dbo.ToSolve --I must have a select or I get and exception
END

Upvotes: 2

Views: 796

Answers (1)

whosrdaddy
whosrdaddy

Reputation: 11860

As you found out, TADOStoredProc.Open is used in cases where there is a recordset returned. Use TADOStoredProc.ExecProc when no recordsets are returned. The same goes for TADOQuery, use ExecSQL for INSERT/UPDATE/DELETE statements and Open for SELECT statements. So your example should be like this:

procedure AddItem(dbCon : TADOConnection; sourcePath : String);

var
  addProc    : TADOStoredProc;

begin
  if FileExists(sourcePath) then
  begin
    addProc := TADOStoredProc.Create(nil);
    try
      addProc.Connection    := dbCon;
      addProc.ProcedureName := 'spTest';
      addProc.ExecProc;    
    finally
      addProc.Free;
    end;
  end;
end;

Upvotes: 3

Related Questions