-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql_extract_stored_proc.vbs
242 lines (186 loc) · 6.33 KB
/
sql_extract_stored_proc.vbs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
' ===================================================
'
' Author : Christophe Avonture
' Date : May 2018
'
' Connect to a SQL Server database, obtain the list of
' stored procedures (USPs) in that db (process all schemas), get
' the code in these stored procs and save them as text files (.md)
'
' At the end, we'll have as many files as there are stored procs
' in the database. One text file by stored proc.
'
' Files will be saved under the /results folder.
'
' Running this script against a SQL Server DB will take a local
' copy of your USPs : you can then take a backup of them easily.
'
' NOTE : The user should have enough permissions on SQL Server niveau
' for retrieving the code of the stored procedure. This is never the
' case of a "simple" user and requires advanced permissions. So; if
' generated files are empty, first check user's permissions (or directly
' use an "admin" user to check if it's better).
'
' Documentation : https://github.com/cavo789/vbs_sql_extract_stored_proc
' ===================================================
Option Explicit
Const cServerName = "" ' <== Name of your SQL server
Const cDatabaseName = "" ' <== Name of the database
Const cUserName = "" ' <== User name
Const cPassword = "" ' <== User password
Dim sDatabaseName, sServerName, sUserName, sPassword
' ---------------------------------------------------
'
' Show help screen
'
' ---------------------------------------------------
Sub ShowHelp()
wScript.echo " ==============================="
wScript.echo " = vbs_sql_extract_stored_proc ="
wScript.echo " ==============================="
wScript.echo ""
wScript.echo " This script requires four parameters : the server, "
wScript.echo " database name, login and password to use for the "
wScript.echo "connection."
wScript.echo ""
wScript.echo " " & wScript.ScriptName & " 'ServerName', 'dbTest', 'Login', 'Password'"
wScript.echo ""
wScript.echo "To get more info, please read https://github.com/cavo789/vbs_sql_extract_stored_proc"
wScript.echo ""
wScript.quit
End sub
' ---------------------------------------------------
'
' Return the current, running, folder
'
' ---------------------------------------------------
Public Function getCurrentFolder()
Dim objFSO, objFile
Dim sFolder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(Wscript.ScriptFullName)
sFolder = objFSO.GetParentFolderName(objFile) & "\"
Set objFile = Nothing
Set objFSO = Nothing
getCurrentFolder = sFolder
End Function
' ---------------------------------------------------
'
' Create a folder if not yet there
'
' ---------------------------------------------------
Public Function makeFolder(ByVal sFolderName)
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FolderExists(sFolderName) Then
Call objFSO.CreateFolder(sFolderName)
End if
Set objFSO = Nothing
End Function
' ---------------------------------------------------
'
' Remove all files in the specified folder
'
' ---------------------------------------------------
Public Function emptyFolder(ByVal sFolderName)
Dim objFSO, objFiles, objFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFiles = objFSO.GetFolder(sFolderName).Files
For Each objFile In objFiles
objFile.Delete
Next
set objFile = Nothing
set objFSO = Nothing
End function
' ---------------------------------------------------
'
' Create a text file on the disk, UTF-8 with LF
'
' ---------------------------------------------------
Public Sub CreateTextFile(ByVal sFileName, ByVal sContent)
Dim objStream
Set objStream = CreateObject("ADODB.Stream")
With objStream
.Open
.CharSet = "x-ansi" ' "UTF-8"
.LineSeparator = 10
.Type = 2 ' adTypeText
.WriteText sContent
.SaveToFile sFileName, 2
.Close
End with
set objStream = Nothing
End Sub
Dim sDSN, sSQL
Dim objConn, rs
Dim sLine, sPath, sFileName, sProcName, sContent, sMDTable
Dim wProcsCount
' Get constants
sServerName = trim(cServerName)
sDatabaseName = trim(cDatabaseName)
sUserName = trim(cUserName)
sPassword = trim(cPassword)
' If one variable is not set by constants, get from
' command line arguments
If (sServerName = "") or (sDatabaseName = "") or _
(sUserName = "") or (sPassword = "") Then
If (wScript.Arguments.Count < 4) Then
Call ShowHelp
wScript.quit
Else
' Read parameters server -> db -> login -> password
sServerName = Trim(Wscript.Arguments.Item(0))
sDatabaseName = Trim(Wscript.Arguments.Item(1))
sUserName = Trim(Wscript.Arguments.Item(2))
sPassword = Trim(Wscript.Arguments.Item(3))
End if
End If
' Define the results folder : a subfolder of the folder
' containing this VBS script.
sPath = getCurrentFolder() & "results\"
makeFolder(sPath)
' Remove files from a previous run
emptyFolder(sPath)
wProcsCount = 0
' Define the connection string
sDSN = "Driver={SQL Server};Server={" & sServerName & "};" & _
"Database={" & sDatabaseName & "};" & _
"User Id={" & sUserName & "};" & _
"Password={" & sPassword & "};"
Set objConn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
objConn.ConnectionTimeout = 60
objConn.CommandTimeout = 60
objConn.Open sDSN
' Get the list of tables in the database
sSQL = "SELECT Specific_Catalog, Specific_Schema, Specific_Name, " & _
"Routine_Definition, Created, Last_Altered " & _
"FROM INFORMATION_SCHEMA.Routines " & _
"WHERE ROUTINE_TYPE = 'PROCEDURE';"
Set rs = objConn.Execute(sSQL)
If Not rs.Eof Then
' Iterate for each table
Do While Not rs.EOF
' Derive the filename :
' * The database name (f.i. dbAdmin)
' * The schema (f.i. dbo)
' * The stored proc name (f.i. uspGetData)
sProcName = rs.Fields("Specific_Catalog").Value & _
"." & rs.Fields("Specific_Schema").Value & _
"." & rs.Fields("Specific_Name").Value
sFileName = sPath & replace(sProcName, ".", "_") & ".md"
' Get the stored procedure content i.e. the programmation
sContent = "```SQL" & vbLf & _
rs.fields("Routine_Definition").Value & vbLf & _
"```"
sContent = "# Stored procedure definition" & vbLf & vbLf & _
"> Created on " & rs.fields("Created").Value & " | " & vbLf & _
"> Last updated on " & rs.fields("Last_Altered").Value & vbLf & vbLf & _
sContent & vbLf
Call CreateTextFile(sFileName, sContent)
rs.MoveNext
Loop
End if
rs.Close
Set rs = Nothing
Set objConn = Nothing