我开始使用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函数,但我真正喜欢的是某种方式在一个函数调用中做到这一切,但我还没有遇到过这样的事情。
您INSERT INTO“@id”将无法工作,因为“@Id”是一个BIGINT不是表。你应该像这样设置@Id参数:'设置@ Id =(从@users选择最高1个ID)' – Jt2ouan 2014-12-02 00:46:07