Skip to content Skip to sidebar Skip to footer

Drop A Schema Using Variable Name

I have the following: DECLARE @SchemaName NVARCHAR(MAX) SET @SchemaName = 'MySchema' DROP SCHEMA MySchema How should I go to use the variable? I tried DROP SCHEMA OBJECT_ID(@Sche

Solution 1:

Try something like this:

DECLARE@sql nvarchar(max), @SchemaName NVARCHAR(MAX)
SET@SchemaName='MySchema'set@sql='DROP SCHEMA '+ quotename(@SchemaName)

---print @sqlexec sp_executesql(@sql)

Solution 2:

Try using Dynamic sql

DECLARE@SchemaName NVARCHAR(MAX)
SET@SchemaName='test'DECLARE@sql NVARCHAR(128)=''SET@sql='DROP SCHEMA '+ quotename(@SchemaName) +''--print @sqlEXEC (@sql) 

Also i don't think you can delete a record from SYS.SCHEMAS view

Post a Comment for "Drop A Schema Using Variable Name"