I'm trying to restore a 500MB .sql file to SQL Server; I've tried running the script directly from a query page, but the memory isn't enough, so I tried with the sqlcmd command as follows:
sqlcmd -S <my server name> -d <my dbname>-U <user> -P <password> -i <pathfile> -a 32767
I've also tried with the -o command, too see if the log would report something useful, but from that command I didn't get any error.
Still, my database in SQL Server is empty, without any single table.
I'm running this on a Windows 10 system.
Any suggestions?
If you are using a SQL instance then the syntax will be like this:
sqlcmd -S <my server name>\instance -d <my dbname> -U <user> -P <password> -i <pathfile> -a 32767
Related
In SQL Server, if I want to run a script from the command line, I can do this
/opt/mssql-tools/bin/sqlcmd -S $DB_HOST -U $DB_USER -P $DB_PASS -d $DB_NAME -i myscript.sql
Is it possible to run just a single command without a script and get the results? For instance, if I just wanted to run
SELECT 1
How would I do that from the command line?
I think you want the -q switch:
sqlcmd -S localhost -U MyUser -P MyPass -d MyDb -q "SELECT 1"
Documentation is here
Given that you want to:
run just a single command without a script and get the results
Another possible answer would be to run:
sqlcmd -S localhost -U MyUser -P MyPass -d MyDb -Q "SELECT 1"
That is with a capital -Q.
This will run the command and then exit, allowing you to read the error code result.
The answer above uses lowercase -q, which will run the command but leave the sqlcmd prompt open and running.
Depending on what you want, the case of the -Q/-q argument matters.
I have a .sql file in my linux machine. I would like to connect to MS SQL database in the remote windows machine and run the .sql file in that database.
osql -S servername -U xx -P yy runs okay and returns a SQL window like:
SQL>
I can run individual queries using this. I have a .sql file with lot of sql commands which I need to run on a database called abc. How can I do that using shell command. The following doesn't work for me,
osql -S mssql -U xx -P yy -i /home/admin/Script.sql -D abc
I get the following error,
Illegal option -i
Syntax: osql -S server -U user -P password
The path of Script.sql file is correct as this opens the file - vi /home/admin/Script.sql. Not sure what the problem is. Any help would be appreciated
osql isn't working for me. I tried sqlcmd and it worked like a charm. Referred these links for ms sql tools installation - http://www.thesqlreport.com/?p=1494, https://sqlserveronlinuxbackup.com/sqlcmd-command-not-found-ubuntu/.
sqlcmd syntax:
sqlcmd -S 10.0.0.0 -U xx -P yy -d mydb -i /home/admin/abc.sql
I am trying to execute a large .sql file using sqlcmd. I can easily access the sa account through SSMS with the correct password. However, I am getting a
Login failed for user 'sa'
error when using sqlcmd.
Commands tried:
sqlcmd -S servername\SQLEXPRESS -U sa -P REDACTED -d dbname -i C:\sample.sql
osql -S servername\SQLEXPRESS -U sa -P REDACTED -d dbname -i C:\sample.sql
sqlcmd -S "servername\SQLEXPRESS" -U "sa" -P "REDACTED" -d "dbname" -i "C:\sample.sql"
What happens if you just login without the -d parameter and then type use dbname? I'm suspicious that the database you're trying to use can't be used by the "sa" for some reason. Is it offline?
I have a stored procedure through which I am creating a dynamic .sql file on the server and the file have successfully generated. Now I want o execute the same file through query in the same stored procedure .
I have go through the links and found the below lines useful
osql -D db_name -S server_name -U username -P password -i sqlfile
or
sqlcmd -D db_name -S server_name -U username -P password -i sqlfile
I have tried to implement the above but it is giving me the error of
osql is not a recognized option
Please tell me the correct way. Thanks
sqlcmd is a command line tool and cannot be called directly from inside a stored procedure.
Use xp_cmdshell to execute it:
EXEC master.dbo.xp_cmdshell ‘sqlcmd -D db_name -S server_name -U username -P password -i sqlfile’
I have a problem restoring a PostgreSQL DB which I have backup early using the following command:
pg_dump -i -h localhost -p 5432 -U Mark -F c -b -v -f "C:\Users\mchan\Desktop\MyDB\DBBackup.backup" mydb
Now When I try to restore this DB backup on (another machine) I got the following error:
pg_restore: [archiver] directory "C:\Users\mark\Desktop\MCHANBackups\DBBackup.backup" does not appear to be a valid archive ("toc.dat" does not exist)
Below is the command I used to restore the backup:
pg_restore -i -h localhost -p 5432 -U Mark -d mydb -v "C:\Users\mark\Desktop\MCHANBackups\DBBackup.backup"
Can someone please help me and tell me what I am doing wrong here?
I think you have used custom format and the backup is sent to normal text file. For restoring from text files, use
psql command: psql -e -d template1 -f "C:\Users\mchan\Desktop\MyDB\DBBackup.backup" Refer to this post on Postgresql's Community page.
Use the psql command as follows:
psql -e -U username -d databaseName -f "C:\Users\mchan\Desktop\MyDB\DBBackup.backup"
Username should be a role defined in the database. Ex: postgres
If the psql command prompts for a password and you don't know the password for the user, replace "md5" with "trust" in pg_hba.conf file under "/data" folder and restart "postgres service" for this change to take effect.
Once this change is done psql will not prompt for the password.
For more documentation on psql and additional options , refer to psql-doc.