2015-08-22 54 views
-1

我正在尋找的是,如果我選擇停產,priceunit等它顯示我在where子句中的查詢部分..如何發送一個正確的參數給存儲過程

存在另一個這樣的方式?

USE [Northwind] 
GO 


ALTER PROCEDURE [dbo].[ProductosDiscontinuos] 
-- Add the parameters for the stored procedure here 
@discont bit, 
@Unit INT, 
@priceUnit INT, 
@cName VARCHAR(30), 
@CONSULTA nvarchar(MAX), 
@tipoConsulta nvarchar(MAX) 
AS 
BEGIN 

SET NOCOUNT ON; 
SELECT 
    p.ProductName, 
    s.CompanyName, 
    p.QuantityPerUnit, 
    p.UnitPrice, 
    p.UnitsInStock, 
    p.UnitsOnOrder, 
    p.ReorderLevel, 
    p.Discontinued, 
    c.CategoryName, 
    c.Description 
FROM 
    dbo.Categories c 
INNER JOIN 
    dbo.Products p ON 
    c.CategoryID=p.CategoryID 
INNER JOIN 
    Suppliers s ON 
    p.SupplierID=s.SupplierID 
WHERE 
@tipoConsulta = 
    (CASE 
    WHEN @CONSULTA='discontinued' THEN '[email protected]' 
    WHEN @CONSULTA='UnitsOnOrder' THEN 'p.UnitsOnOrder > @Unit' 
    WHEN @CONSULTA='UnitPrice' THEN 'p.UnitPrice > @priceUnit' 
    WHEN @CONSULTA ='CompanyNAme' THEN 's.CompanyNAme Like %@cName%' 
    END) 
END 

的執行腳本是...

USE [Northwind] 
GO 

DECLARE @return_value int 

EXEC @return_value = [dbo].[ProductosDiscontinuos] 
    @discont = 1, 
    @Unit = NULL, 
    @priceUnit = NULL, 
    @cName = NULL, 
    @CONSULTA = N'discontinued', 
    @tipoConsulta = N'discontinued' 

SELECT 'Return Value' = @return_value 

,但它並沒有顯示我什麼!

+1

也許你需要使用動態SQL – lad2025

+1

你的WHERE子句將始終爲假,'WHERE「停產」 =「p.Discontinued = @ discont'' – lad2025

回答

0
... 
WHERE 
    CASE 
     WHEN @CONSULTA = 'discontinued' AND p.Discontinued = @discont THEN 1 
     WHEN @CONSULTA = 'UnitsOnOrder' AND p.UnitsOnOrder > @Unit THEN 1 
     WHEN @CONSULTA = 'UnitPrice' AND p.UnitPrice > @priceUnit THEN 1 
     WHEN @CONSULTA = 'CompanyNAme' AND s.CompanyNAme LIKE '%' + @cName + '%' THEN 1 
    END = 1 

這些類型的查詢通常不是很有效,但它應該做你問什麼。還有一些你的大小寫不一致,我只是把它放在原來的位置。最後,我調整了LIKE表達式以正確連接百分比字符。

+0

是的,我錯了串聯的LIKE表達感謝名單!爲什麼這不有效?是爲了表演? – ger

+1

它無法使用索引,因爲它不知道要過濾哪個列。 – shawnt00

1

一種方法是構建動態查詢或只是簡單地使用IF THEN ELSE。

ALTER PROCEDURE [dbo].[ProductosDiscontinuos] 
    @discont bit, 
    @Unit INT, 
    @priceUnit INT, 
    @cName VARCHAR(30), 
    @CONSULTA nvarchar(MAX), 
    @tipoConsulta nvarchar(MAX) 
AS 
BEGIN 
SET NOCOUNT ON; 

IF @CONSULTA = N'discontinued' THEN 
BEGIN 
    SELECT 
     p.ProductName, 
     s.CompanyName, 
     p.QuantityPerUnit, 
     p.UnitPrice, 
     p.UnitsInStock, 
     p.UnitsOnOrder, 
     p.ReorderLevel, 
     p.Discontinued, 
     c.CategoryName, 
     c.Description 
    FROM dbo.Categories c 
    INNER JOIN dbo.Products p 
     ON c.CategoryID=p.CategoryID 
    INNER JOIN Suppliers s 
     ON p.SupplierID=s.SupplierID 
    WHERE 
     [email protected] 
END 
ELSE IF @CONSULTA = N'UnitsOnOrder' THEN 
BEGIN 
    /* SELECT */ 
    WHERE 
    p.UnitsOnOrder > @Unit 
END 
ELSE IF @CONSULTA = N'UnitPrice' THEN 
BEGIN 
    /* SELECT */ 
    WHERE 
    p.UnitPrice > @priceUnit 
END 
ELSE 
BEGIN 
    /* SELECT */ 
    WHERE 
    s.CompanyNAme Like %@cName%  /* This is definetely non-SARGable; poor performance */ 
END 

END

相關問題