如题 今天在公司领导让用Excel做一个批量生成SQL文的工具经过网上的一顿查算是顺利完成任务
这样做的好处是 以后新增或者修改的一些操作 直接在这里可以一键生成后面可以进行批量覆盖
- 简单建一个索引目录 索引到各个表
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
至此 希望可以帮助到大家 再次贴一遍那个帖子地址