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

x
?
Solved

How to created User Functions in T-SQL to include in main select statement, if possible?

Posted on 2011-03-04
5
Medium Priority
?
303 Views
Last Modified: 2012-05-11
Hi,

I need to use a subselect in a select statement, ie

select ID, (select name from.... where ID1 = @ID1 and ID2 = @ID2) as Name from  .....

Now this subselect has got more complicated so rather recopy this sql all over the place I was wondering whether it was possible to just create a User Function/Stored Function/Stored Procedure instead and if possible how to do it. So instead I would hope to:

select ID, (GetName(@ID1,@ID2) as Name from  ...

So my question is really about is GetName possible and how to do it?

Thanks,

Sam..
0
Comment
Question by:SamJolly
[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
  • 3
  • 2
5 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 35035023
yes, that is perfectly possible

create function dbo.GetName(@id1 int, @id2 int) returns varchar(100)
as return ( select name from.... where ID1 = @ID1 and ID2 = @ID2 ) 

Open in new window


and use it:
select ID, dbo.GetName(ID1, ID2) as Name 
from  ..... 

Open in new window


note: the dbo (schema) prefix owner is important

0
 

Author Comment

by:SamJolly
ID: 35035156
Thanks for this. COuld I trouble you for some more detail. My code iverview so far is :

create function dbo.GetDefinitionName(@FkDefinitionID UNIQUEIDENTIFIER, @ClientID UNIQUEIDENTIFIER) returns varchar(50)
as return
(
SELECT ISNULL
(
(
SELECT Name
FROM .... etc
),
NULL
)
)

I am getting "Incorrect syntax near 'RETURN'." Something stupid I am sure.

Thanks again.

Sam
0
 

Author Comment

by:SamJolly
ID: 35035235
Nearest I have got and working I think is:

 
create function dbo.GetDefinitionName(@FkDefinitionID UNIQUEIDENTIFIER, @ClientID UNIQUEIDENTIFIER) returns varchar(50)
as 
BEGIN
DECLARE @DefinitionName varchar(50)

SET @DefinitionName =
(
SELECT ISNULL
(
( 
SELECT Name 
FROM ....etc...),
NULL
) 
) 

RETURN @DefinitionName

END

Open in new window



Is this what you mean?

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35035347
yes, that is fine. what I posted was not double-checked, and indeed would be a table-valued function, you want a single value to be returned.
0
 

Author Closing Comment

by:SamJolly
ID: 35035465
thanks. Much appreciated.
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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

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