Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Drop table using statement store in a table

Author  Topic 

micnie_2020
Posting Yak Master

232 Posts

Posted - 2013-05-20 : 05:16:13
Hi All,

I have a
table name: TempTablesList
column: Tables

select tables from TempTablesList will return me a list of result:-

drop table temp_AAAA
drop table temp_BBBB
drop table tblArch_Abu
drop table tblArch_Ali


How can i execute this drop table value to drop all the tables listed in the tables at TempTableList table?

Please advise.

Thank you.

Regards,
Micheale

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-05-20 : 05:47:49
you have to run as dynamic statement....
--try this..
DECLARE @SQL VARCHAR(MAX) = ''
SELECT @SQL = @SQL +'; ' + tables FROM TempTablesList;
SET @SQL = STUFF(@SQL, 1, 2, '')
EXEC (@SQL)

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-20 : 07:14:14
just do a statement like

SET NOCOUNT ON
GO
SELECT 'DROP TABLE ' + Tables
FROM TempTablesList


choose output as text option in SSMS and execute the above query

in output tab copy the output obtained which will be list of drop statements and paste it into a new window connecting to required db and execute.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-05-20 : 07:23:00
Hi visakh,
I think OP has "DROP Statements" in the column Tables of TempTableList (OPs title is statement stored in a table)

--
Chandu
Go to Top of Page
   

- Advertisement -