当我通过SQL Server Management Studio 2005执行FIRST和SECOND QUERY时,它们可以正常工作并修改正确的数据。但是,当我尝试执行整个SP时,我没有错误,但只有FIRST实际上修改了它应该的数据而SECOND没有。 我研究了THIS Stackoverflow article并认为我的查询中也许存在混淆,但我不确定如何修复它,因为我不是简单地声明变量
注意:
*这与完全正常的代码之间的唯一区别是在FIRST和SECOND中使用LEFT命令。
*当FIRST查询在< 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
答案 0 :(得分:2)
查看您的代码,我认为LEFT功能导致不返回任何行。您实际上正在使用Birthdate(样本'0206A')加入LEFT(p.EmployeeBirthdate,4)(样本'0206')。子查询没有返回任何行,因此不执行任何更新。