如何在 SQL Server 2005 中插入值数组?

发布于 2024-07-06 11:14:14 字数 77 浏览 13 评论 0原文

如何编写 SQL 代码来 INSERT(或 UPDATE)一个值数组(可能带有一个伴随的字段名数组,或者带有它们的矩阵)而不进行简单的迭代?

How do I write the SQL code to INSERT (or UPDATE) an array of values (with probably an attendant array of fieldnames, or with a matrix with them both) without simple iteration?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

痴骨ら 2024-07-13 11:14:15

我将列表构造为 xml 字符串并将其传递给存储过程。 在SQL 2005中,它增强了XML功能来解析XML并进行批量插入。

检查这个帖子:
使用 XML 参数将列表传递到 SQL Server 2005

I construct the list as an xml string and pass it to the stored procs. In SQL 2005, it has enhanced xml functionalities to parse the xml and do a bulk insert.

check this post:
Passing lists to SQL Server 2005 with XML Parameters

蒗幽 2024-07-13 11:14:15

将值连接到列表并将其传递给 sp 的简单方法。

在 sp 中使用 dbo.Split udf 转换回结果集(表)。

创建这个函数:

CREATE FUNCTION dbo.Split(@String nvarchar(4000), @Delimiter char(1))
returns @Results TABLE (Items nvarchar(4000))
as
begin
declare @index int
declare @slice nvarchar(4000)

select @index = 1
if @String is null return

while @index != 0

begin
select @index = charindex(@Delimiter,@String)
if @index !=0
select @slice = left(@String,@index - 1)
else
select @slice = @String

insert into @Results(Items) values(@slice)
select @String = right(@String,len(@String) - @index)
if len(@String) = 0 break
end return
end

然后尝试:

select * from dbo.split('a,b,c,d,e,f,g,h,i,j,k,l', ',')

Simple way to concatenate the values into a list and pass it to the sp.

In the sp use dbo.Split udf to convert back to resultset (table).

Create this function:

CREATE FUNCTION dbo.Split(@String nvarchar(4000), @Delimiter char(1))
returns @Results TABLE (Items nvarchar(4000))
as
begin
declare @index int
declare @slice nvarchar(4000)

select @index = 1
if @String is null return

while @index != 0

begin
select @index = charindex(@Delimiter,@String)
if @index !=0
select @slice = left(@String,@index - 1)
else
select @slice = @String

insert into @Results(Items) values(@slice)
select @String = right(@String,len(@String) - @index)
if len(@String) = 0 break
end return
end

and then try:

select * from dbo.split('a,b,c,d,e,f,g,h,i,j,k,l', ',')
娜些时光,永不杰束 2024-07-13 11:14:15

如果您的数据已在数据库中,您可以使用 INSERT SELECT 语法。 它与 INSERT VALUES 略有不同......

INSERT recipient_table (field1, field2)
SELECT field1_from, field2_from
FROM donor_table
WHERE field1_from = 'condition'

If your data is already in the database you could use INSERT SELECT syntax. It's slightly different from INSERT VALUES one...

INSERT recipient_table (field1, field2)
SELECT field1_from, field2_from
FROM donor_table
WHERE field1_from = 'condition'
谁人与我共长歌 2024-07-13 11:14:15

我知道您正在谈论编写存储过程来接受值数组

使用 SQL Server 2005,您需要使用 XML 变量

SQL 2008 添加了对表变量作为参数的支持

在这里您可以找到将表传递给存储过程的好示例,如下所示XML 并作为 表变量 (SQL Server 2008)

I understand that you are talking about writing stored procedure to accept array of values

With SQL Server 2005 you would need to use XML variable

SQL 2008 adds support to table variable as parameters

Here you can find good examples of passing a table to a stored procedure as XML and as table variable (SQL Server 2008)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文