我正在尋找的是,如果我選擇停產,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
,但它並沒有顯示我什麼!
也許你需要使用動態SQL – lad2025
你的WHERE子句將始終爲假,'WHERE「停產」 =「p.Discontinued = @ discont'' – lad2025