Click here to Skip to main content
15,921,694 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi..
I created funtion inside stored procedure and while executing proc,iam getting error as "Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression."

This is my fuction,
SQL
create function FunctionDetails()
returns nvarchar
as
begin
return ( select first_name + last_name as 'Name' from GeneralDetails where first_name like '%b%');
end


And this is my procedure,
SQL
create procedure selectDetailsUsingFunction
(
@id nvarchar
)
as
begin
select @id =dbo.FunctionDetails()
return @id
end


As am new to sql,guide me what iam wrong in this...
Posted
Comments
[no name] 18-Dec-12 5:21am    
Here your stored procedure asks for ID but your function returns name value. Please be sure what you need to do here. Rectify accordingly...

Hi,

This is absolutely wrong for getting a single record using like operator.
Instead of using the like operator you can use the exact name like:

select ID from GeneralDetails where first_name = 'b'


If you want to retrive the latest record then you can use :

select top 1 ID from GeneralDetails where first_name like '%b%' order by first_name desc

or
<pre> select top 1 ID from GeneralDetails where first_name like '%b%' order by first_name desc</pre>


These are two different methods you can get the result.

Thanks
 
Share this answer
 
Hi Priya,

In your function, there is a select query with a Where clause. It must be returning multiple records based on the search and where conditions.

Try to implement like this:

SQL
select TOP 1 first_name + last_name as 'Name' from GeneralDetails where first_name like '%b%'


It should resolve the error, but not exactly the answer that you need here. The where condition (also generally called as 'Clause') is not limiting the search to only 1 record here, instead fetching all the records which has 'b' in the first_name column.

Say if the letter 'b' exists in 2 or more records, the search query returns a table but the function can only returns a varchar.


Hope you understood the issue.

Have fun with coding...!

Regards,
Vamsi
 
Share this answer
 
As you are trying to get list of names from Table GeneralDetails. And your Sql functions type is Scaler-valued Function which is always return single value not list of values.

to achieve the same try to use this code ::
In UserDefined Function
SQL
CREATE function FunctionDetails()
returns @returnTable Table ( FullName varchar(50) )
as
begin
INSERT INTO @returnTable 
		Select FirstName + ' ' + LastName from SalesLT.Customer Where FirstName LIKE '%b%'
RETURN		
end 


And In Stored Procedure
SQL
select * from dbo.FunctionDetails()
 
Share this answer
 
SQL
select first_name + last_name as 'Name' from GeneralDetails where first_name like '%b%'


this line returns more than once in your query . instead of write "like" in where condition better you can write like this.

SQL
select first_name + last_name as 'Name' from GeneralDetails where first_name = 'b'


what are the test your searching you mentioned in the where condition .
 
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