SQL 2008存储过程每运行一次就会成功运行

时间:2013-02-06 15:45:59

标签: sql-server-2008

好的,我已经被困在这一个上一天半,现在没有在网站上解决。

我正在运行SQL Server 2008标准版(64位) 有问题的存储过程位于本文的底部

我有一个存储过程,只有在我执行它时才会成功运行。基本上这个SP所做的是从几个表(同一本地服务器上的两个表,两个不同的数据库)中获取一些值,并将这些值插入第三个表中。成功执行时,作为insert命令目标的表会附加一个新行。如果没有成功执行,我会收到以下错误:

Msg 0, Level 11, State 0, Line 0
A severe error occurred on the current command.  The results, if any, should be discarded.
Msg 0, Level 20, State 0, Line 0
A severe error occurred on the current command.  The results, if any, should be discarded.

当我右键单击并直接执行SP或从代理作业运行它时,问题就发生了(代理作业,如果我将其设置为重试,则总是会在重试时成功)。当"每隔一次执行"问题的本质是非常可重复的。

我插入的表确实有一个PrimaryKey,我不会在SP中寻址,因为我让SQL处理递增该值。

这是SP(实际上它是这个SP的Alter Procedure输出),有什么想法吗?

USE [Database1]
GO
/****** Object:  StoredProcedure [dbo].[sp210_Daily_Summary_21006]    Script Date: 02/06/2013 10:26:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER Procedure [dbo].[sp210_Daily_Summary_21006]

/*** 21006 ***/
@Date datetime = NULL,
@Shop_Order int = NULL,
@Lot_Number nvarchar(16) = NULL,
@Part_Number nvarchar(16) = NULL,
@Balance_Left int = NULL,
@Day_Shift_OEE real = NULL,
@Day_Shift_Good int = NULL,
@Day_Shift_Supervisor_Notes nvarchar(256) = NULL,
@Day_Shift_Tech_Notes nvarchar(256) = NULL,
@Day_Shift_Repair_Log int = NULL,
@Day_Shift_QA_Notes nvarchar(256) = NULL,
@Day_Shift_NCMR_QIF int = NULL,
@Night_Shift_OEE real = NULL,
@Night_Shift_Good int = NULL,
@Night_Shift_Supervisor_Notes nvarchar(256) = NULL,
@Night_Shift_Tech_Notes nvarchar(256) = NULL,
@Night_Shift_Repair_Log int = NULL,
@Night_Shift_QA_Notes nvarchar(256) = NULL,
@Night_Shift_NCMR_QIF int = NULL

AS
BEGIN

Set NoCount On;

Select
@Date = GETDATE(),
@Shop_Order = (Select SHOP_ORDER_NUMBER from dbo.CurrentJobSetupsAutomation WHERE (WORK_CENTER = 21006)),
@Lot_Number = (Select LOT_NUMBER from dbo.CurrentJobSetupsAutomation WHERE (WORK_CENTER = 21006)),
@Part_Number = (Select ITEM_NUMBER from dbo.CurrentJobSetupsAutomation WHERE (WORK_CENTER = 21006)),
@Balance_Left = (Select BalanceDue from dbo.CurrentJobSetupsAutomation WHERE (WORK_CENTER = 21006)),

--- Day Shift Good (7:00 AM to 7:00 PM previous day) when report runs at 6:30 AM
@Day_Shift_Good = (SELECT sum(Actual)
FROM Database2.dbo.BPAQualityLog
where AssetID = 3 
and SubQualityName = 21006
and DATEADD(HH,DATEDIFF(hour,getutcdate(),getdate()),RecordUpdated) >= DATEADD(HH,-17, DATEADD(DD,0, DATEDIFF(DD, 0, GETDATE())))
and DATEADD(HH,DATEDIFF(hour,getutcdate(),getdate()),RecordUpdated) <= DATEADD(HH,-5, DATEADD(DD,0, DATEDIFF(DD, 0, GETDATE())))
),

@Day_Shift_Supervisor_Notes = (Select Day_Shift_Supervisor_Notes from dbo.[210_Daily_Temp] WHERE (WORK_CENTER = 21006)),
@Day_Shift_Tech_Notes = (Select Day_Shift_Tech_Notes from dbo.[210_Daily_Temp] WHERE (WORK_CENTER = 21006)),
@Day_Shift_Repair_Log = (Select Day_Shift_Repair_Log from dbo.[210_Daily_Temp] WHERE (WORK_CENTER = 21006)),
@Day_Shift_QA_Notes = (Select Day_Shift_QA_Notes from dbo.[210_Daily_Temp] WHERE (WORK_CENTER = 21006)),
@Day_Shift_NCMR_QIF = (Select Day_Shift_NCMR_QIF from dbo.[210_Daily_Temp] WHERE (WORK_CENTER = 21006)),

--- Night Shift Good (7:00 PM to 7:00 AM current day) when report runs at 6:30 AM
@Night_Shift_Good = (SELECT sum(Actual)
FROM Database2.dbo.BPAQualityLog
where AssetID = 3 
and SubQualityName = 21006
and DATEADD(HH,DATEDIFF(hour,getutcdate(),getdate()),RecordUpdated) >= DATEADD(HH,-5, DATEADD(DD,0, DATEDIFF(DD, 0, GETDATE())))
and DATEADD(HH,DATEDIFF(hour,getutcdate(),getdate()),RecordUpdated) <= DATEADD(HH,7, DATEADD(DD,0, DATEDIFF(DD, 0, GETDATE())))
),

@Night_Shift_Supervisor_Notes = (Select Night_Shift_Supervisor_Notes from dbo.[210_Daily_Temp] WHERE (WORK_CENTER = 21006)),
@Night_Shift_Tech_Notes = (Select Night_Shift_Tech_Notes from dbo.[210_Daily_Temp] WHERE (WORK_CENTER = 21006)),
@Night_Shift_Repair_Log = (Select Night_Shift_Repair_Log from dbo.[210_Daily_Temp] WHERE (WORK_CENTER = 21006)),
@Night_Shift_QA_Notes = (Select Night_Shift_QA_Notes from dbo.[210_Daily_Temp] WHERE (WORK_CENTER = 21006)),
@Night_Shift_NCMR_QIF = (Select Night_Shift_NCMR_QIF from dbo.[210_Daily_Temp] WHERE (WORK_CENTER = 21006))

Set NoCount Off;

insert into [Database1].[dbo].[210_Daily_Summary]
([Date], 
[Work_Center], 
[Shop_Order], 
[Lot_Number], 
[Part_Number], 
[Balance_Left],
[Day_Shift_Good],
[Day_Shift_Supervisor_Notes],
[Day_Shift_Tech_Notes],
[Day_Shift_Repair_Log],
[Day_Shift_QA_Notes],
[Day_Shift_NCMR_QIF],
[Night_Shift_Good],
[Night_Shift_Supervisor_Notes],
[Night_Shift_Tech_Notes],
[Night_Shift_Repair_Log],
[Night_Shift_QA_Notes],
[Night_Shift_NCMR_QIF]
)

values
(@Date,
'21006', 
@Shop_Order,
@Lot_Number,
@Part_Number,
@Balance_Left,
@Day_Shift_Good,
@Day_Shift_Supervisor_Notes,
@Day_Shift_Tech_Notes,
@Day_Shift_Repair_Log,
@Day_Shift_QA_Notes,
@Day_Shift_NCMR_QIF,
@Night_Shift_Good,
@Night_Shift_Supervisor_Notes,
@Night_Shift_Tech_Notes,
@Night_Shift_Repair_Log,
@Night_Shift_QA_Notes,
@Night_Shift_NCMR_QIF
)

END

这是我用来创建我插入的表的查询:

USE [Database1]
GO

/****** Object:  Table [dbo].[210_Daily_Summary]    Script Date: 02/06/2013 10:28:34 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[210_Daily_Summary](
[PrimaryKey] [int] IDENTITY(1,1) NOT NULL,
[Date] [datetime] NULL,
[Work_Center] [nvarchar](16) NULL,
[Shop_Order] [int] NULL,
[Lot_Number] [nvarchar](16) NULL,
[Part_Number] [nvarchar](16) NULL,
[Balance_Left] [int] NULL,
[Day_Shift_OEE] [real] NULL,
[Day_Shift_Good] [int] NULL,
[Day_Shift_Supervisor_Notes] [nvarchar](256) NULL,
[Day_Shift_Tech_Notes] [nvarchar](256) NULL,
[Day_Shift_Repair_Log] [int] NULL,
[Day_Shift_QA_Notes] [nvarchar](256) NULL,
[Day_Shift_NCMR_QIF] [int] NULL,
[Night_Shift_OEE] [real] NULL,
[Night_Shift_Good] [int] NULL,
[Night_Shift_Supervisor_Notes] [nvarchar](256) NULL,
[Night_Shift_Tech_Notes] [nvarchar](256) NULL,
[Night_Shift_Repair_Log] [int] NULL,
[Night_Shift_QA_Notes] [nvarchar](256) NULL,
[Night_Shift_NCMR_QIF] [int] NULL
) ON [PRIMARY]

GO

1 个答案:

答案 0 :(得分:0)

好的,首先让我们确保您没有损坏。运行以下命令并告诉我们结果:

DBCC CHECKDB (<Database Name>) WITH NO_INFOMSGS, ALL_ERRORMSGS

接下来,您要检查SQL Server错误日志,以查看服务器是否同时记录了任何其他错误。