如何在不使用 bcp 的情况下将数据批量插入 SQL Server?

发布于 2024-10-31 06:41:14 字数 529 浏览 6 评论 0原文

我有 SQL Server 2005 DB,其中有许多表格,

Table (id <Primary Key, Identity column>, name <VarChar (255)>)

我有一个 Java Servlet,需要能够向这些表批量添加信息。 SQL Server DB 与 Servlet 运行在不同的计算机上,并且我无权访问该计算机的文件系统。

我的理解是,我不能使用批量插入,因为它要求数据文件位于服务器的文件系统上(我做不到),它要求你填写所有字段(做不到,一个字段是由数据库填写的身份字段)或将未填写的字段放在最后一个(我将身份/id字段放在第一位,并且不想更改它),并且要求格式文件位于服务器上文件系统。

在 MySQL 中,我只需使用 Load Data Local InFile。 SQL Server 2005 似乎缺乏这样的功能。我只是错过了什么吗?有没有办法让我一次向 SQL Server 表添加多条记录而不使用 bcp 或批量插入?

格雷格·

蒂亚

I have SQL Server 2005 DB with a number of tables of the form

Table (id <Primary Key, Identity column>, name <VarChar (255)>)

I have a Java Servlet that needs to be able to bulk add information to those tables. The SQL Server DB is running on a different machine than the Servlet, and I do not have access to the file system of that machine.

My understanding is that I can't use Bulk Insert because it requires the data file to be on the server's file system (which I can't do), it requires you to fill in all fields (can't do, one field is an identity field that is filled in by the DB) or to have the unfilled field be the last one (I put the identity / id field first, and have no desire to change that), and requires the format file to be on the Server's file system.

In MySQL, I'd simply use Load Data Local InFile. SQL Server 2005 appears to lack such a capability. Am I just missing something? Is there any way for me to add more than one record at a time to a SQL Server table w/o using bcp or Bulk Insert?

TIA,

Greg

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

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

发布评论

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

评论(2

枕花眠 2024-11-07 06:41:14
INSERT INTO Table (column_name, ...)
Values
(column_values,...),(column_values,...),(column_values, ...), ...

请注意,这对于 SQL Server 2008

http:// /msdn.microsoft.com/en-us/library/ms174335%28v=SQL.100%29.aspx

对于 SQL Server 2005 及更低版本,您需要执行类似以下操作

INSERT INTO table (column_name, column_name, ...)
SELECT column_value , column_value, ...
UNION ALL
SELECT column_value , column_value, ...
UNION ALL
SELECT column_value , column_value, ...
UNION ALL
SELECT column_value , column_value, ...
UNION ALL
SELECT column_value , column_value, ...
INSERT INTO Table (column_name, ...)
Values
(column_values,...),(column_values,...),(column_values, ...), ...

Note this is possible with SQL Server 2008

http://msdn.microsoft.com/en-us/library/ms174335%28v=SQL.100%29.aspx

For SQL Server 2005 and Under you would need to do something like this

INSERT INTO table (column_name, column_name, ...)
SELECT column_value , column_value, ...
UNION ALL
SELECT column_value , column_value, ...
UNION ALL
SELECT column_value , column_value, ...
UNION ALL
SELECT column_value , column_value, ...
UNION ALL
SELECT column_value , column_value, ...
一袭白衣梦中忆 2024-11-07 06:41:14

那么您既无法访问 servlet 计算机上的文件系统,也无法访问 SQL Server? (我也不知道如何在 MySQL 中执行此操作,因为 LOAD DATA LOCAL INFILE 需要一个文件)。

ADO.NET 支持 SqlBulkCopy API:http://msdn .microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.aspx

So you have neither access to the filesystem on the servlet machine nor the SQL Server? (I'm not sure how you would do this in MySQL either, since LOAD DATA LOCAL INFILE needs a file).

ADO.NET supports a SqlBulkCopy API: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.aspx

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