CTE无法在SQL Server存储过程中工作

时间:2015-04-10 16:51:20

标签: c# stored-procedures sql-server-2012

当我使用CTE在C#应用程序中使用它时,我正在编写存储过程但由于某些原因,存储过程没有返回任何数据

这是我的存储过程

ALTER procedure [dbo].[sp_GetBrokerData]
     @broker Varchar
as
Begin
    With CTE AS 
    (
        select   
            Product, Term,
            CASE 
                WHEN BidBroker = @broker THEN BidBroker 
                ELSE null 
            END AS BidBroker,
            CASE 
                WHEN BidBroker = @broker THEN BidVolume 
                ELSE null 
            END AS BidVolume,
            CASE 
                WHEN BidBroker = @broker THEN BidCP 
                ELSE null 
            END AS BidCP,
            CASE 
                WHEN BidBroker = @broker THEN Bid 
                ELSE null 
            END AS Bid,
            CASE WHEN OfferBroker = @broker THEN Offer ELSE null end as Offer,
            CASE WHEN OfferBroker = @broker THEN OfferCP ELSE null end as OfferCP,
            CASE WHEN OfferBroker = @broker THEN OfferVolume ELSE null end as OfferVolume,
            CASE WHEN OfferBroker = @broker THEN OfferBroker ELSE null end as OfferBroker
        from 
            canadiancrudes
   )
   Select * 
   From CTE 
   Where not (Bid is null and Offer is null)
End

我可否知道上述程序中的问题?

1 个答案:

答案 0 :(得分:3)

类型为varchar的默认len变量为1.只需在适当的位置更改存储的proc参数类型len:

例如@broker varchar(20)