今天接到的需求是把所有表的创建写到储存过程里面。
收到创建表的脚本之后就傻了,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脚本执行SQL脚本文件的方法,适用于处理大量表创建任务,包括存储过程和视图。通过使用xp_cmdshell函数,简化了在数据库中执行脚本文件的过程。

2100

被折叠的 条评论
为什么被折叠?



