zoukankan      html  css  js  c++  java
  • 存储过程使用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
  • 相关阅读:
    C++ 内置函数 判断字母、数字及大小写转换
    C++11 随机数 random
    rpc
    C++11 智能指针
    xargs 命令使用
    记录优秀的文章
    腾讯 测试开发
    struts2文件上传、下载、防止重复提交
    注解
    @RestController注解
  • 原文地址:https://www.cnblogs.com/chuifeng/p/1994129.html
Copyright © 2011-2022 走看看