How to return all values ​​from a stored procedure?

Forgive my naivety, but I'm new to using Delphi with databases (which may seem strange to some).

I established a connection to my database (MSSQL) using TADOConnection. I am using TADOStoredProc to access my stored procedure.

My stored procedure returns 2 columns, a column with server names and a 2nd column full of users on the server. It usually returns around 70 records ... not much data.

How can I programmatically list this stored procedure? I can drop the DBGrid in my form and attach it to the TDataSource (which is then attached to my ADOStoredProc), and I can verify that the data is being retrieved correctly.

Ideally, I would like to list the returned data and move it to a TStringList.

I am currently using the following code to list ADOStoredProc, but it only returns "@RETURN_VALUE":

ADOStoredProc1.Open;
ADOStoredProc1.ExecProc;
ADOStoredProc1.Parameters.Refresh;

for i := 0 to AdoStoredProc1.Parameters.Count - 1 do
begin
  Memo1.Lines.Add(AdoStoredProc1.Parameters.Items[i].Name);
  Memo1.Lines.Add(AdoStoredProc1.Parameters.Items[i].Value);
end;
+3
source share
4 answers

Call Open to get the returned dataset.

StoredProc.Open;
while not StoredProc.EOF do
begin
  Memo1.Lines.Add(StoredProc.FieldByName('xyz').Value);
  StoredProc.Next;
end;
+8
source

Use Open to get records from StoredProc
Use the "Development Time", "Special Fields" fields captured with the FieldByName field before the loop, or the [nn] fields to get values.

procedure GetADOResults(AStoredProc: TADOStoredProc; AStrings: TStrings);
var
  fldServer, fldUser: TField;
begin
  AStoredProc.Open;
  fldServer := AStoredProc.FieldByName('ServerName');
  fldUser := AStoredProc.FieldByName('User');
  while not AStoredProc.EOF do
  begin
    AStrings.Add(Format('Server: %s - / User: %s',[fldServer.AsString, fldUser.AsString]));
    // or with FFields and Index (asumming ServerName is the 1st and User the 2nd) and no local vars
    AStrings.Add(Format('Server: %s - / User: %s',[AStoredProc.Fields[0].AsString, AStoredProc.Fields[1].AsString]));
    AStoredProc.Next;
  end;
end;


//use like
  GetADOResults(ADOStoredProc1, Memo1.Lines);

Note. The [nn] fields allow you to write less code, but be careful if StoredProc reorders the returned columns.

+3
source

( ), ExecProc. . Open Active, , :

ADOStoredProc.Open;

for i := 0 to ADOStoredProc1.Parameters.Count - 1 do
begin
  Memo1.Lines.Add(ADOStoredProc1.Parameters.Items[i].Name);
  Memo1.Lines.Add(ADOStoredProc1.Parameters.Items[i].Value);
end;

BTW, Open, ExecProc ; Open , ExecProc , . , . , 100%.

+2

( Googled it):

[http://www.scip.be/index.php?Page=ArticlesDelphi12&Lang=EN#Procedure][1]

, SQL Server , , , , select.

0

Source: https://habr.com/ru/post/1709412/


All Articles