如何查询过程中存储过程返回的表

时间:2010-06-09 11:31:13

标签: sql sql-server database tsql stored-procedures

我有一个正在执行某些ddl dml操作的存储过程。 它在处理来自CTE的数据并交叉应用和其他此类复杂事物后检索数据。

现在这会给我一个4个表格,它们会在前端绑定到各种来源。 现在我想使用其中一个表进一步处理,以便从中获取更多有用的信息。

例如。该表将包含大约2000条记录,其中我想要获得仅属于住宿的记录。

PK_CATEGORY_ID       DESCRIPTION                                        FK_CATEGORY_ID       IMMEDIATE_PARENT                                   Department_ID        Department_Name                                    DESCRIPTION_HIERARCHY                                                                                                                                                                                                                                            DEPTH       IS_ACTIVE   ID_PATH                                                                                                                                                                                                                                                          DESC_PATH
-------------------- -------------------------------------------------- -------------------- -------------------------------------------------- -------------------- -------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------- ----------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1                    Food                                               NULL                 NULL                                               1                    Food                                               (Food) Food                                                                                                                                                                                                                                                      0           1           0                                                                                                                                                                                                                                                                Food
5                    Chinese                                            1                    Food                                               1                    Food                                               (Food) ----Chinese                                                                                                                                                                                                                                               1           1           1                                                                                                                                                                                                                                                                Food->Chinese
14                   X                                                  5                    Chinese                                            1                    Food                                               (Food) --------X                                                                                                                                                                                                                                                 2           1           1->5                                                                                                                                                                                                                                                             Food->Chinese->X
15                   Y                                                  5                    Chinese                                            1                    Food                                               (Food) --------Y                                                                                                                                                                                                                                                 2           1           1->5                                                                                                                                                                                                                                                             Food->Chinese->Y
65                   asdasd                                             5                    Chinese                                            1                    Food                                               (Food) --------asdasd                                                                                                                                                                                                                                            2           1           1->5                                                                                                                                                                                                                                                             Food->Chinese->asdasd
66                   asdas                                              5                    Chinese                                            1                    Food                                               (Food) --------asdas                                                                                                                                                                                                                                             2           1           1->5                                                                                                                                                                                                                                                             Food->Chinese->asdas
8                    Italian                                            1                    Food                                               1                    Food                                               (Food) ----Italian                                                                                                                                                                                                                                               1           1           1                                                                                                                                                                                                                                                                Food->Italian
48                   hfghfgh                                            1                    Food                                               1                    Food                                               (Food) ----hfghfgh                                                                                                                                                                                                                                               1           1           1                                                                                                                                                                                                                                                                Food->hfghfgh
55                   Asd                                                1                    Food                                               1                    Food                                               (Food) ----Asd                                                                                                                                                                                                                                                   1           1           1                                                                                                                                                                                                                                                                Food->Asd
2                    Lodging                                            NULL                 NULL                                               2                    Lodging                                            (Lodging) Lodging                                                                                                                                                                                                                                                0           1           0                                                                                                                                                                                                                                                                Lodging
3                    Room                                               2                    Lodging                                            2                    Lodging                                            (Lodging) ----Room                                                                                                                                                                                                                                               1           1           2                                                                                                                                                                                                                                                                Lodging->Room
4                    Floor                                              3                    Room                                               2                    Lodging                                            (Lodging) --------Floor                                                                                                                                                                                                                                          2           1           2->3                                                                                                                                                                                                                                                             Lodging->Room->Floor
9                    First                                              4                    Floor                                              2                    Lodging                                            (Lodging) ------------First                                                                                                                                                                                                                                      3           1           2->3->4                                                                                                                                                                                                                                                          Lodging->Room->Floor->First
10                   Second                                             4                    Floor                                              2                    Lodging                                            (Lodging) ------------Second                                                                                                                                                                                                                                     3           1           2->3->4                                                                                                                                                                                                                                                          Lodging->Room->Floor->Second
11                   Third                                              4                    Floor                                              2                    Lodging                                            (Lodging) ------------Third                                                                                                                                                                                                                                      3           1           2->3->4                                                                                                                                                                                                                                                          Lodging->Room->Floor->Third
29                   Fourth                                             4                    Floor                                              2                    Lodging                                            (Lodging) ------------Fourth                                                                                                                                                                                                                                     3           1           2->3->4                                                                                                                                                                                                                                                          Lodging->Room->Floor->Fourth
12                   Air Conditioned                                    3                    Room                                               2                    Lodging                                            (Lodging) --------Air Conditioned                                                                                                                                                                                                                                2           1           2->3                                                                                                                                                                                                                                                             Lodging->Room->Air Conditioned
20                   With Balcony                                       12                   Air Conditioned                                    2                    Lodging                                            (Lodging) ------------With Balcony                                                                                                                                                                                                                               3           1           2->3->12                                                                                                                                                                                                                                                         Lodging->Room->Air Conditioned->With Balcony
24                   Mountain View                                      20                   With Balcony                                       2                    Lodging                                            (Lodging) ----------------Mountain View                                                                                                                                                                                                                          4           1           2->3->12->20                                                                                                                                                                                                                                                     Lodging->Room->Air Conditioned->With Balcony->Mountain View
25                   Ocean View                                         20                   With Balcony                                       2                    Lodging                                            (Lodging) ----------------Ocean View                                                                                                                                                                                                                             4           1           2->3->12->20                                                                                                                                                                                                                                                     Lodging->Room->Air Conditioned->With Balcony->Ocean View
26                   Garden View                                        20                   With Balcony                                       2                    Lodging                                            (Lodging) ----------------Garden View                                                                                                                                                                                                                            4           1           2->3->12->20                                                                                                                                                                                                                                                     Lodging->Room->Air Conditioned->With Balcony->Garden View
52                   Smoking                                            20                   With Balcony                                       2                    Lodging                                            (Lodging) ----------------Smoking                                                                                                                                                                                                                                4           1           2->3->12->20                                                                                                                                                                                                                                                     Lodging->Room->Air Conditioned->With Balcony->Smoking
21                   Without Balcony                                    12                   Air Conditioned                                    2                    Lodging                                            (Lodging) ------------Without Balcony                                                                                                                                                                                                                            3           1           2->3->12                                                                                                                                                                                                                                                         Lodging->Room->Air Conditioned->Without Balcony
13                   Non Air Conditioned                                3                    Room                                               2                    Lodging                                            (Lodging) --------Non Air Conditioned                                                                                                                                                                                                                            2           1           2->3                                                                                                                                                                                                                                                             Lodging->Room->Non Air Conditioned
22                   With Balcony                                       13                   Non Air Conditioned                                2                    Lodging                                            (Lodging) ------------With Balcony                                                                                                                                                                                                                               3           1           2->3->13                                                                                                                                                                                                                                                         Lodging->Room->Non Air Conditioned->With Balcony
71                   EA                                                 3                    Room                                               2                    Lodging                                            (Lodging) --------EA                                                                                                                                                                                                                                             2           1           2->3                                                                                                                                                                                                                                                             Lodging->Room->EA
50                   Casabellas                                         2                    Lodging                                            2                    Lodging                                            (Lodging) ----Casabellas                                                                                                                                                                                                                                         1           1           2                                                                                                                                                                                                                                                                Lodging->Casabellas
51                   North Beach                                        50                   Casabellas                                         2                    Lodging                                            (Lodging) --------North Beach                                                                                                                                                                                                                                    2           1           2->50                                                                                                                                                                                                                                                            Lodging->Casabellas->North Beach
40                   Fooding                                            NULL                 NULL                                               40                   Fooding                                            (Fooding) Fooding                                                                                                                                                                                                                                                0           1           0                                                                                                                                                                                                                                                                Fooding
41                   Pizza                                              40                   Fooding                                            40                   Fooding                                            (Fooding) ----Pizza                                                                                                                                                                                                                                              1           1           40                                                                                                                                                                                                                                                               Fooding->Pizza
45                   Onion                                              41                   Pizza                                              40                   Fooding                                            (Fooding) --------Onion                                                                                                                                                                                                                                          2           1           40->41                                                                                                                                                                                                                                                           Fooding->Pizza->Onion
47                   Extra Cheeze                                       41                   Pizza                                              40                   Fooding                                            (Fooding) --------Extra Cheeze                                                                                                                                                                                                                                   2           1           40->41                                                                                                                                                                                                                                                           Fooding->Pizza->Extra Cheeze
77                   Burger                                             40                   Fooding                                            40                   Fooding                                            (Fooding) ----Burger                                                                                                                                                                                                                                             1           1           40                                                                                                                                                                                                                                                               Fooding->Burger

这个结果是使用一些包含一些DML操作的存储过程获得的。

我想要这样的东西

select description from exec spName where fk_category_id=5

请记住,这个spName返回了4个表,我想在其中一个表上执行一些查询,其索引将为我所知。 在进一步查询之前,我不必将其发送到用户界面。

我正在使用Sql Server 2008,但也想要2005的兼容解决方案。

3 个答案:

答案 0 :(得分:3)

这是不可能的。

我能想到的唯一方法是将存储过程中的结果插入到临时表中并查询临时表。当你返回4个表时,这在你的场景中不起作用。

你能不能将你感兴趣的查询重写为函数或视图吗?

编辑:

你会做一些事情(只有当Stored Proc返回一个表时)

Create Proc dbo.usp_FetchUsers

as

begin

Select UserId, UserName
From Users

End
Go

Create Table #t
(
UserId int,
UserName varchar(50)
)

Insert Into #t
Exec dbo.usp_FetchUsers

Select * From #t
Where UserId = 4

drop table #t

答案 1 :(得分:3)

修改这不适用于4个表,但您可以在评论中说明您可以转换为返回一个。

您可以使用OPENROWSET(在Paddy的链接问题中似乎没有提及)。这需要启用adhoc分布式查询。

使用示例

SELECT  *
FROM    OPENROWSET ('SQLOLEDB','Server=(local);TRUSTED_CONNECTION=YES;','set fmtonly off exec master.dbo.sp_who')
AS tbl

答案 2 :(得分:1)