如何在 SQL Server 2005 中插入值数组?
如何编写 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我将列表构造为 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
将值连接到列表并将其传递给 sp 的简单方法。
在 sp 中使用 dbo.Split udf 转换回结果集(表)。
创建这个函数:
然后尝试:
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:
and then try:
如果您的数据已在数据库中,您可以使用
INSERT SELECT
语法。 它与 INSERT VALUES 略有不同......If your data is already in the database you could use
INSERT SELECT
syntax. It's slightly different from INSERT VALUES one...我知道您正在谈论编写存储过程来接受值数组
使用 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)