Cari Blog Ini

22 Maret 2012

How to Connect to the Database Engine Using sqlcmd.exe

1. Run a command prompt

On the Start menu click Run.
In the Open box type cmd, and then click OK to open a Command Prompt window.
At the command prompt, type sqlcmd.
Press ENTER.

2. SQLCMD with Local Server

Microsoft Windows [Version 6.0.6001]
Copyright (c) 2006 Microsoft Corporation.  All rights reserved.

C:\Users\lig>sqlcmd -S (local)
1> USE master
2> SELECT top 5 name, number FROM spt_values
3> go
Changed database context to 'master'.
name                                number
----------------------------------- -----------
 (rpt)                                   -32768
YES OR NO                                    -1
SYSREMOTELOGINS TYPES                        -1
SYSREMOTELOGINS TYPES (UPDATE)               -1
AF: aggregate function                       -1

(5 rows affected)
1>


3. SQLCMD with Local Server Type 2


Microsoft Windows [Version 6.0.6001]
Copyright (c) 2006 Microsoft Corporation.  All rights reserved.

C:\Users\lig>SQLCMD -S (local) -d master
1> SELECT TOP 2 name, Number FROM spt_values
2> GO
name                                Number
----------------------------------- -----------
 (rpt)                                   -32768
YES OR NO                                    -1

(2 rows affected)
1>



3. SQLCMD with Remote Server

Microsoft Windows [Version 6.0.6001]
Copyright (c) 2006 Microsoft Corporation.  All rights reserved.

C:\Users\lig>SQLCMD -S XX.XX.XX.11\SQL2008 -U sa -P sa2008
1> USE AdventureWorks
2> GO
Changed database context to 'AdventureWorks'.
1> SELECT TOP 5 ProductID, Name, ProductNumber FROM Production.Product
2> GO
ProductID   Name                                               ProductNumber

----------- -------------------------------------------------- -----------------
--------
          1 Adjustable Race                                    AR-5381

          2 Bearing Ball                                       BA-8327

          3 BB Ball Bearing                                    BE-2349

          4 Headset Ball Bearings                              BE-2908

        316 Blade                                              BL-2036


(5 rows affected)
1>