创建一个函数 return a table SQL
Create a function to return a table SQL
我正在为 return 和 SQL 中的 table 创建一个函数,但是,我不知道如何创建该函数,我现在的函数是:
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
)
我需要什么,如果 select return 没有来自 table LoteDet,我需要的是 table LoteDetPg 中的 select 函数.
使用这种函数可以做到这一点吗?
可以用其他方法做到这一点吗?
首先Select从LoteDetTable获取数据并存储到变量中,如果变量计数不为零,从LoteDet获取数据否则使用LoteDetPgTable获取数据
试试这个:
CREATE FUNCTION fn_PegaDetalhesDoPagador ( @IdLoteDet BIGINT )
RETURNS @res TABLE
(
NomeSacado VARCHAR(60) ,
IdLoteDet VARCHAR(MAX) ,--check this cols data type
NossoNum VARCHAR(MAX) ,--check this cols data type
Compromisso VARCHAR(25) ,
CNPJSacado VARCHAR(20)
)
AS
BEGIN
IF EXISTS ( SELECT TOP 1 Sacados.NomeSac --better if you replace this with PK col
FROM dbo.LoteDet
INNER JOIN dbo.Sacados ON dbo.LoteDet.IDSac = dbo.Sacados.IDSac
WHERE idlotedet = @IdLoteDet )
BEGIN
INSERT INTO @res
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;
END;
ELSE
BEGIN
INSERT INTO @res
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;
END;
RETURN;
END;
我正在为 return 和 SQL 中的 table 创建一个函数,但是,我不知道如何创建该函数,我现在的函数是:
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
)
我需要什么,如果 select return 没有来自 table LoteDet,我需要的是 table LoteDetPg 中的 select 函数.
使用这种函数可以做到这一点吗?
可以用其他方法做到这一点吗?
首先Select从LoteDetTable获取数据并存储到变量中,如果变量计数不为零,从LoteDet获取数据否则使用LoteDetPgTable获取数据
试试这个:
CREATE FUNCTION fn_PegaDetalhesDoPagador ( @IdLoteDet BIGINT )
RETURNS @res TABLE
(
NomeSacado VARCHAR(60) ,
IdLoteDet VARCHAR(MAX) ,--check this cols data type
NossoNum VARCHAR(MAX) ,--check this cols data type
Compromisso VARCHAR(25) ,
CNPJSacado VARCHAR(20)
)
AS
BEGIN
IF EXISTS ( SELECT TOP 1 Sacados.NomeSac --better if you replace this with PK col
FROM dbo.LoteDet
INNER JOIN dbo.Sacados ON dbo.LoteDet.IDSac = dbo.Sacados.IDSac
WHERE idlotedet = @IdLoteDet )
BEGIN
INSERT INTO @res
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;
END;
ELSE
BEGIN
INSERT INTO @res
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;
END;
RETURN;
END;