select * from table with (TABLOCK)  锁定表
select column_name from information_schema.columns where table_name='你的表名'  取表的列名
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_JobSet]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 drop procedure [dbo].[p_JobSet]

=========================================================
--下列假設只要主鍵相同就認為是重復的
A.重复的
1.結果顯示為A表資料
select * from A where exists(select * from B where 主键=a.主键)
2.結果顯示為B表資料
select * from B where exists(select * from A where 主键=b.主键)
3.結果顯示為AB兩表資料(假設AB表中主鍵是唯一的)
select * from A join B on a.主键=b.主键
B.不重复的
1.結果顯示為A表資料
select * from A where not exists(select * from B where 主键=a.主键)
2.結果顯示為B表資料
select * from B where not exists(select * from A where 主键=b.主键)

=================================

exec sp_msforeachtable 'if not exists(select * from ?) drop table ?'
删除记录为空的表
select * from syscolumns where id=object_id(N'表名') 取列
if exists(select top 1 * from tb where name='列名' and object_id('表名'))
    print '存在'
else
    print '不存在'
==========================================================
--参考:将某个目录上的Excel表,导入到数据库中

--将所有的Excel文件放到一个目录中,假设为c:\test\,然后用下面的方法来做

create table #t(fname varchar(260),depth int,isf bit)
insert into #t exec master..xp_dirtree 'c:\test',1,1
declare tb cursor for select fn='c:\test'+fname from #t
 where isf=1 and fname like '%.xls'  --取.xls文件(EXCEL)
declare @fn varchar(8000)
open tb
fetch next from tb into @fn
while @@fetch_status=0
begin
 --下面是查询语句,需要根据你的情况改为插入语句
    --插入已有的表用:insert into 表 selct * from ...
    --创建表用:select * into 表 from ...
 set @fn='select * from
 OPENROWSET(''MICROSOFT.JET.OLEDB.4.0'',''Excel 5.0;HDR=YES;DATABASE='+@fn+''',全部客户$)'
 exec(@fn)
 fetch next from tb into @fn
end
close tb
deallocate tb

posted on 2005-01-19 09:50  James Wong   阅读(536)  评论(0编辑  收藏  举报