[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Using select and if statements MS SQL Function

Posted on 2011-02-17
2
Medium Priority
?
464 Views
Last Modified: 2012-06-27
Hi,

I have the following funciton working fine although I want to modify it so that it runs a select statement first that checks if the Builder's status is awarded.  

From there I want to have an if statement to run either the below select statment or a differnet one based on whether or not hte builder is awarded.

Any ideas?
ALTER FUNCTION [dbo].[fnBuildersByProject]
      (@ProjectID int , @PID int  )
RETURNS varchar(1000) 
AS
BEGIN
declare @List varchar(1000)

SELECT            @List = COALESCE(@List + ', ', '') + ltrim(rtrim(convert(varchar,BuilderCompany)))
FROM      Builders
WHERE       Builders.projectID = @ProjectID
ORDER BY   Builders.BuilderCompany

RETURN @List

 

END

Open in new window

0
Comment
Question by:glit
[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 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 2000 total points
ID: 34923203
Something like this:
ALTER FUNCTION [dbo].[fnBuildersByProject]
    (@ProjectID int,
     @PID int)
RETURNS varchar(1000)
AS 
BEGIN
    DECLARE @List varchar(1000)

	IF EXISTS (
		SELECT 1
		FROM   YourTable
		WHERE  BuilderStatus = 1
		)
		SELECT  @List = COALESCE(@List + ', ', '') + LTRIM(RTRIM(CONVERT(varchar, BuilderCompany)))
		FROM    Builders
		WHERE   Builders.projectID = @ProjectID
		ORDER BY Builders.BuilderCompany
	ELSE
		SELECT ...
		FROM ...
		WHERE ...
	
    RETURN @List

 

END

Open in new window

0
 

Author Closing Comment

by:glit
ID: 34923236
That's perfect, thank you!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

656 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