我正在尝试创建一个存储过程来确定我的customerid是否存在,如果它存在,那么我的其他参数foundcustomer将被分配给找不到的其他参数。我不确定如何分配找到请帮助 这是我试过的
CREATE PROCEDURE procedure4
-- Add the parameters for the stored procedure here
@FoundCustomer varchar(10) = null,
@Customerid varchar (5) = null
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
if Not(@Customerid is null)
SELECT customerid
from customers
where customerid = @Customerid
END GO
答案 0 :(得分:0)
Gordon是对的,听起来你可能想要一个函数,但如果它必须是一个存储过程,你可以按照这个例子。
CREATE PROCEDURE procedure4
@Customerid varchar(5) = null
AS
BEGIN
SET NOCOUNT ON;
DECLARE @FoundCustomer varchar(10) = ''
IF @FoundCustomer is not null
BEGIN
IF (SELECT COUNT(1) FROM customers WHERE customerid = @customerid) > 0
SET @FoundCustomer = 'Found'
ELSE
SET @FoundCustomer = 'Not Found'
END
SELECT @FoundCustomer
END