9

我有这个存储过程生成的结果。

我想将这些结果与另一个表中的数据连接起来。我已经看到了创建临时表并将其插入其中的各种示例,但是,这并不理想,因为存储过程会返回许多可能会更改的动态列。有没有办法动态加入它们?

示例场景:

存储过程返回:

EXEC uspGetProductCategories

products_id | products_model | Leather Seats | Heated Seats | Tapedeck | Heater | Hybrid | Sunroof | Cruise Control
===================================================================================================================
100         | Saturn Vue     | N             | N            | Y        | N      | N      | N       | N
200         | Toyota Pruis   | Y             | N            | N        | Y      | Y      | N       | N
300         | Ford Focus     | N             | N            | N        | Y      | N      | N       | Y

我想用一个 SQL 查询加入它,该查询会生成如下内容:

SELECT * FROM Products_Detail

products_id | manufacturer | purchaser | pay_type
=================================================
100         | GM           | GREG      | P
200         | TOYT         | SAM       | P
300         | FORD         | GREG      | L

换句话说...

有没有一种无痛的方法可以做到这一点?这是我想要实现的一些伪代码(尽管我知道这不起作用):

SELECT pd.*, sp.* FROM Products_Detail pd
    LEFT JOIN uspGetProductCategories sp ON pd.product_id = sp.product_id

同样,我知道你不能这样做,但希望它描述了我正在寻找的逻辑。

示例所需的输出

products_id | manufacturer | purchaser | pay_type | products_model | Leather Seats | Heated Seats | Tapedeck | Heater | Hybrid | Sunroof | Cruise Control
=========================================================================================================================================================
100         | GM           | GREG      | P        | Saturn Vue     | N             | N            | Y        | N      | N      | N       | N
200         | TOYT         | SAM       | P        | Toyota Pruis   | Y             | N            | N        | Y      | Y      | N       | N
300         | FORD         | GREG      | L        | Ford Focus     | N             | N            | N        | Y      | N      | N       | Y
4

4 回答 4

3

如果您无法使用来自动态存储过程的数据创建临时表,为什么不直接加入该表:

DECLARE @cols AS NVARCHAR(MAX),
    @colsNull AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(categories_name) 
                    from Categories
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

select @colsNull = STUFF((SELECT ',IsNull(' + QUOTENAME(categories_name)+', ''N'')'+' as '+QUOTENAME(categories_name) 
                    from Categories
                    group by categories_name, categories_id
                    order by categories_id
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = '
            select *
            from Products_Detail pd
            left join
            (
              SELECT products_id,
                    products_model,' + @colsNull + ' from 
               (
                  select p.products_id,
                    p.products_model,
                    c.categories_name,
                    ''Y'' flag
                  from products p
                  left join Products_Categories pc
                    on p.products_id = pc.products_id
                  left join Categories c
                    on pc.categories_id = c.categories_id
              ) x
              pivot 
              (
                  max(flag)
                  for categories_name in (' + @cols + ')
              ) p 
            ) p 
              on pd.products_id = p.products_id'

execute(@query)

请参阅带有演示的 SQL Fiddle

于 2013-02-25T23:03:01.757 回答
0

如果无法重新组织代码,请尝试使用这个丑陋的解决方案。

SELECT * INTO #tmp
FROM OPENROWSET('SQLNCLI', 'server=INSTANCENAME;database=DBNAME;trusted_connection=yes', 'uspGetProductCategories') A

您必须Ad Hoc Distributed Queries在您的服务器中允许。

于 2013-02-25T22:48:02.990 回答
-1

你可以这样做:

        INSERT aTemptable 
        EXEC yourstoredproc

如果 #t 已定义,但您不能选择进入 #t 并动态创建它。

于 2013-02-25T22:37:15.680 回答
-1
create proc sp_emp18
(
@cust_id int,@name varchar(20),@order_id int,@order_date date
)
as 
begin
select e.cust_id,e.name,d.order_id,d.order_date 
from 
customer e inner join orders d on e.cust_id=d.cust_id
end

exec sp_emp18 101,'ram',99,'2016-07-21'
于 2016-08-04T09:59:33.973 回答