Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Code
C#
SqlConnection con = new SqlConnection(@"server=ZAIN\SQLEXPRESS;database=Project;user id=sa;pwd=abdeen");
                SqlDataAdapter com = new SqlDataAdapter("execute UpdateShopDetails '" + txtName.Text + "','" + txtAddress.Text + "','" + txtPhoneNo.Text + "'", con);
                DataSet ds = new DataSet();
                con.Open();
                com.Fill(ds);
                con.Close();


Stored Procedure
SQL
create proc UpdateShopDetails
@Name char(50) , @Address varchar(100), @PhoneNo varchar(20)
as
Update Shops
set Name=@Name
where Sid=(select Sid from Shops where Name=@Name)
Update ContactInfo
set Sid=(select Sid from Shops Where Name= @Name)
where Sid=(select Sid from Shops Where Name= @Name)
Update AddressInfo
set Contactid=(select Contactid from ContactInfo where Sid=(select Sid from Shops where Name=@Name)),Address=@Address
where Contactid=(select Contactid from ContactInfo where Sid=(select Sid from Shops where Name=@Name))
Update PhoneInfo
set Contactid=(select Contactid from ContactInfo where Sid=(select Sid from Shops where Name=@Name)),PhoneNo=@PhoneNo
where Contactid=(select Contactid from ContactInfo where Sid=(select Sid from Shops where Name=@Name))



Error
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Posted
Updated 14-May-13 0:10am
v2

SQL
Your getting this error because your subquery is returning more than one value and you are using "=" operator which is anticipating single value.  One to way solve the problem is to use "TOP 1" in your subquery.
For Example
set Sid=(select Top 1 Sid from Shops Where Name= @Name)
 
Share this answer
 
Comments
Zain ul abdeen 14-May-13 6:24am    
What if I use exists in subqueries?
First off, can I suggest that you only read the Sid and ContactId once per execution, and store them in a variable? Then ensure it returns only a single value:
SQL
DECLARE @SID INT
Set @SID = (SELECT TOP 1 Sid FROM MyTable WHERE Name=@Name)
Then use the variable in each of your updates.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900