一、建表语句

  不带分区版本:V1.0:

Sub createTableDDL()
'自动创建建表语句
'定义换行和TAB
Ln = Chr() + Chr()
TB = Chr()
'定义脚本目录
Dim dir AS String
dir = "C:\CREATE_TABLE_DDL"
Set FSOE = CreateObject("Scripting.FileSystemObject")
If FSOE.folderexists(dir) = False Then
MkDir dir
End If '调用脚本定义
Set SqlFileDDL = FSOE.CreateTextFile("C:\CREATE_TABLE_DDL\create_table_ddl.sql", True)
'获得表名
tableName = Trim(Cells(, ).Value)
'获得表注释
tableComment = Trim(Cells(, ).Value)
'获得创建者
createBy = Trim(Cells(, ).Value)
Dim dt As Date
dt = Format(Date, "yyyy-mm-dd")
'获得当前日期
createDate = dt
'获得A列已使用的行数
count_row_k = [A65536].End(xlUp).Row
'定义SQL
SQL = "--创建者:" & createBy & Ln
SQL = SQL & "--创建时间:" & createDate & Ln
SQL = SQL & "DROP TABLE IF EXISTS " & tableName & " ;" & Ln
SQL = SQL & "CREATE TABLE " & tableName & "("
'写入文件
SqlFileDDL.WriteLine (SQL)
For i = To count_row_k
If i = count_row_k Then
col_name = TB & LCase(Cells(i, )) & " " & UCase(Cells(i, )) & " COMMENT '" & Trim(Cells(i, )) & "'" & Ln & ")"
SqlFileDDL.WriteLine(col_name)
Exit For
End If
col_name = TB & LCase(Cells(i, )) & " " & UCase(Cells(i, )) & " COMMENT '" & Trim(Cells(i, )) & "',"
SqlFileDDL.WriteLine(col_name)
Next
SqlFileDDL.WriteLine("COMMENT '" & tableComment & "'")
MsgBox("生成成功!生成路径为:" & dir)
End Sub

   带分区与生命周期版本V2.0:

Sub create_ddl()
'自动创建建表语句
'定义换行和TAB
Ln = Chr() + Chr()
TB = Chr()
'定义脚本目录
Dim dir As String
dir = "C:\CREATE_TABLE_DDL"
Set FSOE = CreateObject("Scripting.FileSystemObject")
If FSOE.folderexists(dir) = False Then
MkDir dir
End If '调用脚本定义
Set SqlFileDDL = FSOE.CreateTextFile("C:\CREATE_TABLE_DDL\create_table_ddl.sql", True)
'获得表名
TableName = Trim(Cells(, ).Value)
'获得表注释
tableComment = Trim(Cells(, ).Value)
'获得生命周期
lifecycle = Trim(Cells(, ).Value)
'获得创建者
createBy = Trim(Cells(, ).Value)
Dim dt As Date
dt = Format(Date, "yyyy-mm-dd")
'获得当前日期
createDate = dt
'获得A列已使用的行数
count_row_k = [A65536].End(xlUp).Row
'获得E列已使用的行数
part_row_k = [E65536].End(xlUp).Row
'定义SQL
Sql = "--创建者:" & createBy & Ln
Sql = Sql & "--创建时间:" & createDate & Ln
Sql = Sql & "DROP TABLE IF EXISTS " & TableName & " ;" & Ln
Sql = Sql & "CREATE TABLE " & TableName & "("
'写入文件
SqlFileDDL.WriteLine (Sql)
For i = To count_row_k
If i = count_row_k Then
col_name = TB & LCase(Cells(i, )) & " " & UCase(Cells(i, )) & " COMMENT '" & Trim(Cells(i, )) & "'" & Ln & ")"
SqlFileDDL.WriteLine (col_name)
Exit For
End If
col_name = TB & LCase(Cells(i, )) & " " & UCase(Cells(i, )) & " COMMENT '" & Trim(Cells(i, )) & "',"
SqlFileDDL.WriteLine (col_name)
Next
SqlFileDDL.WriteLine ("COMMENT '" & tableComment & "'")
'加上分区列
If part_row_k > Then
part_col_name = "PARTITIONED BY ("
SqlFileDDL.WriteLine (part_col_name)
For j = To part_row_k
If j = part_row_k Then
part_col_name = TB & LCase(Cells(j, )) & " " & UCase(Cells(j, )) & " COMMENT '" & Trim(Cells(j, )) & "'"
SqlFileDDL.WriteLine (part_col_name)
Exit For
End If
part_col_name = TB & LCase(Cells(j, )) & " " & UCase(Cells(j, )) & " COMMENT '" & Trim(Cells(j, )) & "'" & ","
SqlFileDDL.WriteLine (part_col_name)
Next
End If
'加上生命周期
SqlFileDDL.WriteLine ("LIFECYCLE " & lifecycle & ";")
MsgBox ("生成成功!生成路径为:" & dir)
End Sub

  模板:

  更新版本2.1:

Sub createTableDDL()
'定义换行和TAB
Ln = Chr() + Chr()
TB = Chr()
'定义脚本目录
Dim dir AS String
dir = "C:\CREATE_TABLE_DDL"
Set FSOE = CreateObject("Scripting.FileSystemObject")
If FSOE.folderexists(dir) = False Then MkDir dir
'调用脚本定义
Set SqlFileDDL = FSOE.CreateTextFile("C:\CREATE_TABLE_DDL\create_table_ddl.sql", True)
'获得表名
tableName = Trim(Cells(, ).Value)
'获得表注释
tableComment = Trim(Cells(, ).Value)
'获得生命周期
lifecycle = Trim(Cells(, ).Value)
'获得创建者
createBy = Trim(Cells(, ).Value)
Dim dt As Date
dt = Format(Date, "yyyy-mm-dd")
'获得当前日期
createDate = dt
'获得A列已使用的行数
count_row_k = [A65536].End(xlUp).Row
'获得E列已使用的行数
part_row_k = [E65536].End(xlUp).Row
'定义SQL
SQL = "--创建者:" & createBy & Ln & _
"--创建时间:" & createDate & Ln & _
"DROP TABLE IF EXISTS " & tableName & " ;" & Ln & _
"CREATE TABLE " & tableName & "("
'写入文件
SqlFileDDL.WriteLine (SQL)
For i = To count_row_k -
col_name = TB & LCase(Cells(i, )) & " " & UCase(Cells(i, )) & " COMMENT '" & Trim(Cells(i, )) & "',"
SqlFileDDL.WriteLine(col_name)
Next
'最后一列
i = count_row_k
col_name = TB & LCase(Cells(i, )) & " " & UCase(Cells(i, )) & " COMMENT '" & Trim(Cells(i, )) & "'" & Ln & ")"
SqlFileDDL.WriteLine(col_name)
SqlFileDDL.WriteLine("COMMENT '" & tableComment & "'")
'加上分区列
If part_row_k > Then
part_col_name = "PARTITIONED BY ("
SqlFileDDL.WriteLine(part_col_name)
For j = To part_row_k -
part_col_name = TB & LCase(Cells(j, )) & " " & UCase(Cells(j, )) & " COMMENT '" & Trim(Cells(j,)) & "'" & ","
SqlFileDDL.WriteLine(part_col_name)
Next
j = part_row_k
part_col_name = TB & LCase(Cells(j, )) & " " & UCase(Cells(j, )) & " COMMENT '" & Trim(Cells(j,)) & "'"
SqlFileDDL.WriteLine(part_col_name)
End If
'加上生命周期
SqlFileDDL.WriteLine("LIFECYCLE " & lifecycle & ";")
MsgBox("生成成功!生成路径为:" & dir)
End Sub

//模板在微云文件中。

二、反向解析

  通过DDL,反向解析出字段填充到excel中

  反向解析1.0:

REM "获取数组长度"
Public Function ArrayLength(ByVal ary) As Integer
ArrayLength = UBound(ary) - LBound(ary) +
End Function Sub resverse_parse()
'获得表名
tableDDL = Trim(Cells(, ).Value)
'截取语句字段部分(以括号分隔)
index_quote_left_1 = InStr(,tableDDL,"(")
index_quote_right_1 = InStr(,tableDDL,")")
index_quote_left_2 = InStr(index_quote_right_1 + ,tableDDL,"(")
index_quote_right_2 = InStr(index_quote_right_1 + ,tableDDL,")")
table_comment_content = Mid(tableDDL,index_quote_right_1 + ,index_quote_left_2)
table_comment_arr = Split(table_comment_content, "'")
table_comment = table_comment_arr()
Cells(, ).Value = table_comment
table_name = Mid(tableDDL,,index_quote_left_1 - )
Cells(, ).Value = table_name
table_content = Mid(tableDDL,index_quote_left_1,index_quote_right_1 - index_quote_left_1 - )
content_arr = Split(table_content,",")
content_len = ArrayLength(content_arr) For i = to content_len - step
col_arr = Split(content_arr(i)," ")
j = i +
col_name = col_arr()
col_comment = Replace(col_arr(),"'","")
MsgBox col_comment
Cells(j, ).Value = col_name
Cells(j, ).Value = col_comment
Next i
End Sub

  清除重置1.1(1.0为直接清除指定区域版,已废弃):

Sub clear()
'采用先清除后填充的策略
Range("A1:D256").ClearContents
'填充
Cells(, ).Value = "建表DDL"
Cells(, ).Value = "表名"
Cells(, ).Value = "表注释"
Cells(, ).Value = "列名"
Cells(, ).Value = "列注释"
End Sub

  模板文件在百度云中,图示如下:

  

excel的宏与VBA实践——建表语句的更多相关文章

  1. 基于表的数据字典构造MySQL建表语句

    表的数据字典格式如下: 如果手动写MySQL建表语句,确认麻烦,还不能保证书写一定正确. 写了个Perl脚本,可快速构造MySQL脚本语句. 脚本如下: #!/usr/bin/perl use str ...

  2. hive查看建表语句

    查看hive建表语句:show create table tablename; 查看hive表结构:describe  tablename; 简写:desc tablename;

  3. 批量导出hive表的建表语句

    转的这里的 首先先导出所有的table表 hive -e "use xxxdb;show tables;" > tables.txt 然后再使用hive内置语法导出hive表 ...

  4. 根据javabean转换为mysql建表语句与mapper内容

    原文地址:  https://www.cnblogs.com/Jeffscnblog/p/10072483.html 一般上,我们会使用数据库表转换为javabean.dao.或是mapper,就叫逆 ...

  5. MySQL 建表语句 create table 中的列定义

    MySQL 建表语句 create table 中的列定义: column_definition: data_type [NOT NULL | NULL] [DEFAULT default_value ...

  6. sqlserver 表中记录生成insert,可以加条件,可以生成建表语句

    sqlserver 表中记录生成insert,可以加条件,可以生成建表语句 create PROCEDURE [sp_getinsert] ( ) , --如果非默认架构,可以加上架构名 例如:sch ...

  7. 三种常用的MySQL建表语句

    MySQL建表语句是最基础的SQL语句之一,下面就为您介绍最常用的三种MySQL建表语句,如果您对MySQL建表语句方面感兴趣的话,不妨一看. 1.最简单的: CREATE TABLE t1(    ...

  8. mysql 常用命令,连接数据库,查看建表语句,批量导入数据,批量更新数据,连接查询

    1. 1)MySQL 连接本地数据库,从cmd中进入mysql命令编辑器: root root分别为用户名和密码 mysql -uroot -proot 2)MySQL 连接本地数据库,用户名为“ro ...

  9. oracle的建表语句

    oracle数据库的建表语句,具体语法如下: CREATE TABLE tablename (column_name datatype [null,not null], column_name dat ...

随机推荐

  1. OSGI企业应用开发(三)Eclipse中搭建Equinox运行环境

    上篇文章介绍了如何在Eclipse中搭建Felix的运行环境,我们需要將Bundle发布到Felix框架的bundle目录下,Felix框架启动时才会自动加载这些Bundle,否则需要在Felix框架 ...

  2. python实现分页插件

    class Pages: def __init__(self, current_page, data_count, per_page_count=10, pager_num=7):#pager_num ...

  3. 如何解决Your project contains C++ files but it is not using a supported native build system

    最近因为项目需要下载Android终端模拟器(Android-Terminal-Emulator)源码进行调试编译,编译过程中出现报错 Error:Execution failed for task ...

  4. MySQL——通过EXPLAIN分析SQL的执行计划

    在MySQL中,我们可以通过EXPLAIN命令获取MySQL如何执行SELECT语句的信息,包括在SELECT语句执行过程中表如何连接和连接的顺序. 下面分别对EXPLAIN命令结果的每一列进行说明: ...

  5. 测试笔试单选题(持续更新ing)

    1.在GB/T17544中,软件包质量要求包括三部分,即产品描述要求._____.程 序和数据要求.( A ) A.用户文档要求 B.系统功能要求 C.设计要求说明 D.软件配置要求 2.软件的六大质 ...

  6. Python pip 安装与使用

    pip 是 Python 包管理工具,该工具提供了对Python 包的查找.下载.安装.卸载的功能. 目前如果你在 python.org 下载最新版本的安装包,则是已经自带了该工具. Python 2 ...

  7. python被游标坑了

    为了方便,这次就不单独写脚本了,直接一步一步执行下来就好了先说下游标,就是一个指针,比如我有1234每条占一行,那么初始游标默认是在1的位置,当read(1)后,游标自动向下next,现在指在2的位置 ...

  8. Linux内核参数基础优化

    web 服务负载均衡器常规网站服务器优化的基本配置: net.ipv4.tcp_fin_timeout =2 net.ipv4.tcp_tw_reuse =1 net.ipv4.tcp_tw_recy ...

  9. 【史上最全】申请配置阿里云服务器,并部署IIS和开发环境,项目上线经验

    最近一年在实验室做web后端开发,涉及到一些和服务器搭建及部署上线项目的相关经验,写个帖子和小伙伴们分享,一同进步! 首先谈一下,为什么越来越多中小型公司/实验室,部署项目的趋势都是在云服务器而不是普 ...

  10. 过滤掉URL中的参数部分

    //将超链接的参数部分滤掉 ?xxx if(url.contains("?")){ url=url.substring(0,url.indexOf("?")); ...