How do you execute large SQL Server script file? - sql-server

I am trying to execute a large SQL Server script file from the command prompt as its not loading in Management Studio. I am using this command
sqlcmd -S .\SQLEXPRESS -U ttandel -P ''
-d [Zen.Databases.Suite]
-i D:\NewMachine\COM.B_Address.Table.sql
Note: password is blank. I have tried all the options for keeping password as blank such as ("",'', ) but nothing is working.
Can anyone please suggest how to do this?
From comment:
I tried with this
sqlcmd -S .\SQLEXPRESS -E
-d [Zen.Databases.Suite]
-i D:\NewMachine\COM.B_Address.Table.sql
This is throwing an error
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login failed for user 'NAPG\ttandel'..
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Cannot open database "[Zen.Databases.Suite]" requested by the login. The login failed..
My Server Security information as below.

The following SQL command will do the work.I have used it multiple times for installing 100 million to 200 million records in DB.
sqlcmd -S Krishneil-PC -E -i C:\Users\Krishneil\Desktop\Script.sql
change Krishneil to your suitability.

Change -d [Zen.Databases.Suite] to -d "Zen.Databases.Suite"

Related

Docker: How to run a sql script in SQL Server Dockerfile

I am trying to build a SQL Server container and then run some SQL scripts to add a user and make the schema by using docker build . and later docker-compose build -> docker-compose up
FROM mcr.microsoft.com/mssql/server
COPY all.sql all.sql
COPY auto-setup-db.sql auto-setup-db.sql
RUN /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P myPassword -i auto-setup-db.sql
this line^ fails with:
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2749.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..
The command '/bin/sh -c /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P myPassword -i auto-setup-db.sql' returned a non-zero code: 1
And I'm not sure what I'm doing wrong, does anyone have an idea?
EDIT: this is the relevant part of my docker-compose file:
version: '3'
services:
mssql:
image: mssql
build: ./e2e/mssql
container_name: mssql
environment:
SA_PASSWORD: "myPassword"
ACCEPT_EULA: "Y"
EDIT 2:
Running docker-compose up gives a lot of output from SQL Server ex.
2019-08-29 17:05:13.02 spid8s Starting up database 'tempdb'.
but I need to find a way to run the schema script in there so I thought docker-build would be the easiest place. I'm very new to docker
Trying adding double quotes around the file path and use the full path. Please see the example.
.
.
.
EXPOSE 1433
RUN mkdir -p /var/opt/mssql/database
WORKDIR /var/opt/mssql/database
COPY auto-setup-db.sql .
CMD /opt/mssql-tools/bin/sqlcmd -S localhost,1433 -U SA -P myPassword -i "/var/opt/mssql/database/auto-setup-db.sql" -o "/var/opt/mssql/database/auto-setup-db.log"

sqlcmd works in CMD and not in bash

I am having quite a few problems making MSSQL drivers work in Ubuntu. I have followed the following tutorial to make sqlcmd work in Ubuntu 16.04.
# In CMD:
sqlcmd -S my_server_name -U my_username -P my_password -d my_database
1> select name from sys.databases
2> go
After installing the same tool in Ubuntu, it seems to work, but it timeouts when attempting to connect to the same database:
# In Ubuntu bash
sqlcmd -S my_server_name -U my_username -P my_password -d my_database
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2AFA.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..
I have tried to change configurations of the database, such that the port is static at 1433, but still no luck.
Do you have any suggestions?
I have fixed the problem!
1. Install MSSQL drivers on Ubuntu
Follow this tutorial.
2. Ensure your port on the database is static
Follow this tutorial to set up a static port.
3. Identify the IP adress of your database
I had to call the following code on the database to get the ip-adress: local_net_address.
SELECT
+ CONNECTIONPROPERTY('net_transport') AS net_transport,
+ CONNECTIONPROPERTY('protocol_type') AS protocol_type,
+ CONNECTIONPROPERTY('auth_scheme') AS auth_scheme,
+ CONNECTIONPROPERTY('local_net_address') AS local_net_address,
+ CONNECTIONPROPERTY('local_tcp_port') AS local_tcp_port,
+ CONNECTIONPROPERTY('client_net_address') AS client_net_address
4. Connect to database (in Ubuntu bash)
Here are two examples with netcat and sqlcmd.
# Using MSSQL tool
sqlcmd -S my_server_ip_adress//my_server_name,my_port -U my_username -P my_password -d my_database
# Using netcat
nc -z -v -w5 my_server_ip_adress my_port

Can't remotely login to a SQL Server database using sqlcmd

I am trying to connect to a remote database using sqlcmd. I have tried something like this
sqlcmd -U <ComputerName>\Administrator -P adminpassword -S *.*.*.* -d mydatabase
But this is causing an error:
Failed to Login failed for user '\Administrator'
If I log onto the server where my database and run the command, it won't let me log in, either. But if I go into my SQL Server database and create a SQL Server user and run the same command with those details, I can log in.
But what I want to do is log in via the Administrator user from a different machine. Does anyone see what I'm doing wrong?
With Windows credentials you should use -E:
sqlcmd -E -S *.*.*.* -d mydatabase

MS SQL Server on Linux: set password using script

I have installed MSSQL Server 2017 on Ubuntu 16.04.
Now, I must change password.
I know that I can change password using the following commands:
sudo systemctl stop mssql-server
sudo /opt/mssql/bin/mssql-conf set-sa-password
But! I need to change password via script.
Ok. To solve this problem, I had installed "expect" and had wrote script:
#!/bin/bash
/usr/bin/expect <<EOD
spawn sudo /opt/mssql/bin/mssql-conf set-sa-password
expect "password:"
send "Pa$$wo4d!\r"
expect "password:"
send "Pa$$wo4d!\r"
interact
EOD
Double password - password+confirmation.
I have run the script:
ubuntu:~# bash 1.sh
spawn sudo /opt/mssql/bin/mssql-conf set-sa-password
Enter the SQL Server system administrator password:
Confirm the SQL Server system administrator password:
But it does not work. When I try to connect to mssql server, I got an error:
ubuntu:~# /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P Pa$$wo4d!
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login failed for user 'SA'..
OK, let's try a Windows authorization:
ubuntu:~# /opt/mssql-tools/bin/sqlcmd -S localhost -E -C
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : SSPI Provider: No
Kerberos credentials available.
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Cannot generate SSPI context.
I can't connect to MSSQL Server until I change my password!
And!
I need to change password via script only.
Can somebody help me please?
More easiest way:
sudo MSSQL_SA_PASSWORD=${Password} /opt/mssql/bin/mssql-conf set-sa-password
Solve the problem via script:
#!/usr/bin/python
import pexpect
ssh_cmd = '/opt/mssql/bin/mssql-conf set-sa-password'
timeout=30
child = pexpect.spawn(ssh_cmd, timeout=timeout)
child.expect(['password:'])
child.sendline('Pa$$wo4d!')
child.expect(['password:'])
child.sendline('Pa$$wo4d!')
child.expect(pexpect.EOF)
child.close()
if 0 != child.exitstatus:
raise Exception(stdout)

Connecting to SQL Server Express from within Docker Container gives an error occurred evaluating the password

I am trying to run a docker container from the Microsoft SQL Server Express image (https://hub.docker.com/r/microsoft/mssql-server-windows-express/)
For example I've tried the following commands (I'll use the -d flag once I've got it working)
docker run -it -p 1433:1433 -e SA_PASSWORD=Mfp_4871nJUj_1-23H -e ACCEPT_EULA=Y --name to_delete_1 microsoft/mssql-server-windows-express powershell.exe
docker run -it -p 1433:1433 -e 'SA_PASSWORD=Mfp_4871nJUj_1-23H' -e 'ACCEPT_EULA=Y' --name to_delete_1 microsoft/mssql-server-windows-express powershell.exe
(not the real password - I've tried many passwords since one of the suggestions for fixing the error I see below is to ensure that the password meets Microsoft's password policy. However for all passwords I get the same error)
I've also tried lower case sa_password
However, each time I try to connect with the sa account from within the container using any of
sqlcmd -U sa -P Mfp_4871nJUj_1-23H
sqlcmd -S localhost -U sa -P Mfp_4871nJUj_1-23H
sqlcmd -S localhost\sqlexpress -U sa -P Mfp_4871nJUj_1-23H
sqlcmd -U sa
sqlcmd -S localhost -U sa
sqlcmd -S localhost\sqlexpress -U sa
(entering password for last 3)
I get
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login failed for user 'sa'..
Looking at the SQL Server logs it shows
2017-03-13 21:25:52.32 Logon Error: 18456, Severity: 14, State: 7.
2017-03-13 21:25:52.32 Logon Login failed for user 'sa'. Reason: An error occurred while evaluating the password. [CLIENT: 172.22.251.6]
The main advice is to ensure the password meets SQL Server's password criteria but I think all of the passwords I've tried do.
I run Docker on Windows 10 Enterprise (build 14393.693).
I have Docker for Windows Version 17.03.0-ce-win1 (10300).
What do I need to do to be able to connect to SQL Server from within the container using the microsoft/mssql-server-windows-express Docker imamge?
Thanks
I don't know if you ever found the answer to this question. I have not tried connecting to the database from within the container, but I have connected to it from SSMS from outside the container. The key for me was to specify the password in double quotes in the original command. The final command looked like this:
docker run -d -p 1433:1433 -e sa_password="useComplexPasswordHere" -e ACCEPT_EULA=Y microsoft/mssql-server-windows-express
I was then able to connect to it through SSMS. Get the IP address from the docker container using:
docker inspect
Then log in to the database through SSMS like this:
SSMS Login info

Resources