1

当我通过 SQL Server Management Studio 2005 执行第一个和第二个查询时,它们可以正常工作并修改正确的数据。但是,当我尝试执行整个 SP 时,我没有收到任何错误,但只有 FIRST 实际修改了它应该修改的数据,而 SECOND 没有。我研究了这篇 Stackoverflow 文章,并认为我的查询中可能也存在混淆,但我不确定如何解决它,因为我不是简单地声明一个变量

笔记:

*这与完全正常工作的代码之间的唯一区别是在 FIRST 和 SECOND 中使用了 LEFT 命令。

*虽然第一个查询在 <1 中执行,但第二个查询需要一分钟多一点。我不确定这是否会导致问题。

*当我执行整个 SP 时,它在 <1 中执行,这表明它正在跳过 SECOND,但我不知道为什么。

*不返回错误

USE [abc]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[bca]
    -- Add the parameters for the stored procedure here

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.


------------------------FIRST------------------------
UPDATE p
SET DATAREVISIONTYPE = 'Transfer'
FROM cba as p
WHERE DATAREVISIONTYPE  = 'Delete' AND
(SELECT COUNT([OriginalUserId]) FROM cba WHERE DataRevisionType = 'Add' AND FirstName = p.FirstName 
AND LastName = p.LastName AND EmployeeEmailAddress = p.EmployeeEmailAddress AND EmployeeBirthdate = LEFT(p.EmployeeBirthdate, 4))> 0

------------------------SECOND-----------------------
UPDATE O
SET DATAREVISIONTYPE = 'Modify'
FROM cba as O
WHERE DATAREVISIONTYPE = 'Add' AND
(SELECT COUNT([OriginalUserId]) FROM cba WHERE DataRevisionType = 'Transfer' AND FirstName = O.FirstName 
AND LastName = O.LastName AND EmployeeEmailAddress = O.EmployeeEmailAddress AND LEFT(EmployeeBirthdate, 4) = O.EmployeeBirthdate)> 0

END

编辑:没有 LEFT 命令的原始代码

USE [abc]
GO
/****** Object:  StoredProcedure [dbo].[bca]    Script Date: 07/02/2013 10:14:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[bca]
    -- Add the parameters for the stored procedure here

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.

UPDATE p
SET DATAREVISIONTYPE = 'Transfer'
FROM cba as p
WHERE DATAREVISIONTYPE  = 'Delete' AND
(SELECT COUNT([OriginalUserId]) FROM cba WHERE DataRevisionType = 'Add' AND FirstName = p.FirstName 
AND LastName = p.LastName AND EmployeeEmailAddress = p.EmployeeEmailAddress AND EmployeeBirthdate = p.EmployeeBirthdate)> 0

UPDATE O
SET DATAREVISIONTYPE = 'Modify'
FROM cba as O
WHERE DATAREVISIONTYPE = 'Add' AND
(SELECT COUNT([OriginalUserId]) FROM cba WHERE DataRevisionType = 'Transfer' AND FirstName = O.FirstName 
AND LastName = O.LastName AND EmployeeEmailAddress = O.EmployeeEmailAddress AND EmployeeBirthdate = O.EmployeeBirthdate)> 0


END
4

1 回答 1

2

查看您的代码,我认为 LEFT 功能导致不返回任何行。您正在有效地加入 LEFT(p.EmployeeBirthdate, 4)(示例 '0206')和 Birthdate(示例 '0206A')。子查询没有返回任何行,因此没有执行更新。

于 2013-07-10T16:57:20.307 回答