Microsoft shipped the SQL Server 2005 command line tool “SQLCMD” along with SQL Server. This SQLCMD replaces the old ISQL command line utility and the OSQL utility. This series will illustrate the various ways of using the SQL Server 2005 command line utility “SQLCMD”.
When “SQLCMD” is run from the MS-DOS command prompt, it uses the OLE DB provider and when it executed from the SQL Server management studio, it uses .NET SQLClient for execution.
[@more@]Method 1
SQLCMD can be used as an interactive command line SQL tool. Let us see all the options that are available in the SQLCMD tool. Execute the following command in MS-DOS prompt.
The following results are returned. [Refer Fig 1.0]
Click for larger image
Fig 1.0
Method 2
SQLCMD can be used as an interactive command line SQL tool. The example below illustrates how to connect to SQL Server and query the server name and version number. Execute the following command at the MS-DOS command prompt as shown below. You will get the following results.
Note: HOME is the servername and –E means we are using windows authentication.
Method 3
The above command can be executed using SQL Authentication as shown below. This example shows how to connect to a named instance of the SQL Server using SQL authentication.
Note: HOME is the hostname, SQLEXPRESS is the instance name servername and –U and –P mean we are using SQL authentication.
Method 4
The following example illustrates how to connect to a named instance of the SQL Server using Hostname and PORT number.
Note: Home is the hostname, 7005 is the PORT number of the named instance SQLEXPRESS.
Method 5
The following example shows how to connect to a SQL Server instance using windows authentication and execute a command directly instead of interactive SQL.
Note: Home is the hostname, 7005 is the PORT number of the named instance SQLEXPRESS. [Refer Fig 1.2]
Fig 1.2
Method 6
The following example illustrates how to connect to a SQL Server instance using windows authentication, execute a command directly on to a database and get the results instead of interactive SQL.
Note: Home is the hostname, 7005 is the PORT number of the named instance SQLEXPRESS and test is the database name.
Conclusion
Part 1 of this series illustrated a few of the various ways of using the SQL Server 2005 command line utility “SQLCMD”. In future articles, we will discuss more of the SQLCMD utility features.
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/1697933/viewspace-896426/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/1697933/viewspace-896426/