0

简而言之,我的问题是如何在没有游标(伪代码)的 t-sql 2012 中执行以下操作:

for each r in input_list:

    insert into t1(...) ...

    if (r.field1 is not null)
        insert into tA(...) (...@@identity ...   r.field1) ... 

    else if (r.field2 is not null)
        insert into tB(...) (...@@identity...    r.field2)  ...

长问题:

假设我有以下 3 个表,模拟对象可以是文件或目录的事实。

obj(id int, creation_date datetime)  -- all objects have a creation date.
file(id int, id_obj int, path nvarchar(max))  -- id_obj is a foreign key to obj
dir(id int, id_obj int, path nvarchar(max), shared bit) -- id_obj is a foreign key to obj

我需要编写一个存储过程,它采用“逻辑对象”列表(可以表示文件或目录)并且必须将它们添加到数据库中,即它必须为每个逻辑对象创建 1)obj 中的一行, 2)文件或目录中的一行(取决于逻辑对象是代表文件还是目录)。

为了编写这个存储过程,我创建了一个表示逻辑对象的表参数。这必须能够表示文件和目录,因此它必须包含文件和目录的(逻辑)字段的合并,如下所示:

create type logicalObj as table(
                                dirPath nvarchar(max) null, 
                                dirShared bit null,  
                                filePath nvarchar(max) null
                               )

我的存储过程是使用表值参数定义的,如下所示:

create procedure foo 
       -- this way the user can pass a list of logical objects to the stored proc
   @lo logicalObj readonly  .

as
begin
...
end

现在在程序主体中,我认为我需要执行类似(伪代码)的操作:

for each lo in @lo:

    insert into obj(creation_date)
        values (curdate())

    if lo.dirPath is not null
        insert into dir(id_obj, path, shared)
        values (@@identity, lo.dirPath, 1 )
    else if lo.filePath is not null
        insert into file(id_obj, path)
        values (@@identity, lo.dirPath )

我的问题:如何在没有游标的情况下做到这一点?如果需要,可以使用 t-sql 2012 独有的功能(例如序列)。

4

2 回答 2

1

您可以使用output子句从第一个基于集合的插入中捕获具有标识值的多行。然后,您可以使用ROW_NUMBER()子句将这些捕获的输出值与原始@lo变量中的行关联起来。

它会是这样的:

declare @IDs table (ID int not null)
insert into obj(creation_date)
output inserted.id into @IDs
select curdate() from @lo --Just makes sure there's one row per row in @lo

;with NumberedIDs as (
     select ID,ROW_NUMBER() OVER (ORDER BY ID) as rn from @IDs
), NumberedObjects as (
     select *,ROW_NUMBER() OVER (ORDER BY dirPath,filePath) as rn from @lo
)
insert into dir (id_obj, path, shared)
select nid.ID,no.dirPath,no.dirShared
from NumberedObjects no
       inner join
     NumberedIDs nid
       on
         no.rn = nid.rn
where
   no.dirPath is not null

;with NumberedIDs as (
     select ID,ROW_NUMBER() OVER (ORDER BY ID) as rn from @IDs
), NumberedObjects as (
     select *,ROW_NUMBER() OVER (ORDER BY dirPath,filePath) as rn from @lo
)
insert into file (id_obj, path)
select nid.ID,no.filePath
from NumberedObjects no
       inner join
     NumberedIDs nid
       on
         no.rn = nid.rn
where
   no.filePath is not null

重要的是在底部的两个插入中完全查询@lo并且NumberedObjects不要过早过滤,以便行号保持匹配。

于 2013-02-13T11:12:27.327 回答
0

为什么不只做三个插入呢?

像这样的东西:

INSERT into obj(creation_date)
SELECT curdate() FROM @lo WHERE (lo.dirPath is not null) OR (lo.dirPath is null AND lo.filePath is not null)

insert into dir(id_obj, path, shared)
SELECT @@identity, lo.dirPath, 1 FROM @lo WHERE lo.dirPath is not null

insert into file(id_obj, path)
SELECT @@identity, lo.dirPath, 1 FROM @lo lo WHERE lo.dirPath is null AND lo.filePath is not null
于 2013-02-13T11:12:06.950 回答