存储过程返回表

Posted rogerlu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了存储过程返回表相关的知识,希望对你有一定的参考价值。

ALTER PROCEDURE proc_xsddgz_lj
AS
--声明变量
--销售订单
BEGIN
DECLARE @fbillno NVARCHAR(255)
DECLARE @ddfentryid INT
DECLARE @fhdfentryid INT
CREATE TABLE #t(DDID INT,FHDID INT ,CKDID INT,ZGYSDID INT,CWYSDID INT)
CREATE TABLE #tt(DDID INT,FHDID INT ,CKDID INT,ZGYSDID INT,CWYSDID INT)
CREATE TABLE #ttt(DDID INT,FHDID INT ,CKDID INT,ZGYSDID INT,CWYSDID INT)
CREATE TABLE #tttt(DDID INT,FHDID INT ,CKDID INT,ZGYSDID INT,CWYSDID INT)
DECLARE @t TABLE(DDID INT,FHDID INT ,CKDID INT,ZGYSDID INT,CWYSDID INT)

--DECLARE @proc_xsddgz_lj TABLE(
--DDID INT,FHDID INT ,CKDID INT,ZGYSDID INT,CWYSDID INT
--)
SET @fbillno = ‘RA181274390‘
INSERT INTO #t(DDID)SELECT soe.FENTRYID
FROM T_SAL_ORDER so
INNER JOIN dbo.T_SAL_ORDERENTRY soe ON so.FID = soe.FID
WHERE so.FBILLNO = @fbillno

--SELECT *FROM #t
--drop TABLE #t
--DROP TABLE #tt
--发货单
INSERT INTO #tt(DDID,FHDID)
SELECT t.DDID,fhd.FENTRYID FROM #t t LEFT JOIN
(
SELECT sde.FENTRYID,sdel.FSID FROM dbo.T_SAL_DELIVERYNOTICEENTRY sde
INNER JOIN T_SAL_DELIVERYNOTICEENTRY_lk sdel ON sde.FENTRYID = sdel.FENTRYID
INNER JOIN T_SAL_DELIVERYNOTICE sd ON sd.fid = sde.FID
WHERE sd.FDOCUMENTSTATUS=‘C‘ )fhd ON t.DDID = fhd.FSID

TRUNCATE TABLE #t
INSERT INTO #t SELECT *FROM #tt
TRUNCATE TABLE #tt


--出库单
INSERT INTO #tt (DDID,FHDID,CKDID)
SELECT t.DDID,t.FHDID,ck.FENTRYID FROM #t t LEFT JOIN (
SELECT soe.FENTRYID,soel.FSID,soe.FACTQTY FROM dbo.T_SAL_OUTSTOCKENTRY soe
INNER JOIN dbo.T_SAL_OUTSTOCKENTRY_LK soel ON soe.FENTRYID = soel.FENTRYID
INNER JOIN T_SAL_OUTSTOCK so ON so.FID = soe.FID
WHERE so.FDOCUMENTSTATUS = ‘C‘) ck ON ck.FSID =t.FHDID


TRUNCATE TABLE #t
INSERT INTO #t SELECT *FROM #tt
TRUNCATE TABLE #tt


--暂估应收
INSERT INTO #ttt (DDID,FHDID,CKDID,ZGYSDID)
SELECT t.DDID,t.FHDID,t.CKDID ,zgys.FENTRYID FROM #t t LEFT JOIN (
SELECT ree.FENTRYID,relk.FSID,ree.FPRICEQTY FROM dbo.T_AR_RECEIVABLE re
INNER JOIN dbo.T_AR_RECEIVABLEENTRY ree ON re.FID = ree.FID
INNER JOIN dbo.T_AR_RECEIVABLEENTRY_LK relk ON ree.FENTRYID = relk.FENTRYID
WHERE re.FSETACCOUNTTYPE=2 AND re.FDOCUMENTSTATUS = ‘C‘)
zgys ON zgys.FSID = t.CKDID


TRUNCATE TABLE #t
INSERT INTO #t SELECT *FROM #ttt
TRUNCATE TABLE #ttt

--财务应收
INSERT INTO #tttt (DDID,FHDID,CKDID,ZGYSDID,CWYSDID)
SELECT t.DDID,t.FHDID,t.CKDID ,t.ZGYSDID,cwys.FENTRYID FROM #t t LEFT JOIN (
SELECT ree.FENTRYID,relk.FSID,ree.FPRICEQTY,re.FSETACCOUNTTYPE FROM dbo.T_AR_RECEIVABLE re
INNER JOIN dbo.T_AR_RECEIVABLEENTRY ree ON re.FID = ree.FID
INNER JOIN dbo.T_AR_RECEIVABLEENTRY_LK relk ON ree.FENTRYID = relk.FENTRYID
WHERE re.FSETACCOUNTTYPE=3 AND re.FDOCUMENTSTATUS = ‘C‘)
cwys ON cwys.FSID = t.ZGYSDID


TRUNCATE TABLE #t
INSERT INTO #t SELECT *FROM #tttt
TRUNCATE TABLE #tttt

INSERT INTO @t ( DDID, FHDID, CKDID, ZGYSDID, CWYSDID )SELECT *FROM #t

DROP TABLE #t
DROP TABLE #tt
DROP TABLE #ttt
DROP TABLE #tttt
SELECT *FROM @t
end

-----------------------------------------

declare @table table(DDID INT,FHDID INT ,CKDID INT,ZGYSDID INT,CWYSDID INT)--定义表变量来存放存储过程返回的内容
insert into @table EXEC proc_xsddgz_lj --将存储过程执行的结果放入表变量中
select *from @table

 

 

 

--------------------------------------

直接   EXEC proc_xsddgz_lj 也可以

以上是关于存储过程返回表的主要内容,如果未能解决你的问题,请参考以下文章

MYSQL的存储过程如何返回查询到的行数据?

Sqlserver存储过程返回表,如何实现?

oracle 存储过程,数据处理并返回结果集问题

SQL SERVER里面如何在存储过程里面获取另一个存储过程所返回的表的数据?

SQL SERVER里面如何在存储过程里面获取另一个存储过程所返回的表的数据?

存储过程/函数可以返回表吗?