根据仓位值集ID取仓位名称原创
金蝶云社区-Bat
Bat
8人赞赏了该文章 1,970次浏览 未经作者许可,禁止转载编辑于2020年06月17日 09:17:20

--注意调整红色业务数据表名

IF OBJECT_ID('TEMPDB..#DATA') IS NOT NULL 

BEGIN DROP TABLE #DATA END

SELECT ROW_NUMBER ( ) OVER ( ORDER BY T.FSTOCKID ) FROWNUM,

T.FSTOCKID,

T.FSTOCKLOCID,

'FF'+CAST (T.FFLEXID AS VARCHAR ) FFLEXID,

'' FFLEXVALUE

INTO #DATA

FROM (

SELECT DISTINCT

A.FSTOCKID,

A.FSTOCKLOCID,

B.FFLEXID

FROM

V_PLM_PRIRSSDETAIL A

INNER JOIN T_BD_STOCKFLEXITEM B ON A.FSTOCKID=B.FSTOCKID) T

ALTER TABLE #DATA ALTER COLUMN FFLEXVALUE NVARCHAR(MAX)

DECLARE @I INT = 1

WHILE (@I < (SELECT COUNT(*) FROM #DATA ))

BEGIN

    DECLARE @SQLRUN VARCHAR(MAX)

    SET @SQLRUN = 

( 'UPDATE #DATA 

SET FFLEXVALUE=

(SELECT ' + CAST ((SELECT FFLEXID FROM #DATA WHERE FROWNUM=@I) AS VARCHAR ) + ' FROM T_BAS_FLEXVALUESDETAIL A WHERE A.FID= '+CAST ((SELECT FSTOCKLOCID FROM #DATA WHERE FROWNUM=@I) AS VARCHAR ) +') WHERE FROWNUM='+CAST (@I AS VARCHAR )

    EXEC(@SQLRUN)

    SET @I = @I + 1

END

SELECT T2.FSTOCKLOCID, FSTOCKLOC = ( STUFF(( SELECT ',' + T1.FNAME FROM 

(SELECT DISTINCT A.FSTOCKLOCID,B.FNAME FROM #DATA A

INNER JOIN T_BAS_FLEXVALUESENTRY_L B ON A.FFLEXVALUE=B.FENTRYID AND B.FLOCALEID=2052

) T1 WHERE T1.FSTOCKLOCID = T2.FSTOCKLOCID FOR XML PATH ( '' ) ),1,1,'' ) ) 

FROM

(SELECT DISTINCT A.FSTOCKLOCID,B.FNAME FROM #DATA A

INNER JOIN T_BAS_FLEXVALUESENTRY_L B ON A.FFLEXVALUE=B.FENTRYID AND B.FLOCALEID=2052

) T2

GROUP BY

T2.FSTOCKLOCID

                            


图标赞 8
8人点赞
还没有人点赞,快来当第一个点赞的人吧!
图标打赏
0人打赏
还没有人打赏,快来当第一个打赏的人吧!