一个 SQL 查询中的多次插入

发布于 2024-11-27 15:47:02 字数 263 浏览 3 评论 0原文

DB:SQL Server

我正在使用以下构造将多条记录插入到表中。我正在从其他数据库接收数据(要插入)。

插入表1 选择 '1','2' UNION ALL 选择 '3','4' UNION ALL 选择“5”、“6”;

还有其他机会在更短的周转时间内进行插入吗?它也作为网络请求执行。我想批量插入不适合这里,因为我的文件中没有数据来进行批量插入。 有什么建议请..

DB: SQL server

I am using the below construct for inserting multiple records into a table. I am receiving the data (to be inserted) from other DB.

Insert into table1
select '1','2' UNION ALL
select '3','4' UNION ALL
select '5','6';

would there be any other chance in doing inserts in less turn around time. Its also been executed as a web request. I guess bulk insert would not fit here, as I don't have the data in a file to do a bulk insert.
Any suggestions please..

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

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

发布评论

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

评论(4

鹊巢 2024-12-04 15:47:02

如果源数据库也是 SQL Server,则可以添加链接服务器并且:

insert table1 select * from linkedserver.dbname.dbo.table1

如果从 .NET 代码插入,插入多行的最快方法是 SqlBulkCopy。 SqlBulkCopy 确实需要 DBO 权限。

If the source database is also a SQL Server, you could add a linked server and:

insert table1 select * from linkedserver.dbname.dbo.table1

If you're inserting from .NET code, the fastest way to insert multiple rows is SqlBulkCopy. SqlBulkCopy does require DBO rights.

苦行僧 2024-12-04 15:47:02

这实际上是我见过的最好的多重插入。只是要小心 SQL 注入,始终在 ASP.NET 中使用 CommandParameters 或在 MySQL 中使用 mysql_real_escape。

That is actually the best multiple insert I have ever seen. Just be careful to SQL injections, always use CommandParameters in ASP.NET or use mysql_real_escape in MySQL.

星軌x 2024-12-04 15:47:02

我最近对此进行了研究,来自 MySQL,并期待 ularis 答案中的语法,但经过一番搜索后,我所能找到的只是您在答案中发布的语法。

编辑:看起来 ularis 删除了他的答案,他正在谈论 INSERT INTO x VALUES (1, 2), (3, 4); 语法。

I looked into this recently, coming from MySQL and expecting the syntax from cularis' answer, but after some searching all I could find is the syntax you posted in your answer.

Edit: Looks like cularis removed his answer, he was talking about the INSERT INTO x VALUES (1, 2), (3, 4); syntax.

吾性傲以野 2024-12-04 15:47:02

如果您使用 SQL Server 2008 和存储过程,则始终可以使用表值参数:

http://www.sqlteam.com/article/sql-server-2008-table-valued-parameters

然后它变成 INSERT INTO ... SELECT * FROM ...

这将有助于解决注入问题。不确定这是否可以通过参数化 SQL 实现。

If you are using SQL Server 2008 and stored procedures, you could always make use of table valued parameters:

http://www.sqlteam.com/article/sql-server-2008-table-valued-parameters

It then becomes an INSERT INTO ... SELECT * FROM ...

This would help against injection problems. Not sure if this is possible with parameterised SQL.

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