存储过程使用shell脚本执行sql文件

来源:互联网 发布:淘宝网支付宝登陆 编辑:程序博客网 时间:2024/05/18 17:59

今天接到的需求是把所有表的创建写到储存过程里面。

收到创建表的脚本之后就傻了,60-70个表,还包含存储过程、视图等。

那么如何解决呢。

思路就是在存储过程里面使用shell脚本执行sql脚本文件。

通过MSDN得到执行shell的函数:xp_cmdshell。

下面是完整的脚本:

复制代码
CREATE PROCEDURE CreatTable ( @UserName varchar(200), @PassWord varchar(200), @FilePath varchar(200), @Trusted bit)ASBEGIN SET NOCOUNT ON; declare @shell varchar(max); EXEC sys.sp_configure 'show advanced options',1; --Open shell EXEC sys.sp_configure 'xp_cmdshell',1 if @Trusted=1 Set @shell= 'osql -E Northwind -i '+ @FilePath; else --use user name connection Set @shell= 'osql -U '+ @UserName +' -P '+ @PassWord +' -d Northwind -i '+ @FilePath; EXEC master..xp_cmdshell @shell; --Close shell EXEC sys.sp_configure 'xp_cmdshell',0ENDGO
原创粉丝点击