Salvando y ejecutando scripts con SQL Server Express Utility
Una herramienta bastante útil para interactuar con SQL Server 2005 es SQL Server Express Utility (SSEUtil), cuyas características son las siguientes:
- Connect to the main instance or user-instance of SQL Server.
- Create/Attach/Detach/List databases on the server.
- Upgrade database files to match the version of the server.
- Execute SQL statements via the console (similar to SQLCMD) or the console window (UI).
- Retrieve the version of SQL Server running.
- Enable/Disable trace flags (e.g. to trace SQL statements sent to the server by any client app)
- List the instances of SQL Server on the local machine or on remote machines.
- Checkpoint and shrink a database
- Measure the performance of executing specific queries using the timer function (console mode).
- Create and playback lists of SQL commands to be executed by the server.
- Log all input/output.
Podemos descargarla desde aquí, y luego de instalarla tendremos el archivo ReadMe.htm para revisarla donde hay muchos ejemplos útiles.
SSEUtil funciona en dos modos: normal (soporta TQL Commands, ejecutados directamente sobre el servdidor) y extendido (soporta comandos extendidos que son interpretados primero por la consola). Los comandos extendidos son precedidos con un caracter "!".
El modo normal de esta herramienta trabaja con los siguientes comandos y opciones:
Comandos ::
- -a[ttach] <dbpath> [<dbname>] : Attach a database file to the server using the name if specified.
- -create <dbpath>|name=<dbname> [<dbname>] : Create a new database given the database path or name.
- -l[ist] : Lists all databases on the server.
- -d[etach] <dbpath
>|name=<dbname> : Detaches database(s) by path or name.
- -u[pgrade] <dbpath>|<directory> : Upgrades an individual database file or all database files in a folder.
- -t[race] +|-[<number>] : Enables or disables the specified trace number for all client connections. Exclude number to trace SQL commands. Output written to the server log.
- -childlist : Lists the child (user) instances of SQL Server.
- -c[onsole] : Console mode. Allows user to type SQL statements to run on the server.
- -consolewnd : Launches the interactive console window.
- -run <filepath> [<var1>=<val1>[,...]] : Runs a command file (SQL or extended commands) with the variables provided.
- -version : Displays the version reported by SQL Server.
- -listsrv [remote] :Lists the local or remote instances of SQL Server.
- -shrink <dbpath>|name=<dbname> : Shrinks the given database and runs checkpoint.
Opciones ::
- -m[ain] : Use the main instance. (Default is to use the child instance.)
- -child [<username>] : Connect to child instance for the current or specified user.
- -s[erver] <server>[\<instance>][,<port>] : Specify the server, instance name and/or port to connect to. e.g. -s .\SQLEXPRESS will connect to SSE on the local machine.
- -user <username> : Specify the user name to connect with.
- -pwd <password> | -pwd? : Specify the password to connect with, or prompt for it. e.g. -s mysrv -user sa -pwd? will prompt for a password.
- -timeout <seconds> : Specify the connection timeout in seconds. 0 for infinite.
- -cmdtimeout <seconds> : Specify the command timeout in seconds. 0 for infinite.
- -log <logfile> : Write all program input/output to the specified log file.
El modo extendido, es decir, modo consola, a parte de soportar los TSQL en modo normal, también trabaja con los siguientes comandos extendidos:
- !attach <dbpath> [<dbname>] : Attaches a database file to the server using the name if specified.
- !create <dbpath>|name=<dbname> [<dbname>] : Creates a new database given the database path or name.
- !detach <dbpath[* ]>|name=<dbname> : Detaches database(s) by path or name.
- !list : Lists all databases on the server.
- !timer : Turns the timer on/off. Will report the execution time of each command.
- !commandTimeout timeout_in_seconds : Sets the command timeout. 0 sets the timeout to unlimited.
- !run commandfile [var1=val1,...] : Run the given command file optionally passing in variable declarations.
- !consolewnd : Shows the interactive console window.
- !history show [count] : Shows all command history or the last 'count' commands.
- !history save filepath : Saves the command history to a file for later playback (see the 'run' command).
- !history clear : Clears the command history.
- !logopen filepath | !logclose : Opens/closes log file. All input/output is written to the log.
Unas de las funcionalidades de esta herramienta que más me gustó es poder salvar un script y reutilizarlo, para esto adjunté una base de datos al servidor SQL Express de una manera realmente sencilla y muy fácil.
C:\SSEUtil.exe -a C:\DB\PUBS.mdf
Ahora que tengo la base de datos adjuntada, paso a hacer una consulta de prueba, para esto primero cambio el modo normal a modo consola.
C:\SSEUtilexe -c
Console mode. Type 'help' for more information.
1> use "C:\DB\PUBS.mdf"
2> SELECT * FROM jobs ORDER BY job_desc,min_lvl
3> GO
Podemos ver los resultados de la consulta:
Bien hasta todo muy fácil, y lo que viene es aún más fácil!!! :). A continuación pasaremos a guardar la consulta anterior, para esto, escribimos lo siguiente y le damos enter:
1>!history save "C:\Comandos.txt"
Ahora ya tenemos el script salvado!. Pasamos a ejecutar dicho script nuevamente usando el modo consola de SSEUtil. Para esto, nuevamente, escribimos y le damos un enter!:
1> !run "C:\Comandos.txt"
Veremos que el script a sido leído y ejecuta correctamente. Esto es sólo una muestra de cómo podemos explotar esta herramienta cuando estamos usando SQL Server 2005 Express Edition, SQL Server 2000 y versiones anteriores. Espero sea de utilidad a los interesados!.
Percy Reyes.