SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Function returns table or message
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

GabrielAp
Starting Member

Brazil
3 Posts

Posted - 06/29/2013 :  21:52:58  Show Profile  Reply with Quote
Hello guys, I created a function that returns a table but I have to check if the table is empty if the table is empty I have to return a message, does anyone know how to perform the check if the table is empty? And return a message?

Here is my function:

REATE FUNCTION FNC_GANHADORES(@COD_PARTIDA INTEGER, @NOME_TIMEA VARCHAR(100),@PLACAR_TIMEA INTEGER, @NOME_TIMEB VARCHAR(100), @PLACAR_TIMEB INTEGER)
RETURNS TABLE

AS

RETURN

SELECT FKIDUSUARIO FROM APOSTA A
INNER JOIN PARTIDA ON (A.APOSTAPLACARSELECAO_A = PARTIDA.PLACARSELECAR_A)
WHERE PLACARSELECAR_A = @PLACAR_TIMEA AND PLACARSELECAO_B = @PLACAR_TIMEB

James K
Flowing Fount of Yak Knowledge

3712 Posts

Posted - 06/29/2013 :  23:31:32  Show Profile  Reply with Quote
Who is the consumer of the function? If you are using the function in your own code, then you can add the check to see if there are any rows in the table that is returned and generate the appropriate message.
Go to Top of Page

GabrielAp
Starting Member

Brazil
3 Posts

Posted - 06/30/2013 :  11:16:14  Show Profile  Reply with Quote
Sim eu mesmo realizo a chamada da funcao estou fazendo desta forma:
Select * from [dbo].[fnc_ganhadores](1,"Brasil",1,"Japao",2) porem nao sei como faco para realizar a verificação na chamada da função, voces podem me ajudar? Desde ja Obrigado.
Go to Top of Page

MuMu88
Aged Yak Warrior

547 Posts

Posted - 06/30/2013 :  13:12:36  Show Profile  Reply with Quote
Tente usar COUNT () operador, se ele retorna> = 1, em seguida, completar a sua chamada de erro de retorno esle função ou aviso.

Select COUNT(1) from [dbo].[fnc_ganhadores](1,"Brasil",1,"Japao",2)


Go to Top of Page

GabrielAp
Starting Member

Brazil
3 Posts

Posted - 06/30/2013 :  14:09:57  Show Profile  Reply with Quote
Muito obrigado pela ajuda gente, MuMu88 e James K, sabia que encontraria ajuda aqui.
A resolução ficou assim:

DECLARE @NUM INTEGER

SET @NUM = (SELECT COUNT (1) FROM [dbo].[FNC_GANHADORES](1,'BRASIL',0,'JAPAO',2))
BEGIN
IF @NUM = 0
PRINT 'NENHUMA APOSTA'
ELSE
SELECT * FROM [dbo].[FNC_GANHADORES](1,'BRASIL',0,'JAPAO',2)
END
Go to Top of Page

MuMu88
Aged Yak Warrior

547 Posts

Posted - 06/30/2013 :  15:37:51  Show Profile  Reply with Quote
nós estamos contentes de ajudar
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000