我在sql server 2005中工作,我想知道是否有办法让存储过程为其参数选择变量a或变量b。
现在如果我宣布A和B它同时需要A和B.可以让它取代A或B吗?那么,如果另一个没有值,它会使用另一个吗?
例如:
@ID VARCHAR(255)
@MAKE VARCHAR(255)
AS
BEGIN
SET NOCOUNT ON
SELECT *
FROM CARS
WHERE ID = @ID OR MAKE =@MAKE
谢谢!
答案 0 :(得分:1)
create procedure MyProcedure
(
@ID VARCHAR(255) = NULL,
@MAKE VARCHAR(255) = NULL
)
AS
SET NOCOUNT ON
SELECT *
FROM CARS
WHERE
(@ID IS NOT NULL AND ID = @ID)
OR
(@MAKE IS NOT NULL AND MAKE =@MAKE)
答案 1 :(得分:1)
IF ID IS NULL
BEGIN
/*Code goes here*/
END
ELSE IF MAKE IS NULL
BEGIN
/*More code goes here*/
END
ELSE
BEGIN
/*Even more code goes here*/
END
我很确定你可以使用NULL来调用函数。
并且,也这样做:
@ID VARCHAR(255) = NULL
@MAKE VARCHAR(255) = NULL
答案 2 :(得分:1)
存储过程可以使用可选参数。
create procedure mysp
@a varchar(255) = NULL,
@b varchar(255) = NULL
as begin
select *
from cars
where (a = @a and @a is not NULL) or
(b = @b and @b is not NULL);
end;