在 SQL 中克隆行的最快方法 [英] Quickest way to clone row in SQL

查看:31
本文介绍了在 SQL 中克隆行的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想克隆具有单个主键 (PrimKey) 的多个表的行.在 SQL Server 2005 中克隆一行的最快方法是什么?

I want to clone multiple tables' rows that have a single primary key (PrimKey). What's the quickest way to clone a row in SQL Server 2005?

举个例子

克隆 PrimKey1 以获取 PrimKey2.所以我尝试以下:

Clone PrimKey1 to get PrimKey2. So I try the following :

INSERT INTO PrimKeys 
SELECT 'PrimKey2' AS PrimKey,* 
  FROM PrimKeys 
 WHERE PrimKey='PrimKey1'

当然这里的问题是,列 PrimKey 在内部 SELECT 语句中重复.有没有其他方法与上面的简单类似?

But of course the issue here is, the column PrimKey gets repeated in the inner SELECT statement. Is there any other way similar in simplicity to the above?

感谢大家的回复.我继续写了一个函数来处理我的应用程序.我不使用存储过程或临时表,所以我无法使用你们中的一些人发布的几个有效答案.

Thank you all for your responses. I went ahead and wrote a function that handles that in my application. I don't use Stored Procs or Temp tables so I couldn't use a couple of valid answers posted by some of you.

推荐答案

如果您从多个表中克隆,最快的方法是使用存储过程,这样所有查询都保留在数据库中,而您不需要't 为客户端和服务器之间的通信付出代价.

If you are cloning from multiple tables, the fastest way will be to use a stored procedure, so that all the queries stay on the database, and you don't pay a price for communication between the client and the server.

一旦你这样做了,然后开始单元测试,看看做这个操作需要多长时间.然后根据你得到的建议开始尝试改变它,看看你是否有任何改进.

Once you do that, then start unit-testing, to see how long it takes to do the operation. Then begin experimenting with changing it, based on suggestions you get, and see if you get any improvement.

这篇关于在 SQL 中克隆行的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆