我对 SQL 比较陌生,可以使用一些帮助。我将帮助我接近目标但没有实现目标的代码片段混合在一起。我的原始数据的要点如下所示:
- 产品 - 数量_运费
- P65238 - - - - - 4
- P65236 - - - - - 2
- P65240 - - - - - 3
我想要结束的基本内容如下所示:
- 产品 - UCC 代码
- P65238 - - 001475010
- P65238 - - 001475011
- P65238 - - 001475012
- P65238 - - 001475013
- P65236 - - 001475014
- P65236 - - 001475015
- P65240 - - 001475016
- P65240 - - 001475017
- P65240 - - 001475018
必须为每批产品分配一个唯一的 UCC 代码(开始于:001475010)到每批货物。下面的代码将根据订购的产品总量创建许多唯一的 UCC。
CREATE TABLE #TEMP(UCCs INT)
DECLARE @UCC INT
,@textXML XML
,@data NVARCHAR(MAX)
,@delimiter NVARCHAR(5)
,@ConCatString NVARCHAR(MAX)
,@LoopCounter INT
SET @ConCatString = ''
SET @UCC = 001475009 + 1
SET @LoopCounter = (
SELECT CAST(Q.DSPTOTQTY_0 AS INT)
FROM x3v6prem.PILOTNEW.SDELIVERY Q
WHERE Q.SOHNUM_0 = 'SO1300259')
IF @LoopCounter = 1 GOTO Skip_Loop
Continue_Loop:
SET @ConCatString = @ConCatString + CAST(@UCC AS NVARCHAR(MAX)) + ','
SET @UCC = @UCC + 1
SET @LoopCounter = @LoopCounter -1
IF @LoopCounter > 1 GOTO Continue_Loop
Skip_Loop:
SET @ConCatString = @ConCatString + CAST(@UCC AS NVARCHAR(MAX))
SELECT @data = @ConCatString,
@delimiter = ','
SELECT @textXML = CAST('<d>' + REPLACE(@data, @delimiter, '</d><d>') + '</d>' AS XML)
INSERT INTO #TEMP
SELECT T.split.value('.', 'nvarchar(max)') AS data
FROM @textXML.nodes('/d') T(split)
SELECT * FROM #TEMP
DROP TABLE #TEMP
接下来是代码,它将根据要运送的产品数量多次复制产品 ID。最后,这些数据将用于创建一个运输标签,该标签将放置在订购的每盒产品上,并为每个盒子显示一个唯一的(条形码)编号。
CREATE TABLE #TEMP2(Products NVARCHAR(MAX))
DECLARE @Item2 NVARCHAR(MAX)
,@textXML2 XML
,@data2 NVARCHAR(MAX)
,@delimiter2 NVARCHAR(5)
,@ConCatString2 NVARCHAR(MAX)
,@LoopCounter2 INT
,@LastLine2 INT
DECLARE CC CURSOR LOCAL FAST_FORWARD FOR
SELECT T.ITMREF_0
FROM x3v6prem.PILOTNEW.SDELIVERYD T
WHERE T.SOHNUM_0 = 'SO1300259'
ORDER BY T.SDDLIN_0
OPEN CC
SET @ConCatString2 = ''
SET @LastLine2 = (
SELECT CAST(L.DSPTOTQTY_0 AS INT)
FROM x3v6prem.PILOTNEW.SDELIVERY L
WHERE L.SOHNUM_0 = 'SO1300259')
GetNextItem2:
FETCH NEXT FROM CC INTO @Item2
IF @@FETCH_STATUS <> 0 GOTO EndBothLoops2
SET @LoopCounter2 = (
SELECT CAST(Q.QTY_0 AS INT)
FROM x3v6prem.PILOTNEW.SDELIVERYD Q
WHERE Q.SOHNUM_0 = 'SO1300259' AND Q.ITMREF_0 = @Item2)
Continue_Loop2:
IF @LoopCounter2 = 0 AND @LastLine2 > 1 GOTO GetNextItem2
SET @ConCatString2 = @ConCatString2 + CAST(@Item2 AS NVARCHAR(MAX)) + ','
SET @LoopCounter2 = @LoopCounter2 - 1
SET @LastLine2 = @LastLine2 - 1
IF @LoopCounter2 > 0 AND @LastLine2 > 1 GOTO Continue_Loop2
IF @LoopCounter2 = 0 AND @LastLine2 > 1 GOTO GetNextItem2
SET @ConCatString2 = @ConCatString2 + CAST(@Item2 AS NVARCHAR(MAX))
EndBothLoops2:
CLOSE CC
DEALLOCATE CC
SELECT @data2 = @ConCatString2
,@delimiter2 = ','
SELECT @textXML2 = CAST('<d>' + REPLACE(@data2, @delimiter2, '</d><d>') + '</d>' AS XML)
INSERT INTO #TEMP2
SELECT T.split.value('.', 'nvarchar(max)') AS data
FROM @textXML2.nodes('/d') T(split)
SELECT * FROM #TEMP2
DROP TABLE #TEMP2
如果我能简单地弄清楚如何将我放入两个单独的表中的输出放到一个表中,我的问题就会得到解决,我可以(我相信)管理其余的。我对正在使用的 XML 代码没有真正的经验,因此我无法辨别如何将多列插入到正在创建的表中。
其他较小的问题:我似乎无法以保留前导零的方式格式化 UCC 值