为什么这个存储过程使用TOP 100 PERCENT?

时间:2015-12-10 15:18:00

标签: sql-server tsql stored-procedures sql-server-2008-r2

我在sql server 2008 R2中的第三方应用程序中有以下存储过程: -

ALTER PROCEDURE [dbo].[GetContacts] 
AS
BEGIN

---------
SELECT     TOP (100) PERCENT  .....
INTO            [#temp200]
FROM         dbo.Contact 

ORDER BY dbo.Contact.Name

--SELECT * from #temp200

SELECT  top 38   *
FROM         #temp200
ORDER BY Fullname

delete top (38) FROM         #temp200

SELECT  top 38   *
FROM         #temp200
ORDER BY Fullname

delete top (38) FROM         #temp200 

SELECT  top 38   *
FROM         #temp200
ORDER BY Fullname

delete top (38) FROM         #temp200 

SELECT   *
FROM         #temp200
ORDER BY Fullname

现在我在sql management studio中运行这个,我得到了以下结果标签: -

  1. 第一个包含38条记录。

  2. 第二个38记录。

  3. 第三个包含38条记录。

  4. 第四个包含30条记录。

  5. 在这种情况下,我有144条记录,所以不确定是什么目的 (SELECT TOP (100)),因为我将获得144条记录。现在作为测试,我将Select TOP(100)更改为Select TOP(35),在这种情况下我获得了2个结果;第一个有38个记录而第二个有17个记录..所以有人可以建议我的上述SP是如何工作的吗?

1 个答案:

答案 0 :(得分:2)

那家伙不明白表没有订单。他试图以有序的方式插入临时表。这是不可能的。 msdeploy.exe -source:package='G:\workspace\webPackage\WebPackage.zip' -dest:auto,computerName="HOST",includeAcls="False" -verb:sync -disableLink:AppPoolExtension -disableLink:ContentExtension -disableLink:CertificateExtension -setParamFile:"G:\workspace\webPackage\SetParameters.xml" Error: (12/10/2015 2:55:36 PM) An error occurred when the request was processed on the remote computer. Error: An item with the same key has already been added. at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) at Microsoft.Web.Deployment.DeploymentManager.SanitizePath(DeploymentProviderOptions providerOptions) at Microsoft.Web.Deployment.DeploymentAgentWorkerRequest.GetTraceMessage(String[] additionalMessage) at Microsoft.Web.Deployment.DeploymentAgent.HandleSync(DeploymentAgentAsyncData asyncData, Nullable`1 passId) at Microsoft.Web.Deployment.DeploymentAgent.HandleRequestWorker(DeploymentAgentAsyncData asyncData) at Microsoft.Web.Deployment.DeploymentAgent.HandleRequest(DeploymentAgentAsyncData asyncData) at Microsoft.Web.Deployment.DeploymentAgent.BeginProcessRequest(DeploymentAgentWorkerRequest workerRequest, AsyncCallback callback, Object extraData) 技巧会关闭此警告,但不会确保顺序。

在早期的SQL Server版本中,这段代码很可能巧合。从那时起,已经添加了更多优化,并且此代码非常脆弱。如果你有机会重写这个。这是潜在的定时炸弹。