Windows 7 OSQL Batch - sql-server-2005

I have just got a new work PC from running Windows 7. I have installed SQL Server 2008 and I have a batch file that runs an OSQL command as follow:
osql -S MyServer -E -d MSDB -n -o results.txt -i MyScript.sql
For some reason it is giving me exception below:
Specified driver could not be loaded due to system error 126: The specified module could not be found. (SQL Server Native Client 10.0, C:\Windows\system32\sqlncli10.dll).
I am not sure what the issue is and I have even tried to run the batch file using 'Run as administrator'.

OSQL is being deprecated; I would consider converting your code to use sqlcmd or PowerShell. However if you're missing SQL native client you might consider adding it. You can download the sqlncli.msi file for your platform from this URL (more than halfway down the page).

Related

How can I import a SQL Server RDS backup into a SQL Server Linux Docker instance?

I've followed the directions from the AWS documentation on importing / exporting a database from RDS using their stored procedures.
The command was similar to:
exec msdb.dbo.rds_backup_database
#source_db_name='MyDatabase',
#s3_arn_to_backup_to='my-bucket/myBackup.bak'
This part works fine, and I've done it plenty of times in the past.
However what I want to achieve now; is restoring this database to a local SQL Server instance; however I'm struggling at this point. I'm assuming this isn't a "normal" SQL Server dump - but I'm unsure what the difference is.
I've spun up a new SQL Server for Linux Docker instance; which seems all set. I have made a few changes so that the sqlcmd tool is installed; so technically the image I'm running is comprised of this Dockerfile; not much different.
FROM microsoft/mssql-server-linux:2017-latest
RUN apt-get update && \
apt-get install -y curl && \
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - && \
apt-get update && \
apt-get install -y mssql-tools unixodbc-dev
This image works fine; I'm building it via docker build -t sql . and running it via docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=myPassword1!' -p 1433:1433 -v $(pwd):/backups sql
Within my local folder, I have my backup from RDS downloaded, so this file is now in /backups/myBackup.bak
I now try to run sqlcmd to import the data with the following command; and I'm running into an issue which makes me assume this isn't a traditional SQL dump. Unsure what a traditional SQL dump looks like, but the majority of the file looks garbled with ^#^#^#^# and of course other things.
/opt/mssql-tools/bin/sqlcmd -S localhost -i /backups/myBackup.bak -U sa -P myPassword1! -x
And finally; I get this error:
Sqlcmd: Error: Syntax error at line 56048 near command 'GO' in file '/backups/myBackup.bak'.
Final Answer
My final solution for this mainly came from using -Q and running a RESTORE query rather than importing with the file, but I also needed to include some MOVE options as they were pointing at Windows file paths.
/opt/mssql-tools/bin/sqlcmd -U SA -P myPassword -Q "RESTORE DATABASE MyDatabase FROM DISK = N'/path/to/my/file.bak' WITH MOVE 'mydatabase' TO '/var/opt/mssql/mydatabase.mdf', MOVE 'mydatabase_log' TO '/var/opt/mssql/mydatabase.ldf', REPLACE"
You should use the RESTORE DATABASE command to interact with your backup file instead of specifying it as an input file of commands to the database:
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P myPassword1! -Q "RESTORE DATABASE MyDatabase FROM DISK='/backups/myBackup.bak'"
According to the sqlcmd Docs, the -i flag you used specifies:
The file that contains a batch of SQL statements or stored procedures.
That flag likely won't work properly if given a database backup file as an argument.

SQL Dump File uploading

I have a bigger sql text file (powerpro.sql --> 350MB) to be uploaded to phpmyadmin database named powerpro-new.
So, I used the following command in windows
C:\mysqldump -uroot -p powerpro-new > "C"\powerpro.sql"
But, received the following error.
'mysqldump' is not recognized as an internal or external command,
operable program or batch file.
What may be the error. Can anyone help me pls?
You should navigate to your mysql installation directory in cmd. The default installation path (MySQL Server 5.6) is:
C:\Program Files\MySQL\MySQL Server 5.6\bin>mysqldump.exe -uroot -p powerpro-new > "C"\powerpro.sql"
If you don't know where you installed MySQL server you can search mysqldump file by using following command:
C:\>dir /S /P mysqldump.exe

In command line, putty remote shell doesn't work, but in GUI, it does work

Using the GUI putty.exe, I can connect to my windows server and once it is connected, i can type any command like rename file or mkdir folder and they all work
However, using command line such as
putty -load test -m C:\users\test.txt
or using the GUI putty, but add 1 command to remote command in SSH under Connection, then the command doesn't get executed.
Can anyone explain to me why this is happening or how can i fix this? I am using FreeSSHd on windows 2008 server.
Not sure if this helps, but try adding the /bin/bash directly after your command in the text file. It will keep the window open and you can see what the output of the shell would be if you ran it from the gui.
; /bin/bash
For example if test.txt is running a script
bash myscript.sh
bash myscript.sh; /bin/bash
This is assuming bash.

how to run tsql from a remote system in command prompt

I wanted to know how to run a TSQL statement from a remote system by connecting to the other system and run a TSQL statement through command prompt.
I have tried the following code but this code doesnt run on the other system.
PS: other system doesnt have sql server installed so it is compulsory that we use just command prompt to run from that system.
sqlcmd -S 100,1433\MSSQLSERVER -U sa -P abc -i C:\table.sql -o C:\output.txt
SQL Server 2000 - osql Utility
There's file called OSQL.exe which I am using for this job.
File comes from SQL Server 2000 (52 KB).
You can run it in CMD with simmiliar arguments to sqlcommand.
Remember to use GO after each batch
Example
Select * from Sales [enter]
GO [enter]

Need to run shell script on linux machine which uses sqlcmd but machine doesn't have SQL Server 2008 installed?

We have a scenario where we need to run a shell script on a Linux machine. This shell script internally uses sqlcmd but the machine doesn't have SQL Server 2008 installed.
Can anyone help me which tools/scripts/commmands that are required so that I can install/copy paste that client to run this script?
It would be helpful to provide link of such client so that i can download and give a try.
Shell script contains :
sqlcmd -S $Server -U $USER -P $PASSWORD -i AggregateChannels.sql
Thanks.
A simple Google gives me this:
The sqlcmd utility is available in the Microsoft ODBC Driver for SQL
Server on Linux.
See here for installation info.
You can now use a Linux version of sql tools in docker to connect to your SQL server.
See: https://hub.docker.com/_/microsoft-mssql-tools