Creating a dynamic drop view sql statement

Hey everyone,

I'm attempting to create a dynamically generated sql statement that will drop all views in the designated database and schema but I'm running into some issues.  Here's what I have so far:

declare @databasename varchar(50);
declare @model_id INT;
declare @SchemaName varchar(8);
declare @SQL nvarchar(max)

set @DatabaseName = 'Database1'
set @model_id = 35
set @SchemaName = 'Schema1'

select @SQL = 'IF EXISTS(select * from [' + @DatabaseName + '].INFORMATION_SCHEMA.VIEWS WHERE TABLE_SCHEMA = ''' + @SchemaName + ''')
DROP VIEW [TABLE_SCHEMA].[TABLE_NAME]
FROM [' + @DatabaseName + '].INFORMATION_SCHEMA.VIEWS
WHERE [TABLE_SCHEMA] = ''' + @SchemaName = '''

print @SQL
EXEC (@SQL)
'

print @SQL
EXEC(@SQL)

Open in new window


I think I have the bones but just having trouble with the last bit to make it run properly.  In theory this should generate a view existence check and drop statement for each view that exists in the database schema and execute them.  Any help is very much appreciated.

Thanks,
Glen
jisoo411Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
That won't do it.  You need a separate, specific DROP statement for every view.

I suggest inserting the generated drop statements into a temp table, and cursor thru them and execute them.  If you're willing to take that approach, let me know and I will code it up and post it here.

You could also try to concatenate the statements using XML and run them as a CLOB, but certain special chars are then a royal p.i.t.a. to deal with.
Alpesh PatelAssistant ConsultantCommented:
Hi Expert no need to use Cursor to create Drop view script for each view.


USE AdventureWorks
GO
DECLARE @listStr VARCHAR(MAX)
SELECT @listStr = COALESCE(@listStr+',' ,'') + Name
FROM Production.Product
SELECT @listStr
GO

Incorporate your need here. This is just a hint. You will get result because I have been using the same for multiple drop or create script.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jisoo411Author Commented:
Got it!  Here's the piece of corrected code that I'll be integrating into the original script:

declare @SQL nvarchar(max);

select @SQL = coalesce(@SQL + ',','') + 'DROP VIEW ' + TABLE_SCHEMA + '.' + TABLE_NAME from database1.INFORMATION_SCHEMA.VIEWS where TABLE_SCHEMA = 'schema1'

exec database1.dbo.sp_executesql @SQL

Open in new window


Thanks for the help, I didn't want to do this procedurally by using temp tables and loops so that my code would run faster.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.