Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

multiple SELECT statements in a function

Posted on 2016-09-29
4
Medium Priority
?
73 Views
Last Modified: 2016-09-29
Hi,

I was wondering if it is possible to put multiple SELECT statements in a function.  What I want to do is to do something like this:

SELECT F1, F2, Fn('p1'), Fn('p2')
FROM table

Inside the function, different SELECT statements will be executed based on the parameter.  thanks
0
Comment
Question by:mcrmg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 41821968
do you mean to create a table valued function?

Table-Valued User-Defined Functions
https://technet.microsoft.com/en-us/library/ms191165(v=sql.105).aspx

or for Fn and Fn you can define them as a normal Function in MS SQL

CREATE FUNCTION (Transact-SQL)
https://msdn.microsoft.com/en-us/library/ms186755.aspx
0
 

Author Comment

by:mcrmg
ID: 41821986
I want to see if it is possible to do this

BEGIN  
	DECLARE @LookupValue varchar(20)
	SET @LookupValue = ''
	
	SELECT @LookupValue = 
	CASE
	WHEN @input = 'abc' THEN

			SELECT @LookupValue = 
								case f1
									when '1' then 'yes'
									when '2' then 'no'   end

					FROM table
					where id = 123
	WHEN @input = 'xyz' THEN

			SELECT @LookupValue = 
								case f2
									when '1' then 'open'
									when '2' then 'close'   end

					FROM table
					where id = 123
	END 








     RETURN @LookupValue
end;
go

Open in new window

0
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 41822117
Yes, you can.  But, for efficiency, avoid using a local variable unless absolutely required:

CREATE FUNCTION function_name (
    @input varchar(10)
)
RETURNS varchar
AS
BEGIN
RETURN (
    SELECT CASE @input
        WHEN 'abc' THEN (
            SELECT case id when '1' then 'yes'
                                       when '2' then 'no' end
            FROM table_name
            WHERE id = 123
                  )
            WHEN 'xyz' THEN (
            SELECT case id when '1' then 'yes'
                                       when '2' then 'no' end
            FROM table_name
            WHERE id = 123
                  )
            END AS result
)
END /*FUNCTION*/
GO
0
 

Author Closing Comment

by:mcrmg
ID: 41822130
It works.  Thank you very much.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question