简体   繁体   English

查询存储过程的结果

[英]Querying results of a stored procedure

I have a stored procedure that returns a large number of results, and would like a better way to debug/ parse the results than copy/pasting into excel or whatever - is there a way to pass the results of the procedure into a query? 我有一个返回大量结果的存储过程,并希望有一种更好的方法来调试/解析结果,而不是复制/粘贴到excel或其他任何东西 - 有没有办法将过程的结果传递给查询? eg, if the procedure call was something like: 例如,如果程序调用是这样的:

exec database..proc 'arg1','arg2','arg3'

my thought was to do something like: 我的想法是做一些像:

select distinct column1 from 
(exec database..proc 'arg1','arg2','arg3')

which clearly did not work, or I wouldn't be here. 这显然不起作用,或者我不会在这里。 If it matters, this is for a sybase database. 如果重要,那就是sybase数据库。

Thanks! 谢谢!

The code below works in MS SQL 2005. I don't have a Sybase installation right now to test it on that. 下面的代码适用于MS SQL 2005.我现在没有Sybase安装来测试它。 If it works in Sybase you could use a temporary table (or permanent table) outside of your stored procedure so that you don't have alter the code that you're trying to test (not a very good testing procedure generally.) 如果它在Sybase中有效,您可以在存储过程之外使用临时表(或永久表),这样就不会改变您尝试测试的代码(通常不是一个非常好的测试过程。)

CREATE TABLE dbo.Test_Proc_Results_To_Table
(
    my_id       INT         NOT NULL,
    my_string   VARCHAR(20) NOT NULL
)
GO

CREATE PROCEDURE dbo.Test_Proc_Results_To_Table_Proc
AS
BEGIN
    SELECT
        1 AS my_id,
        'one' AS my_string
END
GO

INSERT INTO dbo.Test_Proc_Results_To_Table (my_id, my_string)
EXEC dbo.Test_Proc_Results_To_Table_Proc
GO

SELECT * FROM dbo.Test_Proc_Results_To_Table
GO

In SQL Anywhere 10 and 11(didn't see whether it's ASA or ASE you're asking about): 在SQL Anywhere 10和11中(没有看到它是ASA还是ASE,你要问的):

SELECT DISTINCT Column1
FROM procName(parameter1, parameter2, parameter3);

I don't have ASE, and I'm not sure if this works on earlier ASA versions. 我没有ASE,我不确定这是否适用于早期的ASA版本。

you could create a temporary table (#temp) in the sp and populate the result set in there. 你可以在sp中创建一个临时表(#temp)并在那里填充结果集。 You can later select from the same temp table from the same session. 您可以稍后从同一会话中选择相同的临时表。 (Or use a global temp table in sybase with ##temp syntax) (或者在#base temp语法中使用sybase中的全局临时表)

This is because what you want to do (select * from exec sp) is not directly possible in sybase 这是因为你想要做的事情(select * from exec sp)在sybase中是不可能的

Is it possible to rewrite the stored proc as a function that returns a table? 是否可以将存储过程重写为返回表的函数? On SQL Server this is certainly possible. 在SQL Server上,这当然是可能的。 Then you can do... 然后你可以......

select
    <any columns you like>
from
    dbo.myFunc( 'foo', 'bar', 1 )
where
    <whatever clauses you like>
order by
    <same>

I'm not familiar with Sybase, but in MySQL you could use the IN parameter to write one SQL query for all this. 我不熟悉Sybase,但在MySQL中你可以使用IN参数为所有这些编写一个SQL查询。 Ex: 例如:

select distinct column1 from table where column1 in (your_first_query_with_all_the_arguments)

The only real way around this problem is to create a table in your database to store temporary values. 解决此问题的唯一方法是在数据库中创建一个表来存储临时值。

Lets say the stored procedures selects Column1, Column2 & Column3. 让我们说存储过程选择Column1,Column2和Column3。

Have a table (tempTable) with Column1, Column2, Column3 and set your stored procedure to the following: 有一个表(tempTable)与Column1,Column2,Column3并将您的存储过程设置为以下:

CREATE PROCEDURE database..proc
AS
BEGIN
  DELETE FROM tempTable

  INSERT INTO tempTable (Column1, Column2, Column3)
  SELECT Column1, Column2, Column3 
  FROM Table1 
END

then for your sql code to select the values have:

exec database..proc
SELECT Column1, Column2, Column3 
FROM tempTable

I hope this helps, I've come across similar problems before and this was the best I could work out. 我希望这有帮助,我之前遇到过类似的问题,这是我能解决的最好的问题。

I don't have Sybase installed right now, so some minor syntactic aspect may be wrong here - I can't check, but I used it extensively in the past: select * into #temp from proc_name('arg1','arg2','arg3') should create the local temp table for you automatically with the right columns. 我现在没有安装Sybase,所以这里有一些小的语法方面可能是错的 - 我无法检查,但我过去广泛使用它: proc_name 选择 * #temp('arg1','arg2' ,'arg3')应该使用正确的列自动为您创建本地临时表。 Within the same transaction or begin/end block you can access #temp by select * from #temp. 在同一个事务或开始/结束块中,您可以通过select * from #temp访问#temp。

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

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