使用变量名删除模式

2021-09-10 00:00:00 tsql sql-server

我有以下几点:

DECLARE @SchemaName NVARCHAR(MAX)
SET @SchemaName = 'MySchema'

DROP SCHEMA MySchema

我应该怎么去使用这个变量?我试过 DROP SCHEMA OBJECT_ID(@SchemaName)

How should I go to use the variable? I tried DROP SCHEMA OBJECT_ID(@SchemaName)

也许 DELETE FROM SYS.SCHEMAS WHERE NAME = @SchemaName 等于 DROP?

推荐答案

试试这个:

DECLARE @sql nvarchar(max), @SchemaName NVARCHAR(MAX)
SET @SchemaName = 'MySchema'

set @sql = 'DROP SCHEMA ' + quotename(@SchemaName)

---print @sql
exec sp_executesql(@sql)

相关文章