已经是数据库问题中指定的对象

时间:2015-08-20 12:34:41

标签: sql-server tsql

我正在尝试执行包含存储过程结果的电子邮件。我正在查看stackoverflow中的其他帖子,但似乎无法通过错误指出"消息2714,级别16,状态1,过程CompareConfirm_FraudRules,第38行 已有一个名为' ## returnInactiveRules'在数据库中。"我已经查看了数据库,并且没有此名称存在的对象。任何有关如何解决此问题的建议将不胜感激。感谢。

这是我的SP:

BEGIN

CREATE TABLE ##returnInactiveRules (
    ProductName varchar(100),
    ChannelName varchar(100),
    StrategyCode varchar(100),
    StrategyName varchar(100),
    RuleCode varchar(100),
    RuleName varchar(100),
    On_Off varchar(5)
);

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

-- Insert statements for procedure here
-- SELECT R.RuleCode, R.Name as RuleName, S.StrategyCode, S.Name as StrategyName, R.IsActive
SELECT DISTINCT
    CASE
    WHEN PC.ProductIdentifier='2000' THEN 'GP'
    WHEN PC.ProductIdentifier='1000' THEN 'MB'
    END as ProductName, C.Name as ChannelName, S.StrategyCode, S.Name as StrategyName, R.RuleCode, R.Name as RuleName, 
    CASE
    WHEN R.IsActive = 1 THEN 'On'
    WHEN R.IsActive = 0 THEN 'Off'
    END as On_Off
INTO ##returnInactiveRules
FROM dbo.[Rule] R
INNER JOIN dbo.Strategy S  
on S.KnockoutRuleSet = R.KnockoutRuleSet 
INNER JOIN dbo.RFAI P 
on R.RFAIId = P.RFAIId
INNER JOIN dbo.DecisionStatus D 
on D. StatusId = R. StatusId
LEFT OUTER JOIN dbo.NOAA N 
on N.NOAAId = R.NOAAId
INNER JOIN dbo.RuleQuestionsXRef Q 
ON Q.RuleId = R.RuleId
INNER JOIN ProductChannelStrategyRuleXref X 
ON X.RuleId = R.RuleId 
INNER JOIN ProductChannelStrategy CS 
ON CS.ProductChannelStrategyId = X.ProductChannelStrategyId
INNER JOIN ProductChannel PC
ON PC.ProductChannelId = CS.ProductChannelId
INNER JOIN dbo.Channel C
ON C.ChannelId = PC.ChannelId
WHERE R.IsActive = 0
AND R.RuleCode IN ('F06',
'F07',
'F11',
'F12',
'F14',
'F15',
'F16',
'F17',
'F19',
'F23',
'F25',
'F26',
'F10'
)  
-- ORDER BY R.RuleCode, R.Name;
ORDER BY ProductName, C.Name, S.StrategyCode, S.Name, R.RuleCode, R.Name;

-- SELECT * FROM @returnValue;

-- Email the results in the @returnValue table -- 
    EXEC msdb.dbo.sp_send_dbmail
    @execute_query_database='Prod-XXX',
    @recipients=N'msavoy@xxx.com',
    @body='Attached please find a file with the results.', 
    @subject ='Compare Fraud Rule Results',
    @profile_name ='Reports',
    @query ='EXEC CompareConfirm_Rules',
    @attach_query_result_as_file = 1,
    @query_attachment_filename ='CompareRuleResults.txt'
END
DROP TABLE ##returnInactiveRules;
GO

3 个答案:

答案 0 :(得分:3)

如果存在删除它,然后创建表

IF Object_id('tempdb..##returnInactiveRules') IS NOT NULL
  DROP TABLE ##returnInactiveRules

CREATE TABLE ##returnInactiveRules
  (
     ProductName  VARCHAR(100),
     ChannelName  VARCHAR(100),
     StrategyCode VARCHAR(100),
     StrategyName VARCHAR(100),
     RuleCode     VARCHAR(100),
     RuleName     VARCHAR(100),
     On_Off       VARCHAR(5)
  ); 

答案 1 :(得分:2)

##开头的临时表是全局临时表,因此如果您打开了2个连接,并且其中一个创建了一个名为##Temp的表,则您将无法创建相同的表从连接2开始,直到它被2个连接中的任何一个连接掉。

最好的办法是使用#returnInactiveRules并在创建之前检查是否存在。

IF OBJECT_ID('tempdb..#returnInactiveRules') IS NOT NULL 
   DROP TABLE #returnInactiveRules
GO

答案 2 :(得分:0)

在开头有drop语句,如果它是一个全局临时表,确保在任何时候只有一个进程创建它,因为两个进程不能同时存在..

此外,除非您需要其他会话来访问数据,否则请考虑使用临时表而不是全局临时表,将##替换为#