Showing posts with label sqlserver. Show all posts
Showing posts with label sqlserver. Show all posts

Saturday, November 03, 2012

Differences between isql, osql, sqlcmd and Invoke-Sqlcmd

What's the difference between isql, osql, sqlcmd, and Invoke-SqlCmd?
My GreatGreatGrandFather, Elijah Fincher, playing the part of "isql"
All four of these tools let you interact with SqlServer via the command line.
I'm not a big database guy, so I thought I was cool a few years ago moving to osql from isql. Wrong. The cool kids are on Invoke-SqlCmd
What's the difference:
isql - the grandfather, last used in SQLServer 2000. You should only be using this if you are working for some government agency.
osql - the father, introduced in SqlServer 2000, will probably be phased out soon. Use this if you work for a large corporation like GM.
sqlcmd - new kid, introduced in SqlServer 2005, used in current SqlServer insta
lls. This is what everybody else should be using.
Invoke-SqlCmd - the new cool kid, used inside PowerShell. Use this if you are really cool and have installed linux on your old computers at home.


Example using sqlcmd:
sqlcmd -S MyServerName\SqlExpress -E -Q "CREATE DATABASE Movies"
sqlcmd -S MyServerName\SqlExpress -d Movies -E -i "C:\workfiles\Movies_CreateTables.sql"

The "-E" option says to use trusted identity;
"-Q" the query to execute
"-d" the database to access
"-i" the command input file

Wednesday, August 27, 2008

SqlServer Timeout Expired Exception

One of our users emailed and said he got the following exception with a reporting query:
System.Data.SqlClient.SqlException: Timeout expired.  
The timeout period elapsed prior to completion of the operation or the server is not responding.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)

The exception just started happening because the database has slowly been increasing to where the report query fails to finish in a timely manner. The timely manner is 30 seconds, the default for .Net database connections.

You can increase the default time of 30 seconds by using the "CommandTimeout" field on the "Command" object as shown below:

using (connection) {
SqlCommand sqlcommand = connection.CreateCommand();
sqlcommand.CommandTimeout = 60; //default is 30 seconds
sqlcommand.CommandText = sqlText;
...

Don't be misled by the database connection string option, "Connection Timeout", e.g., "Data Source=myMachine; Integrated Security=SSPI; database=Northwind;Connection Timeout=60". That timeout is for getting the connection, not for how long it queries the database.