I'm creating a function to return an table in SQL, but, I don't know how exactly to create the function, my function at now is:
CREATE FUNCTION fn_PegaDetalhesDoPagador ( @IdLoteDet bigint )
RETURNS TABLE
(
NomeSacado varchar(60),
CNPJSacado varchar(20),
Compromisso varchar(25)
)
AS
RETURN
(
SELECT Sacados.NomeSac,
LoteDet.IdLoteDet,
LoteDet.NossoNum,
LoteDet.Comprom,
dbo.Sacados.CNPJ
FROM dbo.LoteDet INNER JOIN dbo.Sacados ON dbo.LoteDet.IDSac = dbo.Sacados.IDSac
WHERE idlotedet=@IdLoteDet
IF @@ROWCOUNT = 0
BEGIN
SELECT Sacados.NomeSac,
LoteDetPg.IdLoteDet,
LoteDetPg.NossoNum,
LoteDetPg.Comprom,
dbo.Sacados.CNPJ
FROM dbo.LoteDetPg INNER JOIN dbo.Sacados ON dbo.LoteDetPg.IDSac = dbo.Sacados.IDSac
WHERE idlotedet=@IdLoteDet
RETURN
END
else
RETURN
)
What I need, if the select returns nothing from table LoteDet, I need than the function do the select in table LoteDetPg.
It's possible to do this using this kind of function ?
It's possible to do this with some other method ?