Tutorial Gateway

  • C Language
  • Java
  • R
  • SQL
  • MySQL
  • Python
  • BI Tools
    • Informatica
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • QlikView
  • Js

Connect to SQL Server using sqlcmd utility

by suresh

In this article, we will show you, How to Connect to SQL Server using sqlcmd utility with practical examples.

Before you start using command prompt to connect with Sql Server. First use SQLCMD help command to see the list of available options

SQLCMD -?

Connect to SQL Server using sqlcmd utility 1

Connect to SQL Server using sqlcmd utility – Windows Authentication

You can connect SQL Server in multiple ways.

-- Connect SQL Default Instance using Windows Authentication
SQLCMD -S SERVERNAME -E

-- Connect SQL Named Instance using Windows Authentication
SQLCMD -S SERVERNAME\INSTANCE -E

-- Connecting Named Instance with port using Windows Authentication
SQLCMD -S SERVERNAME\INSTANCE, 1919 -E

In this example, we will connect with windows authentication

SQLCMD -S PRASAD -E

Connect to SQL Server using sqlcmd utility 2

Use QUIT keyword to exit or close the Sql connection

QUIT

Connect to SQL Server using sqlcmd utility 3

If your SQL server has the localhost instance then you can use that localhost to connect with server

SQLCMD -S localhost -E

Connect to SQL Server using sqlcmd utility 4

Let me execute one query. below query will return all the databases that are available in Server

SELECT name FROM master.sys.databases

Connect to SQL Server using sqlcmd utility 6

Connect to SQL Server using sqlcmd utility – SQL Authentication

You can connect SQL Server with Sql authentication in multiple ways.

-- Connect SQL Default Instance using SQL Authentication
SQLCMD -S SERVERNAME -U Username -P password

-- Connect SQL Named Instance using SQL user Authentication
SQLCMD -S SERVERNAME\INSTANCE -U Username -P password

-- Connecting Named Instance with port using SQL Authentication
SQLCMD -S SERVERNAME\INSTANCE, 1919 -U Username -P password

Let me connect with Sql authentication

SQLCMD -S PRASAD -U Suresh -P yourpassword

Connect to SQL Server using sqlcmd utility 7

Let me select the Column names from Employee table

USE [SQL Tutorial]
GO

SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'Employee'
GO

Connect to SQL Server using sqlcmd utility 8

Placed Under: SQL, SQL Interview Questions

Stay in Touch!

Sign Up to receive Email updates on Latest Tutorials

  • C Programs
  • Java Programs
  • SQL FAQ’s
  • Python Programs
  • SSIS
  • Tableau
  • JavaScript

Copyright © 2019 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy