Hello! Tell me, please, how to display the contents of the database table via the Windows command line with one command. You need to output without using sqlcmd.

  • Ie, you want to push sql request into standard cmd? - Yurii Manziuk
  • @YuriiManziuk, if possible, yes. The main goal is to display the contents of the table in some way, using the windows command line - Uchenitsa
  • without programming? Do you need a finished product? - MaximK
  • 2
    Those. Do you want to work with sql server from the console without using the standard console client for sql server? What for? - PashaPash
  • one
    You apparently used sqlcmd online, but you can execute sqlcmd scripts with one command if you specify the name of the script file with the parameter, or the script text itself, for example. sqlcmd -S hostName -d dbName -Q "select * from TableName" . However, this: " forbidden to use sqlcmd " is a rather strange requirement. How else? You can probably write a script for powershell. Or powershell is also prohibited to use? - i-one

2 answers 2

One command with sqlcmd :

 sqlcmd -S hostName -d dbName -Q "select * from TableName" 

If sqlcmd cannot be used, another option is to use powershell . To do this, create a text file with the extension .ps1 approximately the following content:

 $hostName = "hostName" $dbName = "dbName" $connectionString = "Server = $hostName; Database = $dbName; Integrated Security = True;" $connection = New-Object System.Data.SqlClient.SqlConnection $connection.ConnectionString = $connectionString $connection.Open() $cmd = New-Object System.Data.SqlClient.SqlCommand $cmd.CommandText = "SELECT * FROM TableName" $cmd.Connection = $connection $adapter = New-Object System.Data.SqlClient.SqlDataAdapter $adapter.SelectCommand = $cmd $dataTable = New-Object System.Data.DataTable $adapter.Fill($dataTable) > $null $connection.Close() foreach ($row in $dataTable.Rows) { for ($i=0; $i -lt $dataTable.Columns.Count; $i++) { $colName = $dataTable.Columns[$i]; $val = $row.ItemArray[$i]; if ([DBNull]::Value.Equals($val)) { write-host $colName " = NULL" } else { write-host $colName " = " $val } } write-host } 

which is then launched from the console with one command

 powershell -File <ИмяФайлаСкрипта>.ps1 
  • one
    For powershell, there is also Invoke-Sqlcmd, but it requires SQL Server installed. - PashaPash
  • @ i-one, thanks for the reply! I was given a hint that I need to write a small program using gscript or vscript, which will display the contents of the table - Uchenitsa

Bcp program

The bcp program is used to copy data between SQL Server and a user-formatted data file. Using the bcp program, you can ... export data from tables to data files.