Solved

Using select and if statements MS SQL Function

Posted on 2011-02-17
2
457 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
2 Comments
 
LVL 75

Accepted Solution

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.‚Äč
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

747 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

13 Experts available now in Live!

Get 1:1 Help Now