所以我期望的是第一段代码找到表名,然后如果该表名存在并且超过 3 天,则删除该表。
我对这段代码的问题是代码没有替换@temp_name
为实际的表 DrinkSales。所以变量没有在 select 语句中正确设置。
当前代码:
declare @table varchar(100) = 'DrinkSales'
DECLARE @temp_name VARCHAR(100)
declare @drop varchar(max) = '
DECLARE @temp_name VARCHAR(100)
select @temp_name= name
FROM sys.objects
WHERE DATEDIFF(day, create_date, getdate()) > 3
and name = '''+@table+'''
select @temp_name
--if object_id(''dbo.'+@table+''', ''U'') is not null -- needs to be changed to detect if variable is null rather than table.
--drop table dbo.'+@table+'
'
print(@drop)
exec(@drop)
所以结果应该是:
DECLARE @temp_name VARCHAR(100)
select @temp_name= name
FROM sys.objects
WHERE DATEDIFF(day, create_date, getdate()) > 3
and name = 'DrinkSales'
select @temp_name
--if object_id('dbo.DrinkSales', 'U') is not null -- this should be changed to
--drop table dbo.DrinkSales
*if @temp_name is not null *
*drop dbo.drinksales*
(1 row affected)