Solved

multiple SELECT statements in a function

Posted on 2016-09-29
4
45 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
  • 2
4 Comments
 
LVL 49

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 69

Accepted Solution

by:
ScottPletcher earned 500 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now