2

I have created a stored procedure that takes an argument

ALTER procedure [dbo].[myprocedure](@myName char(20))
as
   select param1
   from table1
   where name = @myName

Now this is working but when I try to select parameters from other tables it is not working I tried like this after the first select

(Select param2 table2)

But I get

MS 512, Level 16, State 1, Procedure candidate, Line 3
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

Does anyone know hot to use the select to get values from more than one table? Thanks

2
  • Does that mean you want Param1 from table1 and Param2 from table 2? In that case use Union all Commented Jan 4, 2014 at 9:46
  • how would Union all be used Commented Jan 4, 2014 at 9:49

1 Answer 1

3

Use IN operator instead of = after Where clause

ALTER procedure [dbo].[myprocedure](@myName char(20))
as

select param1
from table1
where name = @myName

---
select param1
from table1
where name IN (Select param2 from table2)

Edit

select param1
from table1
where name = @myName
union
Select param2
from table2

Note that UNION is going to remove duplicate values after combining resultset from table1 and table2.

To keep duplicate values , use UNION ALL

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah but how can I match the name with @myName
So you want to match name column from table1 with param2 column of table2 as well as with paramaeter @myName ?
I want to match name colum from table1 with @myName and to return param2 from table 2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.