简体   繁体   English

我可以在没有往返的情况下将Select语句的结果插入另一个表吗?

[英]Can I Insert the Results of a Select Statement Into Another Table Without a Roundtrip?

I have a web application that is written in MVC.Net using C# and LINQ-to-SQL (SQL Server 2008 R2). 我有一个使用C#和LINQ-to-SQL(SQL Server 2008 R2)用MVC.Net编写的Web应用程序。

I'd like to query the database for some values, and also insert those values into another table for later use. 我想在数据库中查询一些值,并将这些值插入另一个表中以备后用。 Obviously, I could do a normal select, then take those results and do a normal insert, but that will result in my application sending the values back to the SQL server, which is a waste as the server is where the values came from. 显然,我可以执行常规选择,然后获取这些结果并进行常规插入,但这将导致我的应用程序将值发送回SQL Server,这是浪费的,因为服务器是值的来源。

Is there any way I can get the select results in my application and insert them into another table without the information making a roundtrip from the the SQL server to my application and back again? 有什么方法可以在我的应用程序中获得选择结果并将其插入到另一个表中,而无需信息往返SQL Server和我的应用程序?

It would be cool if this was in one query, but that's less important than avoiding the roundtrip. 如果在一个查询中,这会很酷,但是这比避免往返没有那么重要。

Assume whatever basic schema you like, I'll be extrapolating your simple example to a much more complex query. 假设您喜欢任何基本模式,我都会将您的简单示例推断为一个更复杂的查询。

Can I Insert the Results of a Select Statement Into Another Table Without a Roundtrip? 我可以在没有往返的情况下将Select语句的结果插入另一个表吗?

From a "single-query" and/or "avoid the round-trip" perspective: Yes. 从“单个查询”和/或“避免往返”的角度来看:是。

From a "doing that purely in Linq to SQL" perspective: Well...mostly ;-). 从“纯粹在Linq到SQL中做”的角度来看:好吧...主要是;-)。

The three pieces required are: 所需的三个部分是:

  • The INSERT...SELECT construct: INSERT...SELECT构造:
    By using this we get half of the goal in that we have selected data and inserted it. 通过使用此选项,我们已达到目标的一半,因为我们选择了数据并将其插入。 And this is the only way to keep the data entirely at the database server and avoid the round-trip. 这是将数据完全保留在数据库服务器上并避免往返的唯一方法。 Unfortunately, this construct is not supported by Linq-to-SQL (or Entity Framework): Insert/Select with Linq-To-SQL 不幸的是,Linq-to-SQL(或实体框架)不支持此构造: 使用Linq-To-SQL进行插入/选择

  • The T-SQL OUTPUT clause: T-SQL OUTPUT子句:
    This allows for doing what is essentially the tee command in Unix shell scripting: save and display the incoming rows at the same time . 这允许做什么,本质上是在tee中的Unix shell脚本命令:保存,并 在同一时间显示传入行。 The OUTPUT clause just takes the set of inserted rows and sends it back to the caller, providing the other half of the goal. OUTPUT子句只接受插入的行集,然后将其发送回调用方,从而提供目标的另一半。 Unfortunately, this is also not supported by Linq-to-SQL (or Entity Framework). 不幸的是,Linq-to-SQL(或实体框架)也不支持此功能。 Now, this type of operation can also be achieved across multiple queries when not using OUTPUT , but there is really nothing gained since you then either need to a) create a temp table to dump the initial results into that will be used to insert into the table and then selected back to the caller, or b) have some way of knowing which rows that were just inserted into the table are new so that they can be properly selected back to the caller. 现在,当不使用OUTPUT ,也可以在多个查询中实现这种类型的操作,但是实际上并没有获得任何收益,因为您要么需要a)创建一个临时表以将初始结果转储到该表中,然后将其用于插入到表格,然后选择返回给调用者,或b)通过某种方式知道刚刚插入表中的哪些行是新行,以便可以将它们正确地选择回给调用者。

  • The DataContext.ExecuteQuery<TResult> (String, Object[]) method: DataContext.ExecuteQuery <TResult>(字符串,对象[])方法:
    This is needed due to the two required T-SQL pieces not being supported directly in Linq-to-SQL. 之所以需要这样做是因为Linq-to-SQL不直接支持两个必需的T-SQL。 And even if the clunky approach to avoiding the OUTPUT clause is done (assuming it could be done in pure Linq/Lambda expressions), there is still no way around the INSERT...SELECT construct that would not be a round-trip. 即使完成了避免OUTPUT子句的笨拙方法(假定可以在纯Linq / Lambda表达式中完成),仍然无法绕过INSERT...SELECT构造。

Hence, multiple queries that are all pure Linq/Lambda expressions equates to a round-trip. 因此,全部都是纯Linq / Lambda表达式的多个查询等同于往返。

The only way to truly avoid the round-trip should be something like: 真正避免往返的唯一方法应该是:

var _MyStuff = db.ExecuteQuery<Stuffs>(@"
    INSERT INTO dbo.Table1 (Col1, Col2, Col2)
    OUTPUT INSERTED.*
       SELECT Col1, Col2, Col3
       FROM   dbo.Table2 t2
       WHERE  t2.Col4 = {0};",
       _SomeID);

And just in case it helps anyone (since I already spent the time looking it up :), the equivalent command for Entity Framework is: Database.SqlQuery<TElement> (String, Object[]) 并且以防万一,它对任何人都有帮助(因为我已经花了很多时间查找它:),因此,Entity Framework的等效命令是: Database.SqlQuery <TElement>(String,Object [])

根据您的要求尝试此查询

insert into IndentProcessDetails (DemandId,DemandMasterId,DemandQty) ( select DemandId,DemandMasterId,DemandQty from DemandDetails)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从表中删除一行并将其通过SQL语句插入另一行? - How can I remove one row from a table and insert it into another via a SQL statement? 当我使用 PingAsync 同时 ping 到多个 IP 时,如何获得 ping 结果(如往返时间和 ping 状态)? - How can I get the ping results (like roundtrip time and ping status) when I ping to multiple IP's at the same time with PingAsync? 从表中选择,然后将(值+ 1)插入另一个表中? - Select from table, then insert that (value + 1) into another table? 用一个按钮选择一个数据库表,然后用另一个按钮插入数据库表 - Select a database table with a button and insert into it with another LINQ Group结果并从另一个表中选择 - LINQ Group results and select from another table 将SELECT查询的结果保存到另一个表 - Saving results from SELECT query to another table 使用INSERT INTO…SELECT将一个表的值添加到另一个表中 - Adding values of one table into another with INSERT INTO … SELECT 我尝试使用 c# (winforms) 在 oracle 数据库中写一些东西,我只能让 SELECT 语句起作用,而不是 INSERT 语句 - I try to write something in a oracle database with c# (winforms) and I can only get the SELECT Statement to work, not the INSERT Statement 在没有往返的情况下更新实体框架6 - Update in Entity Framework 6 without a roundtrip 使用 Fluent.NHibernate,如何基于 Select 语句执行 Insert - Using Fluent.NHibernate, how can I do an Insert based on a Select statement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM