如何设置存储过程参数以允许或不可以由用户传递值?

How to set stored procedure parameter for may or may not pass the values by user?

用户可能会也可能不会在 SQL 存储过程

中传递存储过程值中的值

那么如何在StoredProcedure中声明我的变量以及如何在Where子句中定义?

例子

CREATE PROC spGetRecord
@Storeid nchar(10)=null
AS
BEGIN
    SELECT ID,NAME
    FROM table1
    WHERE StoreID=@Storeid    //@Storeid is may pass the value by user or may not
END

那么在Where子句

中应该如何定义@Storeid

谢谢

我猜您正在构建某种搜索程序...

一个简单的开始,检查参数是否匹配 Storeid,或者参数是否为 null:

create proc spGetRecord (@Storeid nchar(10)=null) as
begin
  set nocount, xact_abort on;

  select id,name
  from table1
  where (Storeid=@Storeid or @Storeid is null)
end;

有关更高级的包罗万象的搜索查询,请参阅以下内容: