sqlserver动态脚本及python执行目录下所有sql

sqlserver动态脚本

use DataBase
declare @sql nvarchar(4000)
declare @cols nvarchar(2000)
set @cols = stuff(
    (
        select ','+name
        from sys.columns
        where object_id = object_id(N'[dbo].[bill]',N'U') and name <>'TIMESTAMP'
        for xml path('')
    ),1,1,''
)

set @sql = N'truncate table DataBase.[dbo].[bill];
             SET IDENTITY_INSERT DataBase.[dbo].[bill] ON 
                 insert into DataBase.[dbo].[bill]('+@cols+N')
                 select '+@cols +N'
                 from DataBase.[dbo].[billBackup];
             SET IDENTITY_INSERT DataBase.[dbo].[bill] OFF 
             '
exec (@sql);

python执行目录下所有sql文件

try:
    file_name = os.listdir('src//sql')
    for name in file_name:
        with open('src//sql//%s' % name) as f:
            sql_list = f.read()
        self.mssql_handler.execute_sql(sql_list)
    logger.info('数据库删除成功!')
except Exception as e:
    logger.error('删除数据库出错:%s' % str(e))

 

转载于:https://www.cnblogs.com/carlvine/articles/9726394.html

你可能感兴趣的:(sqlserver动态脚本及python执行目录下所有sql)