Search code examples
sql-serversqlcmd

How to log in to Microsoft SQL Server 2008 from command line for the first time?


Just recently installed Microsoft SQL Server 2008. I want to run it from command line using following command:

sqlcmd [-U login id] [-P password] [-S server] [-H hostname] 

In this case, how can I find out the login id, password, server and hostname parameters? When logging in to the SQL Server instance through SQL Server Management Studio, in the opening dialog box, the logging in is done through Windows authentication, which does not require any password.


Solution

  • See SQLCMD utility on MSDN - it lists the many parameters:

    -E
    Uses a trusted connection instead of using a user name and password to log on to SQL Server. By default, without -E specified, sqlcmd uses the trusted connection option.

    The -E option ignores possible user name and password environment variable settings such as SQLCMDPASSWORD. If the -E option is used together with the -U option or the -P option, an error message is generated.

    So just define that -E command line switch and you're logged in using your current Windows credentials.

    sqlcmd -E -S server ......