Running queries with OSQL.EXE

Sometimes we can run SQL queries with a tool called OSQL.EXE . This is available in the below path by default based on SQL Server version.

D:\Program Files\Microsoft SQL Server\140\Tools\Binn\OSQL.exe. 

See the below image.











By using the OSQL tool I have a run a SELECT query. 

-S ServerName or InstanceName

-i  Path in which I saved the .SQL query which contains SELECT command. And I      saved it in a  folder of a drive.

-E  Windows Authentication

-U  SQL Authentication.

-P Password need to be provided if you choose SQL Authentication(-U). Below case I have chosen Windows Authentication

And my select query looks below. And I save this to AdventureWorks2012 folder of D drive. 

USE AdventureWorks2012

GO

SELECT top 10 * FROM [HumanResources].[Department]

Go to Run-->Cmd--> and here is the query and double quotes("") are compulsory





If you want to export above result set to text file pass a parameter called -o and provide the path. See the below screenshot


Comments

Popular posts from this blog

System.Data.SqlClient.SqlException (0x80131904): Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out

Pre login Handshake or Connection Timeout Period

Transparent Data Encryption(TDE) with Master Key and Certificate in SQL Server