Skip to content

Instantly share code, notes, and snippets.

@tallpeak
Last active November 7, 2019 19:48
Show Gist options
  • Save tallpeak/ce07ab5cb7110c3fe19ce0c3e13cf205 to your computer and use it in GitHub Desktop.
Save tallpeak/ce07ab5cb7110c3fe19ce0c3e13cf205 to your computer and use it in GitHub Desktop.
Script all stored procedures for all MSSQL databases for select servers
// edit these lines for your liking:
let servers = ["server1"; "server2"]
let baseDir = @"c:\temp"
let getConnectionString (serverName:string) =
sprintf @"Data Source=%s;Initial Catalog=master;Integrated Security=True" serverName
#I """../exportProcs\packages\System.Data.SqlClient\lib\net461"""
#r """System.Data.SqlClient.dll"""
open System
open System.Data.SqlClient
open System.IO
let getDBs = @"select name
from master..sysdatabases
where [name] not in ('master','tempdb','model','msdb','LogonAudit','SQLOPSDB')"
//from https://docs.microsoft.com/en-us/sql/relational-databases/system-compatibility-views/sys-sysobjects-transact-sql?view=sql-server-ver15
let sqlModules (db:string) =
sprintf """use %s
SELECT o.name,definition, o.xtype,
typeDesc = CASE o.xtype
WHEN 'AF' THEN 'AggregateFunction'
WHEN 'C' THEN 'CHECKconstraint'
WHEN 'D' THEN 'Default'
WHEN 'F' THEN 'FOREIGNKEYconstraint'
WHEN 'FN' THEN 'ScalarFunction'
WHEN 'FS' THEN 'AssemblyScalarFunction'
WHEN 'FT' THEN 'AssemblyTableValuedFunction'
WHEN 'IF' THEN 'InlinedTableFunction'
WHEN 'IT' THEN 'Internaltable'
WHEN 'L' THEN 'Log'
WHEN 'P' THEN 'StoredProcedure'
WHEN 'PC' THEN 'AssemblyStoredProcedure'
WHEN 'PK' THEN 'PRIMARYKEYconstraint'
WHEN 'RF' THEN 'Replicationfilterstoredprocedure'
WHEN 'S' THEN 'Systemtable'
WHEN 'SN' THEN 'Synonym'
WHEN 'SQ' THEN 'Servicequeue'
WHEN 'TA' THEN 'AssemblyDMLtrigger'
WHEN 'TF' THEN 'Tablefunction'
WHEN 'TR' THEN 'SQLDMLTrigger'
WHEN 'TT' THEN 'Tabletype'
WHEN 'U' THEN 'Usertable'
WHEN 'UQ' THEN 'UNIQUEconstraint'
WHEN 'V' THEN 'View'
WHEN 'X' THEN 'ExtendedStoredProcedure'
END
FROM sys.sql_modules m
join sys.sysobjects o
on m.object_id = o.id
where o.category=0
order by 1""" db
let gsr (rdr:SqlDataReader) (n:int) : String =
match rdr.[n] with
| null -> ""
| v when v.Equals( DBNull.Value ) -> ""
| v -> v :?> String
for serverName in servers do
let conn = new SqlConnection(getConnectionString serverName)
conn.Open()
let cmdDbs = new SqlCommand(getDBs, conn)
let rdrDbs = cmdDbs.ExecuteReader()
let databaseNames = [| while rdrDbs.Read() do yield gsr rdrDbs 0 |]
rdrDbs.Close()
for databaseName in databaseNames do
let cmd = new SqlCommand(sqlModules databaseName, conn)
try
let rdr = cmd.ExecuteReader()
while rdr.Read() do
let gs = gsr rdr
let nm,defn,typeDesc = gs 0,gs 1,gs 3
let dir = sprintf "%s\\%s_%s" baseDir serverName databaseName
if not <| IO.Directory.Exists dir then
IO.Directory.CreateDirectory dir |> ignore
let filnam = sprintf "%s\\%s.%s.sql" dir nm typeDesc
printfn "Writing:%s" filnam
File.WriteAllText(filnam, defn)
rdr.Close()
with | ex -> printfn "Cannot access database %s: %s" databaseName ex.Message
conn.Close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment