Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Subquery Returned more than 1 Value

Author  Topic 

velnias2010
Posting Yak Master

125 Posts

Posted - 2010-11-24 : 11:13:31
SELECT form_field_value AS RefNumber,
(SELECT form_field_value
FROM form_data_tbl
WHERE (form_id = 888) AND (form_field_name = 'Surname')) AS Surname
FROM form_data_tbl AS x
WHERE (form_id = 888) AND (form_field_name = 'refNumber')

Hey See Above,

I want to basically pull surname and firstname from this db but its kinda awkward way data is stored.

Can u see above how I can get around the error ?

ahmeds08
Aged Yak Warrior

737 Posts

Posted - 2010-11-24 : 11:16:59
check the sub query.is it returning one record or multiple records.you have to refine the sub query so that it returns only one record.
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2010-11-24 : 11:20:26
Could change it to
(SELECT top 1 form_field_value
FROM form_data_tbl
WHERE (form_id = 888) AND (form_field_name = 'Surname')) AS Surname

But that might be the wrong value
Why not start from

SELECT x.form_field_value AS RefNumber,
x2.form_field_value as Surname
FROM form_data_tbl AS x
join form_data_tbl x2
on x.form_id = x2.form_id
and x2.form_field_name = 'Surname'
WHERE x.form_id = 888 AND x.form_field_name = 'refNumber'


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-11-24 : 11:21:07
can you show how data is stored in your table?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-11-24 : 11:22:25
It's a vertical table right..post the DDL



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -