我想创建一个接受可选参数的存储过程。 我想完成以下内容:
CREATE PROC myProc(@myCriteria Int) AS
SELECT myFieldList
FROM myTable
IF @myCriteria IS NOT NULL
WHERE myCriteria = @myCriteria
答案 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)