日期:2014-05-17  浏览次数:20454 次

存储过程根据参数值不同查询动态的列
本帖最后由 chai1338 于 2013-09-16 15:09:39 编辑

 if @FromPT<>'all'
WHILE @Next <= dbo.Get_StrArrayLength(@ProductIDs,',') --循环id集合
BEGIN
SET @CurrProductID = CONVERT(int,dbo.Get_StrArrayStrOfIndex(@ProductIDs,',',@Next))
   
INSERT INTO #ProductTmp ([FromPT],[ProductId], [Price])
SELECT 
    p.FromPT,
P.ProductID,
ISNULL((SELECT MIN(cn_Price) FROM Nop_Productvariant WHERE ProductID = P.ProductId and FromPT=@FromPT),0) AS Price
FROM 
Nop_Product P
WHERE
P.ProductID = @CurrProductID  AND Deleted = 0
            and p.FromPT=@FromPT
SET @Next=@Next+1
END

@FromPT的值为固定4个,不同的国家
如何在查询做个判断 根据@FromPT的值查询对应的不同价格列
当@FromPT为“中国”时,查询cn_Price列
当为“英国”时,查询en_Price列

------解决方案--------------------
case 
when @FromPT='中国' then cn_Price 
when @FromPT='英国' then en_Price 
end
------解决方案--------------------
 if @FromPT<>'all'
WHILE @Next <= dbo.Get_StrArrayLength(@ProductIDs,',') --循环id集合
BEGIN
SET @CurrProductID = CONVERT(int,dbo.Get_StrArrayStrOfIndex(@ProductIDs,',',@Next))
   
INSERT INTO #ProductTmp ([FromPT],[ProductId], [Price])
SELECT p.FromPT,P.ProductID,
case @FromPT when'中国' then cn_Price when'英国' then en_Price end as price
FROM Nop_Product P
LEFT JOIN Nop_Productvariant T ON T.ProductID = P.ProductId AND T.FromPT=@FromPT
WHERE P.ProductID = @CurrProductID  AND Deleted = 0 and p.FromPT=@FromPT
SET @Next=@Next+1
END