我在疯了之前需要你的帮助...
我现在有以下SP,如果我在SP中执行它可以正常工作,但只要我执行sp我就没有结果....
ALTER PROCEDURE [dbo].[BIDASHBOARD_MiningUtilization]
@DST datetime, @DET datetime, @Equipment int
AS
BEGIN
--DECLARE @DST datetime, @DET datetime, @Equipment int
--SET @DET = @DET
--SET @DET = '2014-02-01 00:00:00.000'
--SET @DST = DATEADD(dd,1,@DST)
--SET @DST = '2014-02-28 00:00:00.000'
--SET @Equipment = 8
DROP TABLE #TMP1
CREATE TABLE #TMP1 (DownTime int, DType varchar(50));
INSERT INTO #TMP1
SELECT SUM(UTILSUM.[Minutes])/ 60 AS DOWNTIME, UTILSUM.DTYPE
FROM
(SELECT TOTAL.EquipmentID, TOTAL.DownTimeReasonCategoryID, TOTAL.ReasonNote,TOTAL.SDATE,TOTAL.EDATE, DATEDIFF(Mi,TOTAL.SDATE,TOTAL.EDATE) AS [Minutes],TOTAL.DTYPE
FROM
(SELECT
[EquipmentID]
,[DownStartTime]
,[DownEndTime]
,[DownTimeReasonCategoryID]
,[ReasonNote]
,SDATE =
CASE
WHEN [DownStartTime] BETWEEN @DET AND @DST THEN [DownStartTime]
ELSE @DET
END
,EDATE =
CASE
WHEN [DownEndTime] BETWEEN @DET AND @DST THEN [DownEndTime]
ELSE @DST
END
, DTYPE =
CASE
WHEN [DownTimeReasonCategoryID] IN (2,5,7,9,11) THEN 'Equipment Unavailable'
WHEN [DownTimeReasonCategoryID] IN (1,3,8,12,13) THEN'Equipment Not Utilized'
ELSE 'CAT6'
END
FROM RPZCBIDashboard].[dbo].[Mining_DOWNTIMETEST]
Where DownStartTime < @DST
AND DownEndTime >= @DET
and EquipmentID in (SELECT[EquipmentId]
FROM RPZCBIDashboard].[dbo].[Mining_Equipment]
Where EquipmentTypeId = @Equipment
and IsActive = 1
and EquipmentDownTime = 1)) AS TOTAL) AS UTILSUM
GROUP BY UTILSUM.DTYPE
SELECT #TMP1.DownTime, #TMP1.DType FROM #TMP1 WHERE #TMP1.DType <> 'CAT6'
UNION ALL
SELECT DATEDIFF(HH,@DET,@DST) * (SELECT COUNT([EquipmentId])
FROM RPZCBIDashboard].[dbo].[Mining_Equipment]
Where EquipmentTypeId = @Equipment
and IsActive = 1)-(SELECT SUM(#TMP1.DownTime) FROM #TMP1 WHERE #TMP1.DType <> 'CAT6' ), 'Productive Time'
SELECT @DST
SELECT @DET
SELECT @Equipment
这是我的SP,如果我删除注释部分,我设置变量并运行它去除顶部参数的声明它返回结果没问题。
但是如果我按如下方式执行代码:
DECLARE @return_value int
EXEC @return_value = [dbo].[BIDASHBOARD_MiningUtilization]
@DST = '2014-02-01 00:00:00.000',
@DET = '2014-02-28 00:00:00.000',
@Equipment = 8
SELECT 'Return Value' = @return_value
GO
我只能得到这个......
DownTime DType
NULL Productive Time
和这条消息.. Msg 3701,Level 11,State 5,Procedure BIDASHBOARD_MiningUtilization,Line 21 无法删除表格#TMP1&#39;,因为它不存在或您没有权限。
这是我在Store Procedure中运行查询时得到的结果:
DownTime DType
23 Equipment Not Utilized
1130 Equipment Unavailable
7919 Productive Time
答案 0 :(得分:1)
,请尝试替换此
DROP TABLE #TMP1
用这个
IF OBJECT_ID('tempdb..#TMP1') IS NOT NULL DROP TABLE #TMP1
答案 1 :(得分:0)
喜欢drop table if exist
,而不是直接放弃
if exists (select * from sys.objects where name = '#TMP1')
drop table #TMP1
你的程序中有很多错误,如下所示
ALTER PROCEDURE [dbo].[BIDASHBOARD_MiningUtilization]
@DST datetime, @DET datetime, @Equipment int
AS
BEGIN
DROP TABLE #TMP1 <-- change it as sigested in answer
CREATE TABLE #TMP1 (DownTime int, DType varchar(50));
INSERT INTO #TMP1
SELECT SUM(UTILSUM.[Minutes])/ 60 AS DOWNTIME, UTILSUM.DTYPE
FROM
(SELECT DATEDIFF(Mi,TOTAL.SDATE,TOTAL.EDATE) AS [Minutes],TOTAL.DTYPE <-- 2, no need of feching extra columns
FROM
(SELECT
[EquipmentID]
,[DownStartTime]
,[DownEndTime]
,[DownTimeReasonCategoryID]
,[ReasonNote]
,SDATE =
CASE
WHEN [DownStartTime] BETWEEN @DET AND @DST THEN [DownStartTime]
ELSE @DET
END
,EDATE =
CASE
WHEN [DownEndTime] BETWEEN @DET AND @DST THEN [DownEndTime]
ELSE @DST
END
, DTYPE =
CASE
WHEN [DownTimeReasonCategoryID] IN (2,5,7,9,11) THEN 'Equipment Unavailable'
WHEN [DownTimeReasonCategoryID] IN (1,3,8,12,13) THEN'Equipment Not Utilized'
ELSE 'CAT6'
END
FROM [RPZCBIDashboard].[dbo].[Mining_DOWNTIMETEST] <-- 3, missing '['
Where DownStartTime < @DST
AND DownEndTime >= @DET
and EquipmentID in (SELECT [EquipmentId] <-- 4, missing a space after select
FROM [RPZCBIDashboard].[dbo].[Mining_Equipment] <-- 5, missing '['
Where EquipmentTypeId = @Equipment
and IsActive = 1
and EquipmentDownTime = 1)) AS TOTAL) AS UTILSUM
GROUP BY UTILSUM.DTYPE
SELECT #TMP1.DownTime, #TMP1.DType FROM #TMP1 WHERE #TMP1.DType <> 'CAT6'
UNION ALL
SELECT DATEDIFF(HH,@DET,@DST) * (SELECT COUNT([EquipmentId]) <-- 6, remove SELECT from SELECT COUNT([EquipmentId]
FROM RPZCBIDashboard].[dbo].[Mining_Equipment]
Where EquipmentTypeId = @Equipment
and IsActive = 1)-(SELECT SUM(#TMP1.DownTime) FROM #TMP1 WHERE #TMP1.DType <> 'CAT6' ), 'Productive Time'
答案 2 :(得分:0)
我已经解决了这个问题:
EXEC @return_value = [dbo].[BIDASHBOARD_MiningUtilization]
@DET = '2014-02-01 00:00:00.000',
@DST = '2014-02-28 00:00:00.000',
@Equipment = 8
日期范围错误。