在Delphi中如何获得SQL中存储过程的返回值?

示例存储过程:
create procedure proc_login
username varchar(20),
password varchar(20)
as
declare @result int
select @result=count(*) from loginuser where [email protected] and [email protected]
if @result=0
return 0
return 1
go

Delphi代码:
var ret:integer;
......
      with ADOStoredProc1 do
        begin
          Close;
          ProcedureName:=‘proc_login‘;
          Parameters.Clear;
          Parameters.Refresh;
          Parameters.ParamByName(‘@username‘).Value:= Edit1.text;
          Parameters.ParamByName(‘@password‘).Value:= Edit2.text;
          ExecProc;
          ret:= Parameters.ParamByName(‘@return_value‘).Value;
        end;

if ret=1 //用户名密码匹配
     begin
       //你想要的操作
      end

示例二

在delphi中取存储过程的返回值
   Close;
    SQL.Clear;
    SQL.Text:=‘declare @ReturnCount int exec Pr_SelStockHead ‘‘‘+StockNo+‘‘‘,@ReturnCount output select @ReturnCount‘;
    open;
    CountNO:=fields[0].value;
    cxtxtdtNameCSHYH.Text:=IntToStr(CountNO);
在sql语句里面 如果有返回值的话,可以是用return 返回,,其他的都是参数返回,如果取参数的话 从 Parameters[1].Value 开始,如果取return 的返回值 要用Parameters[0].Value

调用存储过程的方法,用adodataset控件
function TfrmPower_Cut.HasNewPowerCutInfo: Boolean;
begin
Result := False;
with spPower_Cut do //spPower_Cut为Tadostoredproc控件
begin
    Close;
    ProcedureName := ‘p_Has_PowerCut_Info‘;
    with Parameters do
    begin
      Clear;
      Refresh;
      ParamByName(‘@BiggestID‘).Direction := pdInputOutput;
      ParamByName(‘@BiggestID‘).Value := FPower_Cut_ID_Refresh;
      ParamByName(‘@NoProcess‘).Direction := pdInputOutput;
      ParamByName(‘@NoProcess‘).Value := FNoProcess;
      ParamByName(‘@IsPassTimeAndNoProc‘).Direction := pdInputOutput;
      ParamByName(‘@IsPassTimeAndNoProc‘).Value := FIsPassTimeAndNoProc;
      ParamByName(‘@IsNearestTime‘).Direction := pdInputOutput;
      ParamByName(‘@IsNearestTime‘).Value := FIsNearestTime;
      ParamByName(‘@IsDelete‘).Direction := pdInputOutput;
      ParamByName(‘@IsDelete‘).Value := FIsNearestTime;
      ParamByName(‘@Hour‘).Value := 3;
    end;
    Prepared;
    try
      ExecProc;
      if Parameters[0].Value <> FPower_Cut_ID_Refresh then
      begin
        FPower_Cut_ID_Refresh := Parameters[1].Value;
        Result := True;
      end;
      if Parameters[2].Value <> FNoProcess then
      begin
        FNoProcess := Parameters[2].Value;
        Result := True;
      end;
      if Parameters[3].Value <> FIsPassTimeAndNoProc then
      begin
        FIsPassTimeAndNoProc := Parameters[3].Value;
        Result := True;
      end;
      if Parameters[4].Value <> FIsNearestTime then
      begin
        FIsNearestTime := Parameters[4].Value;
        Result := True;
      end;
      if Parameters[5].Value <> FIsDelete then
      begin
        FIsDelete := Parameters[5].Value;
        Result := True;
      end;
    except
      on e: Exception do
        ShowMessage(e.Message);
    end;
end;
end;
存储过程
/*
功能: 判断数据库内是否有新的呼叫中心停电信息
参数: @BiggestID 客户端最大的记录ID,如果小于当前表中的ID,则返回最大的ID,客户端据此判断是否刷新
返回值: 无
*/
ALTER procedure p_Has_PowerCut_Info @BiggestID bigint=0 output, @NoProcess int=0 output, 
@IsPassTimeAndNoProc int=0 output, @IsNearestTime int=0 output, @IsDelete int=0 output, @Hour int=3 as
begin
declare @tmp_ID bigint,@tmp_NoProcess int
select @tmp_ID=Power_Cut_ID from Power_Cut 
if (@@error=0) and (@@rowcount>0)
    if @tmp_id>@BiggestID 
      set @BiggestID[email protected]_ID
select @tmp_NoProcess=count(*) from Power_Cut where PC_ProcType=0
if (@@error=0) and (@@rowcount>0)
    set @[email protected]_NoProcess
--超过发送时间未处理
select @IsPassTimeAndNoProc=count(case when (getdate()>PC_StartTime and PC_ProcType=0) then 1 end) from Power_Cut 
--距离发送时间还有3小时未处理
select @IsNearestTime=count(case when (DATEDIFF(minute, getdate(), PC_StartTime)>=0 and DATEDIFF(minute, getdate(), PC_StartTime)<@Hour*60 
    and PC_ProcType=0) then 1 end) from Power_Cut
select @IsDelete=count(*) from Power_Cut where PC_State=2
return @BiggestID
end
return 返回的是正确或错误的标志,比如 100: 成功 0: 失败(未知原因) 1: 参数错误 2: .... 然后参数里面返回具体需要的数据

原文地址:https://www.cnblogs.com/jijm123/p/10128874.html

时间: 2025-01-04 20:33:39

在Delphi中如何获得SQL中存储过程的返回值?的相关文章

SqlServer和MySQL中存储过程out返回值处理C#代码

1.SqlServer中out处理 C#代码 #region"SqlServer中存储过程处理out返回值" //public void getdata() //{ // string str = " server=192.168.xxxx ;user id=xxx;[email protected]#;database=xxxxx_db;min pool size=4;max pool size=4;packet size=3072"; // SqlConnect

webMethods-Developer/Designer中try-catch与SQL中事务的实现

TryCatch的结构为三个Sequence: -Main(exit on:Success) +try(exit on:Failure) +catch (exit on:Done) *以上做法是以前webMethods对于数据库操作的实现,现在有了JDBC Adapter之后,数据库操作变得简单灵活,因而取代了这种做法.如果是有其他特殊目的的这种方法依然适用. 在JDBC Adapter建立的过程中会提供三种Transaction的选择: NO_TRANSACTION:没有使用事务 LOCAL_

Entity Framework中对存储过程的返回值的处理

很早就开始注意到EF了,但一直没有机会用,换了工作后,第一个项目就使用EF6进行开发. 项目不是很大,EF完全可以胜任. 但是开发过程中,难免还是会遇到一些复杂的运算,需要频繁访问数据库. 此时,想到的比较简单的方式,就是使用存储过程,在存储过程中进行一定的运算,然后把运算的结果(一个查询结果)通过存储过程返回. 思路上完全没有问题,就直接建了一个存储过程,然后更新edmx.发现,更新后,EF中,生成的对这个存储过程的调用,返回结果却是“Int”??? 之前也有做过通过存储过程返回值,都是返回一

012-关于EditText中的getText()方法的返回值类型以及string的转换问题(转)

原文链接:https://blog.csdn.net/mattdong0106/article/details/10139389 EditText中的getText()方法的返回值为CharSequence,如果我们想要获得string类型数据的话,需要在后边加上.toString 另外,String类型转为int:Integer.parseInt(str); String类型转为float:Float.parseFloat(str); string和Date的相互转换: DateFormat

string 中的 length函数 和size函数 返回值问题

string 中的 length函数 和 size函数 的返回值  (  还有 char [ ] 中 测量字符串的  strlen 函数 ) 应该是 unsigned int 类型的 不可以 和 -1 比较. 应尽量避免 unsigned int 类型 和 int类型 数据 的比较 .当unsigned int 类型 和 int类型 数据 比较 时 ,会 把int 类型 转换 为 unsigned int类型 .如果 int是负数 ,转换 为 unsigned int 会是 一个 很大 的正整数

Golang中interface{}作为函数参数和函数返回值的使用

package main import (     "errors"     "fmt" ) type item struct {     Name string } func (i item) String() string {     return fmt.Sprintf("item name: %v", i.Name) } type person struct {     Name string     Sex  string } func

C#中的函数(二) 有参有返回值的函数

接上一篇 C#中的函数(-) 无参无返回值的函数 http://www.cnblogs.com/fzxiaoyi/p/8502613.html 这次研究下C#中的函数(二) 有参有返回值的函数 依然写一个小例子,用来测试 跟上一个例子差不多,区别就是MyFunction有二个参数a,b,返回二个数相加的值 F5调试运行,中断后转到反汇编 这里很明显看到不同了 这里就得讲到参数传递的方式,参数从左向右依次存入寄存器ecx edx 但是不同的编程语言有不同的传递参数的方式,有空再写一篇文章介绍下 要

将存储过程的返回值赋给变量

1.OUPUT参数返回值 复制代码代码如下: CREATE PROCEDURE [dbo].[nb_order_insert](@o_buyerid int ,@o_id bigint OUTPUT)ASBEGINSET NOCOUNT ON;BEGININSERT INTO [Order](o_buyerid )VALUES (@o_buyerid )SET @o_id = @@IDENTITYENDEND 存储过程中获得方法: 复制代码代码如下: DECLARE @o_buyerid int

利用SQLServer查询分析器获取存储过程的返回值,检查测试存储过程

1.存储过程没有返回值的情况(即存储过程语句中没有return之类的语句)用方法 int count = ExecuteNonQuery(..)执行存储过程其返回值只有两种情况(1)如果通过查询分析器执行该存储过程,在显示栏中如果有影响的行数,则影响几行count就是几(2)如果通过查询分析器执行该存储过程,在显示栏中如果显示'命令已成功完成.'则count = -1;在显示栏中如果有查询结果,则count = -1总结:A.ExecuteNonQuery()该方法只返回影响的行数,如果没有影响