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

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

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

那么如何解决呢。

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

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

下面是完整的脚本:

  
    
CREATE PROCEDURE CreatTable
(
@UserName varchar ( 200 ),
@PassWord varchar ( 200 ),
@FilePath varchar ( 200 ),
@Trusted bit
)
AS
BEGIN
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 ' , 0
END
GO

你可能感兴趣的:(shell脚本)