sqlcmd

How to run a sql script file using sqlcmd and output to both shell and file

我的梦境 提交于 2019-12-04 09:14:08
问题 I'm trying to run a sql script from a file using sqlcmd , using the following command: sqlcmd -S <server> -d <database> -i <input file> -o <output file> -U <user> -P <password> I run my sql file and output to a log file. The problem is this change the output of sqlcmd to the file.. and i want to get the output also to the shell. 回答1: @Noam, There's a standard program in UNIX world that reads from stdin and writes to stdout and to a specified file: the tee command. The following example lists

Remove column header from SQL Server query result

不羁岁月 提交于 2019-12-04 07:27:35
I want to remove column header from SQL Server query output. I did the search but not found any solution. I have a query eg. select cc.DepartmentID , cc.Name from HumanResources.Department cc When I run this query I am getting output like this. ID Name 12 Document Control 1 Engineering 16 Executive 14 Facilities and Maintenance 10 Finance 9 Human Resources I want to remove ID and Name (Column Header) from the output in SQL Server. I will run this query by script to generate csv file. Edit: When i run the query by script i got the csv file as output and it look like this. #TYPE System.Data

Sqlcmd - run sql query script without cleartext password?

给你一囗甜甜゛ 提交于 2019-12-04 05:42:24
问题 Hope you're having a great day thus far :) I'm using this script to automate an sqlquery, so that another team can use these logs. However, they are not permitted to have the login credentials for this database, and my current script uses a cleartext password. I suggested that we create a new database profile and limit the permissions of the profile, so that only the specific table can be accessed with read-only permissions. However, this isn't best practice, and my lead doesn't like the idea

Display DataType and Size of Column from SQL Server Query Results at Runtime

我与影子孤独终老i 提交于 2019-12-04 03:28:52
Is there a way to run a query and then have SQL Server management studio or sqlcmd or something simply display the datatype and size of each column as it was received. Seems like this information must be present for the transmission of the data to occur between the server and the client. It would be very helpful to me if it could be displayed. A little background: The reason I ask is because I must interface with countless legacy stored procedures with anywhere from 50 to 5000+ lines of code each. I do not want to have to try and follow the cryptic logic flow in and out of temp tables, into

How to format SQLCMD output

三世轮回 提交于 2019-12-04 02:22:57
I am using below command line to run a SQL query using SQLCMD sqlcmd -S Server -Q "select top 100 * From people" -d people -t 10 The table has 20 columns and when i look at output command line window wraps the text and makes it difficult to read. I want my results to be displayed the same way it displays in SQL Server Management Studio (properly formatted). I am not looking for any grids, but i need all my columns to be displayed in row 1 and the results properly beneath. Thanks in advance. Answer We can set the width of each column. C:/> sqlcmd -S my_server > :setvar SQLCMDMAXVARTYPEWIDTH 30

SQLCMD utility from BAT file - how to return ERRORLEVEL in case of syntax error

走远了吗. 提交于 2019-12-03 17:24:31
问题 How can I get %ERRORLEVEL% from SQLCMD utility when some of .sql files contains syntax error? These files create stored procedures. They don't invoke "raiseerror", but they can conatin syntax error and I need to terminate the process. But it always returns %ERRORLEVEL% as 0. I tried use -b, -V and -m (and their combinations), but nothing worked for me as expected. Here's the code fragment of my BAT file. REM process all sql files in "SQL\scripts" folder and subfolders FOR /R "SQL\scripts" %%G

How to change the port when calling sqlcmd

百般思念 提交于 2019-12-03 15:33:54
问题 I have this command sqlcmd.exe -E -i "C:\joe-db-scripts\joe-db-tasks.Install.sql" and I need to add the specific non-standard port of 32001. How do I do that? I was thinking of some flag like -p or something but I don't know the syntax. 回答1: According to this MSDN link, you'd use -S servername,32001 . 回答2: That's it: How to Connect to the Database Engine using sqlcmd. Something like this: sqlcmd -S ComputerA,1691 回答3: Following solution is working for me in SQL Server 2008 and above. sqlcmd

passing path to SqlCmd within powershell script

一个人想着一个人 提交于 2019-12-03 12:29:42
I'm trying to write a powershell script which will execute sqlcmd.exe to run a sql script. The script contains a SQLCMD variable which I want to pass in on the command line via sqlcmd's -v switch. The problem is that powershell is doing something weird when I pass a path as the value of one of my variables which in turn causes the SQL script to fail. For example I'm calling: $path = 'C:\path' sqlcmd -SMySQLServerInstance -i 'MySqlScript.sql' -v MyVariablePath=$path when run I receive a error which says: Sqlcmd: ':\path': Invalid argument. No amount of double or single quotes that I have tried

Docker + mssql-server-linux: How to launch .sql file during build (from Dockerfile)

佐手、 提交于 2019-12-03 12:21:41
问题 I am trying to create my own Docker image with MSSQL DB for development. It's based on microsoft/mssql-server-linux image. During the build I want to copy some .sql files into the container and then run these scripts (to create DB schemas, tables, insert some data etc.). My Dockerfile looks like this: # use MSSQL 2017 image on Ubuntu 16.04 FROM microsoft/mssql-server-linux:2017-latest # create directory within SQL container for database files RUN mkdir -p /opt/mssql-scripts # copy the

How to change the port when calling sqlcmd

六眼飞鱼酱① 提交于 2019-12-03 10:10:57
I have this command sqlcmd.exe -E -i "C:\joe-db-scripts\joe-db-tasks.Install.sql" and I need to add the specific non-standard port of 32001. How do I do that? I was thinking of some flag like -p or something but I don't know the syntax. According to this MSDN link , you'd use -S servername,32001 . That's it: How to Connect to the Database Engine using sqlcmd . Something like this: sqlcmd -S ComputerA,1691 Following solution is working for me in SQL Server 2008 and above. sqlcmd -S "hostname or IP,port number" -U SA -P password 来源: https://stackoverflow.com/questions/6099923/how-to-change-the