I want to know if what user input parameter exists in a column in a table in SQL.
For example: check if "john" exists in column CustomerName
in the Customer
table.
I have tried exist
keyword, but I do not completely understand it. Can someone help with a solution for this?
This is my code:
create procedure getOrderWithCusID
@customerId char
as
if (@customerId EXISTS(SELECT o.CustomerID FROM Orders o))
begin
select *
from Orders o
where o.CustomerID = @customerId;
end
else
print('This customer does not exist');
return;
end;
The procedure you're trying to write should look something like:
Although really it's not necessary to use exists first and hit the table twice - you can simply check if any rows are returned and handle accordingly; also presumably your CustomerId is an
int
not achar
with length 1...