create table ttt ( aa nvarchar(50) ) declare @TableName nvarchar(50) declare cursor_1 cursor for select name from sysobjects where xtype='U' open cursor_1 fetch next from cursor_1 into @TableName while @@fetch_status=0 begin exec ('if (select count(*) from '+@TableName+')=0 insert into ttt values ('''+@TableName+''')') fetch next from cursor_1 into @TableName end select * from ttt drop table ttt close cursor_1 deallocate cursor_1
找到方法了,不用这么麻烦select sysobjects.name,rows from sysobjects,sysindexes where sysobjects.name=sysindexes.name and xtype='u' and rows=0
(
aa nvarchar(50)
)
declare @TableName nvarchar(50)
declare cursor_1 cursor for select name from sysobjects where xtype='U'
open cursor_1
fetch next from cursor_1 into @TableName
while @@fetch_status=0
begin
exec ('if (select count(*) from '+@TableName+')=0 insert into ttt values ('''+@TableName+''')')
fetch next from cursor_1 into @TableName
end
select * from ttt
drop table ttt
close cursor_1
deallocate cursor_1