存储过程:
CREATE PROCEDURE liyan
@parm1 char(5),
@parm2 varchar(200)
 AS
   set @parm1='liyan'
   set @parm2='is a chinese'
   print @parm1
   print ('     '+@parm2)
GO
这个存储过程在查询分析器中执行的结果是:
liyan
     is a chinese
问题:怎么在delphi程序中调用这个存储过程,返回执行的结果,这里关键的地方是执行 结果在存储过程中用print的,请问怎么取到?我就是要在delphi中取到:
liyan
     is a chinese      就OK了,希望大家帮忙,分马上给,我在线等待,谢谢!!

解决方案 »

  1.   

    晕!修改为CREATE PROCEDURE liyan
    @parm1 char(5),
    @parm2 varchar(200)
     AS
       set @parm1='liyan'
       set @parm2='is a chinese'
       Select @parm1 + Chr(13) + '       '+@parm2 as Field1
    GOdelphi中用query ,设置sql 为  exec liyan '111','xxxx'  ,返回的记录集 是 field1 这个字段的内容;
      

  2.   

    print 出來的, 就我所知比較難得到!
    一般要麼用 output 參數, 要麼用 select 來返回
      

  3.   

    同意 要麼用 select 來返回
      

  4.   

    to  S.F.(CSDN:896410000)  :关键是我调用的存储过程中的print很多,不可能用一个参数来返回,如果像你的那种方法,都可以想得出来
    不好意思!!
    我就是要得到这个结果,并不是能用一个传出变量解决得了问题的!!谢谢!!
      

  5.   

    我调用的这个存储过程print后的显示结果大概有1000多行,不可能通过赋给一个变量来解决,请大家帮忙,刚刚回答的朋友还是万分感谢!!呵呵:)
      

  6.   

    为什么不能给一个变量?
    declare @tmpStr varchar(4000)这样不行?带回车的string 给TStrings 后会自动分行;或者你建立临时表,把每行信息写进去,最后select 它,drop 它;甚至你可以用动态sql ,或者是返回TABLE类型的数据。
      

  7.   

    关注先贴一点帮助。 SQL Server 有两种返回错误信息的机制: 。错误
    严重度为 11 或高于 11 的 sysmessages 中的错误。
    严重度为 11 或高于 11 的任意 RAISERROR 语句。 。消息
    PRINT 语句的输出。
    几个 DBCC 语句的输出。
    严重度为 10 或低于 10 的 sysmessages 中的错误。
    严重度为 10 或低于 10 的任意 RAISERROR 语句。 
      

  8.   

    toS.F.(CSDN:896410000) :现在的问题是这个存储过程是项目固定了的,我是不可能去改它的,我只能与后台合作,谢谢你的发言,你的那种想法我也想到过,但是我根本没有权力去改存储过程!
      

  9.   

    该消息作为 ADO,OLE DB 和 ODBC 应用程序中的消息性错误被返回。SQLSTATE 设为 01000,本机错误设为 0,而错误信息字符串被设为在 PRINT 语句中指定的字符串。字符串则返回给 DB-Library 应用程序的消息处理程序回调函数中。
    如何处理 ODBC 错误 (ODBC)
    可以使用两个 ODBC 函数调用来检索 ODBC 信息:SQLGetDiagRec 和 SQLGetDiagField。若要获得 SQLState、pfNative 和 ErrorMessage 诊断字段中与 ODBC 有关的主要信息,请调用 SQLGetDiagRec,直到它返回 SQL_NO_DATA 为止。可对每个诊断记录调用 SQLGetDiagField 以检索个别字段。所有驱动程序专用字段都必须用 SQLGetDiagField 检索。SQLGetDiagRec 和 SQLGetDiagField 由 ODBC 驱动程序管理器而不是个别的驱动程序来处理。ODBC 驱动程序管理器在连接成功建立之前,不高速缓存驱动程序专用诊断字段。在连接成功建立之前无法对驱动程序专用诊断字段调用 SQLGetDiagField。这还包括 ODBC 连接命令,即便它们返回 SQL_SUCCESS_WITH_INFO。驱动程序专用诊断字段在下一个 ODBC 函数调用之前不可用。
    Using PRINT and RAISERROR Statements
    Transact-SQL PRINT and RAISERROR statements also return data by calling SQLGetDiagRec. PRINT statements cause the SQL statement execution to return SQL_SUCCESS_WITH_INFO, and a subsequent call to SQLGetDiagRec returns a SQLState of 01000. A RAISERROR with a severity of ten or lower behaves the same as PRINT. A RAISERROR with a severity of 11 or higher causes the execute to return SQL_ERROR, and a subsequent call to SQLGetDiagRec returns SQLState 42000. For example, the following statement returns SQL_SUCCESS_WITH_INFO:SQLExecDirect (hstmt, "PRINT  'Some message' ", SQL_NTS);Calling SQLGetDiagRec returns:szSQLState = "01000", *pfNative Error = 0,
    szErrorMsg= "[Microsoft] [ODBC SQL Server Driver][SQL Server]
                    Some message"The following statement returns SQL_SUCCESS_WITH_INFO:SQLExecDirect (hstmt, "RAISERROR ('Sample error 1.', 10, -1)",
                SQL_NTS)Calling SQLGetDiagRec returns:szSQLState = "01000", *pfNative Error = 50000,
    szErrorMsg= "[Microsoft] [ODBC SQL Server Driver][SQL Server]
                    Sample error 1."The following statement returns SQL_ERROR:SQLExecDirect (hstmt, "RAISERROR ('Sample error 2.', 11, -1)", SQL_NTS)Calling SQLGetDiagRec returns:szSQLState = "42000", *pfNative Error = 50000,
    szErrorMsg= "[Microsoft] [ODBC SQL Server Driver][SQL Server]
                    Sample error 2."The timing of calling SQLGetDiagRec is critical when output from PRINT or RAISERROR statements is included in a result set. The call to SQLGetDiagRec to retrieve the PRINT or RAISERROR output must be made immediately after the statement that receives SQL_ERROR or SQL_SUCCESS_WITH_INFO. This is straightforward when only a single SQL statement is executed, as in the examples above. In these cases, the call to SQLExecDirect or SQLExecute returns SQL_ERROR or SQL_SUCCESS_WITH_INFO and SQLGetDiagRec can then be called. It is less straightforward when coding loops to handle the output of a batch of SQL statements or when executing SQL Server stored procedures.In this case, SQL Server returns a result set for every SELECT statement executed in a batch or stored procedure. If the batch or procedure contains PRINT or RAISERROR statements, the output for these is interleaved with the SELECT statement result sets. If the first statement in the batch or procedure is a PRINT or RAISERROR, the SQLExecute or SQLExecDirect returns SQL_SUCCESS_WITH_INFO or SQL_ERROR, and the application needs to call SQLGetDiagRec until it returns SQL_NO_DATA to retrieve the PRINT or RAISERROR information.If the PRINT or RAISERROR statement comes after an SQL statement (such as a SELECT statement), then the PRINT or RAISERROR information is returned when SQLMoreResults positions on the result set containing the error. SQLMoreResults returns SQL_SUCCESS_WITH_INFO or SQL_ERROR depending on the severity of the message. Messages are retrieved by calling SQLGetDiagRec until it returns SQL_NO_DATA.
      

  10.   

    http://www.cndevx.com/Article_Show.asp?ArticleID=1187 这里是odbc api 的参考。