案件条件在何种情况下

时间:2014-12-29 20:52:10

标签: sql-server case

我想创建一个接受可选参数的存储过程。 我想完成以下内容:

CREATE PROC myProc(@myCriteria Int) AS
SELECT myFieldList
FROM myTable
IF @myCriteria IS NOT NULL
WHERE myCriteria = @myCriteria

1 个答案:

答案 0 :(得分:1)

试试这个。

CREATE PROC myProc(@myCriteria Int) AS
SELECT myFieldList
FROM myTable
WHERE (myCriteria = @myCriteria) or (@myCriteria IS NULL and 1=1) 

更新了Phillip建议

CREATE PROC myProc(@myCriteria Int) AS
SELECT myFieldList
FROM myTable
WHERE (@myCriteria is null OR myCriteria = @myCriteria)