"Invalid object name ..." 错误出现在一台服务器上,但没有出现在另一台服务器上
The "Invalid object name ..." error appears in one server but no in the other one
我有一个有线的情况。下面的代码在一台服务器上运行良好,但在另一台服务器上运行不佳。两台服务器上的数据库兼容性级别相同。我使用 sql 服务器 2016.
;USE MyDB;
GO
--exec MyDB.dbo.sp_Cleanup_Bid5YearData
ALTER PROCEDURE dbo.sp_Cleanup_Bid5YearData
AS
DECLARE @date VARCHAR(10),
@cmdIf NVARCHAR(200),
@cmd NVARCHAR(4000)
SET @date = CAST(YEAR(GETDATE()) AS VARCHAR(4)) + '_' + CAST(MONTH(GETDATE()) AS VARCHAR(2)) + '_' + CAST(DAY(GETDATE()) AS VARCHAR(2))
IF OBJECT_ID('tempdb..#Id_ToBeRemoved') IS NOT NULL --SELECT 1
DROP TABLE #Id_ToBeRemoved;
CREATE TABLE #Id_ToBeRemoved
(
Id INT PRIMARY KEY NOT NULL,
Code CHAR(3) NOT NULL
);
INSERT INTO #Id_ToBeRemoved (Id, Code)
VALUES(12,'TMB'),
(13,'FGR');
IF EXISTS (SELECT TOP 1 1 FROM #Id_ToBeRemoved)
SET @cmd = ('
;USE MyDB;
IF EXISTS (SELECT 1 FROM [MyDB].dbo.Table_5YearData_'+@date+')
DROP TABLE [MyDB].dbo.Table_5YearData_'+@date+';
;USE MyDB;
SELECT
Id,
Code
INTO [MyDB].dbo.Table_5YearData_'+@date+'
FROM #Id_ToBeRemoved;
')
EXEC sp_executesql @cmd;
--EXEC (@cmd);
--select * from [MyDB].dbo.Table_5YearData_2018_4_25 -- you may need to change the ending of the table "2018_4_25" depending on the date you run this statement
我收到的错误消息是:
Msg 208, Level 16, State 1, Line 7
Invalid object name 'bidNotification.dbo.Table_5YearData_2018_4_25'
table bidNotification.dbo.Table_5YearData_2018_4_25 在您的第二个数据库中不存在。如果 table 确实存在,您的查询将检查 table 中是否有行。仅当 table 已经存在时,您的查询才有效。
您可以修改并执行此操作,而不是使用 OBJECT_ID()
来验证 table 是否存在。
SET @cmd = ('
;USE MyDB;
IF OBJECT_ID(N''[MyDB].dbo.Table_5YearData' + @date + ''', N''U'') IS NOT NULL
DROP TABLE [MyDB].dbo.Table_5YearData_'+@date+';
;USE MyDB;
SELECT
Id,
Code
INTO [MyDB].dbo.Table_5YearData_'+@date+'
FROM #Id_ToBeRemoved;
')
我有一个有线的情况。下面的代码在一台服务器上运行良好,但在另一台服务器上运行不佳。两台服务器上的数据库兼容性级别相同。我使用 sql 服务器 2016.
;USE MyDB;
GO
--exec MyDB.dbo.sp_Cleanup_Bid5YearData
ALTER PROCEDURE dbo.sp_Cleanup_Bid5YearData
AS
DECLARE @date VARCHAR(10),
@cmdIf NVARCHAR(200),
@cmd NVARCHAR(4000)
SET @date = CAST(YEAR(GETDATE()) AS VARCHAR(4)) + '_' + CAST(MONTH(GETDATE()) AS VARCHAR(2)) + '_' + CAST(DAY(GETDATE()) AS VARCHAR(2))
IF OBJECT_ID('tempdb..#Id_ToBeRemoved') IS NOT NULL --SELECT 1
DROP TABLE #Id_ToBeRemoved;
CREATE TABLE #Id_ToBeRemoved
(
Id INT PRIMARY KEY NOT NULL,
Code CHAR(3) NOT NULL
);
INSERT INTO #Id_ToBeRemoved (Id, Code)
VALUES(12,'TMB'),
(13,'FGR');
IF EXISTS (SELECT TOP 1 1 FROM #Id_ToBeRemoved)
SET @cmd = ('
;USE MyDB;
IF EXISTS (SELECT 1 FROM [MyDB].dbo.Table_5YearData_'+@date+')
DROP TABLE [MyDB].dbo.Table_5YearData_'+@date+';
;USE MyDB;
SELECT
Id,
Code
INTO [MyDB].dbo.Table_5YearData_'+@date+'
FROM #Id_ToBeRemoved;
')
EXEC sp_executesql @cmd;
--EXEC (@cmd);
--select * from [MyDB].dbo.Table_5YearData_2018_4_25 -- you may need to change the ending of the table "2018_4_25" depending on the date you run this statement
我收到的错误消息是:
Msg 208, Level 16, State 1, Line 7
Invalid object name 'bidNotification.dbo.Table_5YearData_2018_4_25'
table bidNotification.dbo.Table_5YearData_2018_4_25 在您的第二个数据库中不存在。如果 table 确实存在,您的查询将检查 table 中是否有行。仅当 table 已经存在时,您的查询才有效。
您可以修改并执行此操作,而不是使用 OBJECT_ID()
来验证 table 是否存在。
SET @cmd = ('
;USE MyDB;
IF OBJECT_ID(N''[MyDB].dbo.Table_5YearData' + @date + ''', N''U'') IS NOT NULL
DROP TABLE [MyDB].dbo.Table_5YearData_'+@date+';
;USE MyDB;
SELECT
Id,
Code
INTO [MyDB].dbo.Table_5YearData_'+@date+'
FROM #Id_ToBeRemoved;
')