简体   繁体   English

从存储过程中从存储过程中的 SELECT 语句获取标量值

[英]Get scalar value from SELECT statement in stored proc, from within a stored proc

I know the preferred method for returning scalar values from stored procs is either using RETURN or an OUTPUT parameter.我知道从存储过程返回标量值的首选方法是使用RETURNOUTPUT参数。 But lets say that I have a stored proc that returns the value using a select statement:但是假设我有一个存储过程,它使用 select 语句返回值:

CREATE PROC spReturnNumber AS

SELECT 1

Is it possible to get this value from within another stored proc?是否可以从另一个存储过程中获取此值?

CREATE PROC spCheckNumber AS

EXEC spReturnNumber -- <-- get the return value here?

Clarification: I need a solution that doesn't require using an OUTPUT parameter, or using RETURN to return the value.澄清:我需要一个不需要使用OUTPUT参数或使用RETURN返回值的解决方案。

Thanks in advance.提前致谢。

You could use insert-exec to store the result of a stored procedure in a table:您可以使用insert-exec将存储过程的结果存储在表中:

declare @t table (col1 int)
insert @t exec spReturnNumber
return (select col1 from @t)

The definition of the table has to match the result set of the stored procedure.表的定义必须与存储过程的结果集相匹配。

Use an OUTPUT parameter instead of (or in addition to, if this procedure is used by other applications) the SELECT .使用OUTPUT参数代替 SELECT (或者,如果此过程被其他应用程序使用,则作为SELECT )。

ALTER PROCEDURE dbo.spReturnNumber
    @Number INT OUTPUT
AS
BEGIN
    SET NOCOUNT ON;

    SET @Number = 1;
    SELECT @Number;
END
GO

CREATE PROCEDURE dbo.spCheckNumber
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @Number INT;
    EXEC dbo.spReturnNumber @Number = @Number;
    SELECT @Number;
END
GO

If you can't change the original procedure, but you know its output will remain static, you could use a #temp table.如果您无法更改原始程序,但您知道其 output 将保持 static,则可以使用#temp 表。

CREATE PROCEDURE dbo.spCheckNumber
AS
BEGIN
    SET NOCOUNT ON;

    CREATE TABLE #n(i INT);
    INSERT #n(i) EXEC dbo.spReturnNumber;

    DECLARE @Number INT;
    SELECT @Number = i FROM #n;
END
GO

You can't get the SELECT value from "parent" procedure but you can get the return value like this:您无法从“父”过程中获得 SELECT 值,但您可以像这样获得返回值:

CREATE PROC A AS
BEGIN
    DECLARE @ret int

    EXEC @ret = spReturnNumber

    RETURN @ret
END

Per OP's request, the PostgreSQL way to do this.根据 OP 的要求,执行此操作的 PostgreSQL 方式。 The first function, foo() just returns an INT.第一个 function, foo()只是返回一个 INT。 The following functions, bar() and baz() call foo() , in straight SQL, as well as in PL/pgSQL, respectively.以下函数bar()baz()调用foo() ,分别在 SQL 以及 PL/pgSQL 中。

CREATE FUNCTION foo() RETURNS INT AS $$
    SELECT 1
$$ LANGUAGE sql;

CREATE FUNCTION bar() RETURNS INT AS $$
    SELECT foo()
$$ LANGUAGE sql;

CREATE FUNCTION baz() RETURNS INT AS $$
DECLARE
    x INT;
BEGIN
    SELECT INTO x foo();
    RETURN x;
END
$$ LANGUAGE plpgsql;

db=# SELECT foo();
foo
-----
1
(1 row)

db=# SELECT bar();
bar
-----
1
(1 row)

db=# select baz();
baz
-----
1
(1 row)

If you are unable to change the proc being called.. place the result set in a temp table [or table variable]:如果您无法更改被调用的 proc.. 将结果集放在临时表 [或表变量] 中:

CREATE TABLE #results (val INT)
   DECLARE @someval int
   INSERT #results
     EXEC dbo.spCheckNumber 

   SELECT @someval =val from  #results

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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