Declare @a varchar(100);
If @a = select productname from product
The above mentioned query is throwing error as my sql query (select product name from product) is returning multiple values.
Can someone help me with this?
Declare @a varchar(100);
If @a = select productname from product
The above mentioned query is throwing error as my sql query (select product name from product) is returning multiple values.
Can someone help me with this?
Try something like:
Declare @a varchar(100);
SELECT @a = 'MyProduct'
Then either
If @a = select TOP 1 productname from product ORDER BY <some field>
OR
If @a IN (select productname from product)
However, how do you know which product(s) to match to; you might need a WHERE
clause. Some sample data and desired results would help.
Please note that you need to put the SELECT
query inside the parenthesis in this case! Also you should note that your select query, in this case should not return more than one value. So to resolve these you need to write it as:
Declare @a varchar(100);
If @a = (select TOP 1 productname from product)
However your query logically seems to be invalid and you should rethink about it, for example you need to say that, you are going to check @a with which product? You might need to add some filters to query and/or add ELSE
to your if, etc.
You might also need to read the @PeterSmith's answer too(Using IN
...)