Excel批量生成Sql语句(附带资源,随拿随用)

如题 今天在公司领导让用Excel做一个批量生成SQL文的工具经过网上的一顿查算是顺利完成任务

这样做的好处是 以后新增或者修改的一些操作 直接在这里可以一键生成后面可以进行批量覆盖

  1. 简单建一个索引目录 索引到各个表

 2. 写表数据 或者表的结构 这边是为了导入数据 写了一些数据

3. 写宏 使用的是VBA语言 简单看一下很容易理解的

  这个是在Excel文件里自带的编辑器

4. 页面调用宏

  将代码绑定在控件上就可以进行调用了

5. 效果

把这些sql语句复制到数据库工具里 运行一下就可以完成一些基本批量操作了 方便以后修改或者覆盖

这里给大家贴一个教科书式教学的帖子 我就是看这个学了几个小时就会一点点的,完全够用

https://www.cnblogs.com/wzh313/articles/9737573.html

鼠标贴一下我的代码

Sub CreateSql()
    Dim Arr(1 To 100) As String
    Dim StaArrrtLine, RowIndex, ColIndex, MaxCol, Index As Integer
    Dim srcSheet, destSheet, tempStr As String
    Dim TableName, strDrop, strCreate As String
    Dim Arr2(1 To 100) As Integer
    
    'Get Table Name
    srcSheet = Worksheets("Tools").Range("I1").Value
    TableName = srcSheet
    
   
    'Clear Cells
    Worksheets("Tools" & i).Cells.ClearContents
    Worksheets("Tools").Range("I1").Value = srcSheet
       
    'Write Delete
    strDrop = "Delete from " & TableName & ";"
    Worksheets("Tools").Cells(6, 2).Value = strDrop
    
    'Column line`
    ColIndex = 1
    RowIndex = 2
    ' Start Line to write
    StartLine = 9
    While Worksheets(srcSheet).Cells(1, ColIndex).Value <> ""
        ColIndex = ColIndex + 1
    Wend
    
    MaxCol = ColIndex

    ColIndex = 1
    Index = 1
      
    '获取表的日期字段序号存入Arr2中
    While Index < MaxCol
            tempStr = Worksheets(srcSheet).Cells(1, Index).Value
            If tempStr Like "dt_*" Then
            Arr2(Index) = Index
            Count = Count + 1
            End If
            Index = Index + 1
        
        Wend
    
   
    
    'Write Insert
    While Worksheets(srcSheet).Cells(RowIndex, ColIndex).Value <> ""
        While ColIndex < MaxCol
        
            tempStr = Worksheets(srcSheet).Cells(RowIndex, ColIndex).Value
            
                     
            If InStr(tempStr, "'") <> 0 Then
                ' 含有单引号
                tempStr = Replace(tempStr, "'", "''")
            End If
            
            
            If tempStr <> "" Then
                tempStr = "'" & Trim(tempStr) & "'"
            Else
                tempStr = "null"
            End If
          
            For i = 1 To Index
            If ColIndex = Arr2(i) And tempStr <> "null" Then
                tempStr = "to_date(" & tempStr & ",'YYYY-MM-DD HH24:MI:SS')"
               Exit For
            End If
            Next
           
                     
            Arr(ColIndex) = tempStr
            ColIndex = ColIndex + 1
        Wend
        
        tempStr = Arr(1)
        For i = 2 To 100
            If (Arr(i) <> "") Then
                tempStr = tempStr & "," & Arr(i)
            End If
        Next i
        
              
        strCreate = "Insert into " & TableName & " values(" & tempStr & ");"
        Worksheets("Tools").Cells(StartLine, 2).Value = strCreate
     
        RowIndex = RowIndex + 1
        ColIndex = 1
        StartLine = StartLine + 1
    Wend
End Sub

至此 希望可以帮助到大家 再次贴一遍那个帖子地址

https://www.cnblogs.com/wzh313/articles/9737573.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值