当前位置: 动力学知识库 > 问答 > 编程问答 >

sql server - DROP Database with a WHERE clause - TSQL

问题描述:

I want to delete any development databases that are older than 15 days by comparing the date column in a specified table.

Is there a way to write a DROP database query with a WHERE clause or do I have to do a dynamic SQL?

Below is my SQL query:

DROP DATABASE IF EXISTS

WHERE (SELECT ID FROM master.TableName

WHERE Date < DATEADD(DAY, -15, GETDATE())

AND ColumnName1 = 1

AND ColumnName2 = 0)

网友答案:

sp_MSforeachdb will run a statement on every database.

exec sp_MSforeachdb 'IF EXISTS (SELECT ID FROM [?].dbo.TableName WHERE Date < DATEADD(DAY, -15, GETDATE()) AND ColumnName1 = 1 AND ColumnName2 = 0) DROP DATABASE [?];'
网友答案:

Try this code:

   IF EXISTS
   (SELECT ID FROM master.TableName
    WHERE Date < DATEADD(DAY, -15, GETDATE())
        AND ColumnName1 = 1
        AND ColumnName2 = 0)
       Drop database ......
分享给朋友:
您可能感兴趣的文章:
随机阅读: