Solved

multiple SELECT statements in a function

Posted on 2016-09-29
4
37 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
It works.  Thank you very much.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

763 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

9 Experts available now in Live!

Get 1:1 Help Now