读书人

在dbstudent中每个学生一个表以姓

发布时间: 2012-08-10 12:19:33 作者: rapoo

在dbstudent中,每个学生一个表,以姓名命名表,怎样删除所有姓李的学生表?请问怎么写
如题,删除单单一张表内的数据我会,删除表与表,怎么写

[解决办法]
union?
[解决办法]
我对这个也不熟,如果你的表么有什么constraint的话
如下代码应该能达到

SQL code
declare @cmd varchar(4000)declare cmds4 cursor for Select    'drop table [' + Table_Name + ']'From    INFORMATION_SCHEMA.TABLESWhere    Table_Name like 'tblacad%'open cmds4while 1=1begin    fetch cmds4 into @cmd    if @@fetch_status != 0 break    exec(@cmd)endCLOSE cmds4DEALLOCATE cmds4
[解决办法]
SQL code
--删除外键约束DECLARE c1 cursor for     select 'alter table ['+ object_name(parent_obj) + '] drop constraint ['+name+']; '    from sysobjects     where xtype = 'F'open c1declare @c1 varchar(8000)fetch next from c1 into @c1while(@@fetch_status=0)    begin         exec(@c1)        fetch next from c1 into @c1    endclose c1deallocate c1 --删除表DECLARE c2 cursor for     select 'drop table ['+name +']; '    from sysobjects     where xtype = 'u' open c2declare @c2 varchar(8000)fetch next from c2 into @c2while(@@fetch_status=0)    begin        exec(@c2)        fetch next from c2 into @c2    endclose c2deallocate c2
[解决办法]
SQL code
exec sp_msforeachtable @command1="drop table ? ;",                       @whereand='and schema_id = (select schema_id from sys.schemas where [name] like ''%李%'''
[解决办法]
提供思路如下

先用语句取出所有系统表的名称,可以根据李过滤

循环所有出来的表名,逐个drop table
[解决办法]
探讨

引用:

我对这个也不熟,如果你的表么有什么constraint的话
如下代码应该能达到
SQL code


declare @cmd varchar(4000)
declare cmds4 cursor for
Select
'drop table [' + Table_Name + ']'
From
INFORMATION_SCHEMA.TABLE……

读书人网 >SQL Server

热点推荐