2014-12-01 62 views
0

我开始使用sql数据库并尝试对其进行修改以使其使用批量插入(而不是逐个)的现有程序,并且还防止重复输入。SQL:编写忽略现有条目的批量插入脚本

下面是我得到了什么,我的函数添加一个用户(只要该用户不存在),这应该是工作:

use [DebugDatabase] 
go 

set ansi_nulls on 
go 

set quoted_identifier on 
go 

create procedure [dbo].[AddUser] 
    @Id bigint, @Login nvarchar(100), @ConsoleName nvarchar(100), @Ip nvarchar(50) 
as 
begin 
    set nocount on; 

    declare @FoundId bigint = (select Id from [User] where Id = @Id) 

    if @FoundId is null 
     begin 
      insert into [User] (Id, Login, ConsoleName, Ip) values (@Id, @Login, @ConsoleName, @Ip) 
     end 
    else 
     begin 
      update [User] 
      set [Id] = @Id, 
       [Login] = @Login, 
       [ConsoleName] = @ConsoleName, 
       [Ip] = @Ip 
      where Id = @Id 
     end 
    select top 1 * from [User] where Id = @Id; 
end 
go 

现在我想写一个函数用于批量插入,该函数调用上述函数来检查每个条目。我表现出的方​​式,这需要一个自定义的表类型:

use [DebugDatabase] 
go 

create type [dbo].[UserTableType] as table 
    (
    [Id] [bigint] not null, 
    [Login] [nvarchar](100) not null default 'Unknown', 
    [ConsoleName] [nvarchar](100) not null default 'Unknown', 
    [Ip] [nvarchar](50) not null default '0.0.0.0' 
    ) 
go 

,并添加多个条目的功能(这是我遇到的麻烦之一):

use [DebugDatabase] 
go 

set ansi_nulls on 
go 

set quoted_identifier on 
go 

create procedure [AddMultipleUsers] 
    @users UserTableType readonly 

as 

declare 
    @Id bigint, 
    @Login nvarchar(100), 
    @ConsoleName nvarchar(100), 
    @Ip nvarchar(50) 

begin 
    insert into @Id select Id from @users 
    insert into @Login select Login from @users 
    insert into @ConsoleName select ConsoleName from @users 
    insert into @Ip select Ip from @users 

    exec AddUser @Id, @Login, @ConsoleName, @Ip 
end 
go 

我得到“必须声明表变量”@Id“我不知道如何从表类型中提取各个值,以便将它们传递给AddUser函数,但我真正喜欢的是某种方式在一个函数调用中做到这一切,但我还没有遇到过这样的事情。

+0

您INSERT INTO“@id”将无法工作,因为“@Id”是一个BIGINT不是表。你应该像这样设置@Id参数:'设置@ Id =(从@users选择最高1个ID)' – Jt2ouan 2014-12-02 00:46:07

回答

3

这种类型的操作可以使用单个查询来完成,而无需使用存储过程。 (顺便说一句,请使用正确的术语:您的AddUser是一个存储过程,而不是一个函数)。请看看在MERGE命令

MERGE [User] AS target 
    USING (SELECT [Id], [Login], [ConsoleName], [Ip] FROM @users) AS source 
    (Id, Login, ConsoleName, Ip) 
    ON (target.Id = source.Id) 
WHEN MATCHED THEN 
     UPDATE SET [Login] = source.Login, 
        [ConsoleName] = source.ConsoleName, 
        [Ip] = source.Ip 
WHEN NOT MATCHED THEN 
    INSERT (Id, Login, ConsoleName, Ip) 
    VALUES (source.Id, source.Login, source.ConsoleName, source.Ip); 
+0

谢谢,这似乎迄今为止工作良好。由于长期目标是使用实体框架从c#程序中调用它,我相信它必须作为一个过程存储。 – 2014-12-02 01:19:18

+0

只需放入'[AddMultipleUsers]'的内部并摆脱您的DECLARE语句 – cha 2014-12-02 02:48:10

+0

这正是我所做的。谢谢你的帮助,cha。 – 2014-12-02 18:41:25

0
create procedure [AddMultipleUsers] 
    @users UserTableType readonly 

as 

declare 
    @Id bigint, 
    @Login nvarchar(100), 
    @ConsoleName nvarchar(100), 
    @Ip nvarchar(50) 

-- A opcion ... 
-- Declare the cursor for the list of users to be processed. 
DECLARE userT CURSOR FOR SELECT * FROM UserTableType ; 

-- Open the cursor. 
OPEN userT; 

-- Loop through the users. 
FETCH NEXT 
    FROM userT 
    INTO @Id, @Login, @ConsoleName, @Ip; 

WHILE @@FETCH_STATUS = 0 
BEGIN; 
    exec AddUser @Id, @Login, @ConsoleName, @Ip 

    FETCH NEXT FROM userT INTO @Id, @Login, @ConsoleName, @Ip; 
END; 

-- Close and deallocate the cursor. 
CLOSE partitions; 
DEALLOCATE partitions;