根据列值添加行

时间:2016-08-04 18:42:45

标签: sql sql-server tsql

我遇到了根据列值创建其他行的问题。

如果我的PageCount = 3,那么我需要额外增加2行,其中PONo会重复,但ImagePath会为每个新行增加1。

我能够获得第一行但是,在ImagePath增加1的情况下创建其他行是我被困住的地方。

我的结果:
enter image description here

预期结果:
enter image description here

完成:finished values

当前选择声明:

    public override void Process([NotNull] AddFromTemplateArgs args)
    {
        ID id; 

        if (args.Aborted 
            || string.IsNullOrWhiteSpace(RuleFolderId) 
            || !Settings.Rules.ItemEventHandlers.RulesSupported(args.Destination.Database)
            || !ID.TryParse(RuleFolderId, out id))
        {
            return;
        }

        Assert.HasAccess(args.Destination.Access.CanCreate(), "You do not have permission to create items here.");

        using (new EventDisabler()) // fixes the exception from nexus
        {
            var item = args.Destination.Database.Engines.DataEngine.AddFromTemplate(
                args.ItemName,
                args.TemplateId,
                args.Destination,
                args.NewId);

            args.ProcessorItem = item;
            args.Result = item;
        }

        var ruleContext = new PipelineArgsRuleContext<AddFromTemplateArgs>(args);

        RuleManager.RunRules(ruleContext, id);
    }

批处理文件:

        SELECT PO, CASE WHEN LEFT(u.Path,3)= 'M:\' THEN '\\ServerName\'+RIGHT(u.Path,LEN(u.Path)-3) ELSE u.Path END AS [Imagepath],PAGECOUNT
FROM OPENQUERY([LinkedServer],'select * from data.vw_purchasing_docs_unc') AS u INNER JOIN
OPENQUERY([LinkedServer],'select * from data.purchasing_docs') AS d ON u.docid=d.docid
WHERE (CONVERT(VARCHAR(10),d.STATUS_DATE,120)=CONVERT(VARCHAR(10),GETDATE(),120))

2 个答案:

答案 0 :(得分:3)

Select ponumber,b.rplc,pagecount
from table t
cross apply
(select replace(imagepath,'f'+cast(n-1) as varchar(100),'f0') as rplc from numbers n where n<=t.pagecount)b

要创建数字表,如果您想知道为什么需要它。查看here

CREATE TABLE Number (N INT IDENTITY(1,1) PRIMARY KEY NOT NULL);
GO

INSERT INTO Number DEFAULT VALUES;
GO 10000

更新后使用您的select语句:

;With cte(ponumber,imagepath,pagecount)
as
SELECT PO, CASE WHEN LEFT(u.Path,3)= 'M:\' THEN '\\ServerName\'+RIGHT(u.Path,LEN(u.Path)-3) ELSE u.Path END AS [Imagepath],PAGECOUNT
FROM OPENQUERY([LinkedServer],'select * from data.vw_purchasing_docs_unc') AS u INNER JOIN
OPENQUERY([LinkedServer],'select * from data.purchasing_docs') AS d ON u.docid=d.docid
WHERE (CONVERT(VARCHAR(10),d.STATUS_DATE,120)=CONVERT(VARCHAR(10),GETDATE(),120))
)
select Ponumber,b.rplc,pagecount from cte c
cross apply
 (select replace(imagepath,'f'+cast((n-1) as varchar(100)),'f0') as rplc from numbers n where n<=c.pagecount)b

答案 1 :(得分:0)

如果您想避免使用其他表格,可以使用CTE:

WITH Images AS
(
  SELECT * FROM (VALUES
  ('C:\Folder', 2),
  ('D:\Folder', 3)) T(ImagePath, Val)
), Numbers AS
(
    SELECT * FROM (VALUES (1),(2),(3),(4)) T(N)
    UNION ALL
    SELECT N1.N*4+T.N N FROM (VALUES(1),(2),(3),(4)) T(N) CROSS JOIN Numbers N1
    WHERE N1.N*4+T.N<=100
)
SELECT ImagePath + '\f' + CONVERT(nvarchar(10) ,ROW_NUMBER() OVER (PARTITION BY ImagePath ORDER BY (SELECT 1))) NewPath
FROM Images
CROSS APPLY (SELECT TOP(Val) * FROM Numbers)  T(N)

图片是您的源表。它可以是任何东西,即OPENQUERY。它产生:

NewPath
-------
C:\Folder\f1
C:\Folder\f2
D:\Folder\f1
D:\Folder\f2
D:\Folder\f3