简体   繁体   中英

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM