如果我正在检查对象是否存在并且如果存在我将删除它,我不确定为什么我会不断收到对象已经存在的消息。有人有任何线索吗?
错误信息:
消息 2714,级别 16,状态 1,过程 spCreateDirectoryStructure,第 54 行数据库中已经有一个名为“#dirs”的对象。消息 2714,级别 16,状态 1,过程 spCreateDirectoryStructure,第 74 行数据库中已经有一个名为“#dirs”的对象。
存储过程:
CREATE PROCEDURE spCreateDirectoryStructure
AS
BEGIN
SET NOCOUNT ON;
DECLARE @year CHAR(4)
DECLARE @month VARCHAR(2)
DECLARE @day VARCHAR(2)
DECLARE @root VARCHAR(200)
DECLARE @dir VARCHAR(200)
DECLARE @yearDir VARCHAR(200)
DECLARE @monthDir VARCHAR(200)
DECLARE @dayDir VARCHAR(200)
SET @root = 'C:\Test\'
SET @year = DATEPART(YEAR, GETDATE())
SET @month = DATEPART(MONTH, GETDATE())
SET @day = DATEPART(DAY, GETDATE())
SET @yearDir = @root + @year + '\'
SET @monthDir = @root + @year + '\' + @month + '\'
SET @dayDir = @root + @year + '\' + @month + '\' + @day + '\'
-- check root folder for year folder
IF OBJECT_ID(N'tempdb..#dirs') IS NOT NULL
BEGIN
DROP TABLE #dirs
END
CREATE TABLE #dirs (Directory VARCHAR(200))
INSERT INTO #dirs
EXEC master.dbo.xp_subdirs
@root
IF NOT EXISTS ( SELECT Directory
FROM #dirs
WHERE Directory = @year )
EXEC master.sys.xp_create_subdir
@yearDir
-- **********************************************
-- check year folder for month folder
IF OBJECT_ID(N'tempdb..#dirs') IS NOT NULL
BEGIN
DROP TABLE #dirs
END
CREATE TABLE #dirs (Directory VARCHAR(200))
INSERT INTO #dirs
EXEC master.dbo.xp_subdirs
@yearDir
IF NOT EXISTS ( SELECT Directory
FROM #dirs
WHERE Directory = @month )
EXEC master.sys.xp_create_subdir
@monthDir
-- **********************************************
-- check month folder for day folder
IF OBJECT_ID(N'tempdb..#dirs') IS NOT NULL
BEGIN
DROP TABLE #dirs
END
CREATE TABLE #dirs (Directory VARCHAR(200))
INSERT INTO #dirs
EXEC master.dbo.xp_subdirs
@monthDir
IF NOT EXISTS ( SELECT Directory
FROM #dirs
WHERE Directory = @day )
EXEC master.sys.xp_create_subdir
@dayDir
END
GO