#r "Microsoft.SqlServer.Smo.dll";;
#r "Microsoft.SqlServer.ConnectionInfo.dll";;
open Microsoft.SqlServer.Management.Smo
open Microsoft.SqlServer.Management.Common
let svr = Server(@"Z002\SQL2K8")
let db = svr.Databases.["pubs"]
for t in db.Tables do
for s in t.Script() do
printfn "%s" s;;
Notes
- The first three lines are not comments, they are used to resovle the assembly path and reference the SMO assemblies. These lines are specific to the interactive console if you're using Visual Studio you would add references as you would normally.
- F# is case sensitive
- Whitespace is important
- You use a dot before brackets to access an element, which is different than other languages
- Double semi-colons terminate a command in the interactive console
- The @ sign is used for verbatim strings (here-strings) -- used to escape special characters.
- The above example isn't very F#-like which favors functions and recursion over imperative looping, but this just a simple example
- Although it may not look like it, F# is strongly typed. It uses type inference to determine type. You can explicitly type items
EDIT Jan 24, 2010: Tony Davis blogged about this post in his article Life at the F# end providing a revised solution that is more F#-like as follows:
db.Tables
|> Seq.cast
|> Seq.collect (fun (t:Table) -> t.Script() |> Seq.cast)
|> Seq.iter (fun s -> printfn "%s" s);;
You'll need to read the article for an explanation of the F# code. Tony also suggests F# as a common scripting language for both developers and administrators. My thought on the subject is that Powershell is the common scripting language for administrators, but perhaps F# may have a niche use case for administrators needing better scale--I would love to see more practical examples of F# administration scripts. Be sure to read the comments section in which I respond with my reasons for exploring F# out of a need to achieve some concurrency missing from Powershell. Oh, and I also appologize for making someones' teeth itch with my use of imperative looping in F#