批量执行脚本文件
批量执行脚本文件
--王成辉原创,转贴请注明出自微软BI开拓者www.windbi.com
--SQL2000中的方法
create table #T(id int identity(1,1),col varchar(100))
insert into #t exec master..xp_cmdshell 'dir E:\*.sql'
delete from #t where col not like '%.sql' or col is null
update #T set col=right(col,len(col)-36) --这里的36可能需要你做相应的修改
declare @sql varchar(500),@id int,@id1 int
select @id=min(id),@id1=max(id) from #t
while @id<=@id1begin
select @sql='isql -Usa -Ppassword -Sserver_name -ddb_name -iE:\'+col from #t where id=@id exec master..xp_cmdshell @sql, no_output
set @id=@id+1
end
drop table #T
----------------------------------------------------------------------
注:该方法在2005中也可以用,或者将isql改为sqlcmd也可以。由于SQL2005中的的xp_cmdshell默认是关闭的,所以要先启动。如果已经启动就不用再启动了。启动用下面的语句:
EXEC sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
EXEC sp_configure 'xp_cmdshell', 1
GO
RECONFIGURE
GO

拓狼 最后编辑于 2007-07-17 09:11:41
虽有智慧,不如乘势;虽有鎡基,不如待时。
君子学以聚之,问以辨之,宽以居之,仁以行之。
独学而无友,则孤陋而寡闻。