I am trying to write a stored procedure in sql server 2005 which will first check if the phone exists in Database. If a row exists, it will give me MerchantID of that row. Else, it will check for CompanyName, City, State and gets the MerchanTID, else it does not give me anything. But i have this error coming up “Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The ‘CheckifMerchantexists’ procedure attempted to return a status of NULL, which is not allowed. A status of 0 will be returned instead. ” Where am i doing wrong? Thank you for all your help.
ALTER PROCEDURE [dbo].[CheckifMerchantexists]
(@CompanyName varchar(50)
,@Phone varchar(15)
,@City varchar(30)
,@State varchar(2)
)
AS
BEGIN
declare @MerchantID int
set @MerchantID = (Select MerchantID from Merchant where Phone = @Phone)
IF @MerchantID = null
Begin
set @MerchantID = (Select MerchantID from Merchant where CompanyName = @CompanyName and City = @City and State = @State)
return @MerchantID
END
else
Begin
return @MerchantID
End
You do not need to use “set” here
try replacing it with
But note that you may not get expected results if more than one Merchant has the same phone number. You may have to consider changing your logic.