Click here to Skip to main content
15,891,682 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
if count(issue) in project is 0
return 1
else if(count(issue)>0 and count(issue)<5)
return 0
else
return -1

I want to create function for above in sql...
Please tel me how to do
Posted
Updated 14-Feb-13 23:11pm
v4
Comments
Nandakishore G N 15-Feb-13 4:55am    
what have you done till now?paste it

1 solution

Your question is lacking in detail. Can you confirm what SQL tables you have. It looks like you're trying to count the number of rows in a table and return a value depending on that. Is it just total rows in the table or based on some condition. The following might help you

SQL
CREATE PROCEDURE SampleProc 

AS
BEGIN
	DECLARE @RowCount int
	SELECT @RowCount = COUNT(*) FROM project
	IF @RowCount = 0 
	BEGIN
		RETURN 1
	END
	IF @RowCount > 0 AND @RowCount < 5
	BEGIN
		RETURN 0
	END
	RETURN -1
END
GO

-- Sample call
DECLARE @RC int
 EXEC @RC = SampleProc
PRINT @RC


or as a function:

SQL
CREATE FUNCTION dbo.SampleFunc() RETURNS int
AS
BEGIN
    DECLARE @RowCount int
    SELECT @RowCount = COUNT(*) FROM project
    IF @RowCount = 0
    BEGIN
        RETURN 1
    END
    IF @RowCount > 0 AND @RowCount < 5
    BEGIN
        RETURN 0
    END
    RETURN -1

END
GO

select dbo.SampleFunc()
 
Share this answer
 
v3
Comments
Rohit Kolekar11 15-Feb-13 5:01am    
Ok thank you.... Can u please send me how to do it by using functions?
Chris Reynolds (UK) 15-Feb-13 5:41am    
Updated solution for functions

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900