How to use a return Varchar value from a function in where clause

Hi there
,

I have a function which returns a string i want to use this in where clause to return the serviceCode A, C or both. I trying to do it as follows. But doesn't return any data when i use it in select statement. Any idean why? or i would appreciate if you could please tell me the better way of doing it?

ALTER FUNCTION [dbo].[ret_ServiceCode]
(
	-- Add the parameters for the function here
	@ServiceCode int
)
RETURNS nvarchar(200)
AS
BEGIN
	-- Declare the return variable here
	Declare @SerCode nvarchar(200)

	-- Add the T-SQL statements to compute the return value here
	If @ServiceCode = 1
   Begin
		set @SerCode = 'Select ServiceCode from tblServiceCodes Where ServiceCode = A'
   End
   Else  If @ServiceCode = 2
   Begin
		set @SerCode ='Select ServiceCode from tblServiceCodes Where ServiceCode = C'
   End
   Else  If @ServiceCode = 3
   Begin
		set @SerCode ='Select ServiceCode from tblServiceCodes Where ServiceCode in(A,C)'
   End

	-- Return the result of the function
	RETURN @SerCode

END

Open in new window


and in my select statement i am using the function as
select * from tblPatient where ServiceCode in (dbo.ret_ServiceCode(1))

Open in new window


can you please help
Ali ShahSQL DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vitor MontalvãoMSSQL Senior EngineerCommented:
It only returns a value, right?
Then use '=' instead of IN:
select * from tblPatient where ServiceCode = dbo.ret_ServiceCode(1)

Open in new window

0
Scott PletcherSenior DBACommented:
You can't return query code and have it run dynamically.

You could use an inline-table-valued function to get the values:

ALTER FUNCTION [dbo].[ret_ServiceCode]
(
      @ServiceCode int
)
RETURNS TABLE
AS
RETURN (
   Select ServiceCode
   From tblServiceCodes
   Where (@ServiceCode = 1 And ServiceCode = 'A')
      Or (@ServiceCode = 2 And ServiceCode = 'C')
      Or (@ServiceCode = 3 And ServiceCode IN ('A', 'C'))
)
GO --end function


select *
from tblPatient
where ServiceCode in (select servicecode from dbo.ret_ServiceCode(1))
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Walter RitzelSenior Software EngineerCommented:
The error is that you are returning a string containing a query, instead of the possible results.

Take a look on this documentation:
https://msdn.microsoft.com/en-us/library/ms188001.aspx
0
Ali ShahSQL DeveloperAuthor Commented:
Ah Thanks a lot. That works perfectly.
Kindest Regards
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.