how to run .sql file from SSMS? - sql-server-2008

Hi i am using SQL SERVER 2008 R2 and i wanted to execute prestored *.sql files in a folder and wanted to run in SSMS.
can it be possible and if possible can somebody please help me to get how can we implement that?

Ok If I'm clear with your question.
Go to cmd and navigate to the path where you have stored your .sql file.
In my case I've stored file on desktop with vish.sql name.
then sqlcmd -S DALVI1 -d cookbookdb -i vish.sql
Note (This is my configuration
DALVI1 is my ServerName
cookbookdb is database name
vish.sql is filename)
Modify as per your configuration.
For More Reference

Related

Executing binary SQL file using SQLCMD from WiX

I'm trying to install SQL script(SSDT) using SQLCMD - as this script contains to many SSDT definitions and cannot be run by the WIX SQL extension.
i want my SQL script file to be binary(as i don't want it to stay on target machine)
how can i set the SQLCMD command to use the binary script (with -i)?
p.s.
i tried this blog:
http://neilsleightholm.blogspot.co.il/2008/08/executing-sqlcmd-from-wix.html##
but this code don't shows the link between the binary SQL file and the SCLCMD command.
can someone help me with the correct code?
this is the code i used, which did not work for me
<Binary Id="CreateSchema.sql" SourceFile="..\SQL\CreateSchema.sql" />
<CustomAction Id="sqlcmd.cmd"
Property="sqlcmd"
Value=""sqlcmd.exe" -S [DATABASE_SERVER]
-i "[#CreateSchema.sql]" -v var=SYSTEM_USER -o [INSTALLDIR]installSql.log" />
<CustomAction Id="sqlcmd"
BinaryKey="WixCA"
DllEntry="CAQuietExec"
Return="check"
Execute="deferred"
Impersonate="yes" />
<InstallExecuteSequence>
<Custom Action="sqlcmd.cmd" After="InstallFiles">NOT Installed</Custom>
<Custom Action="sqlcmd" After="sqlcmd.cmd">NOT Installed</Custom>
</InstallExecuteSequence>
the log file showed that -i parameter did not had any file name value:
MSI (s) (4C:6C) [09:58:15:610]: Executing op: CustomActionSchedule(Action=sqlcmd,ActionType=1025,Source=BinaryData,Target=CAQuietExec,CustomActionData="sqlcmd.exe" -S (local) -i "" -v var=SYSTEM_USER -o C:\installSql.log)
That's not how <Binary> works. The [#FileID] syntax is used to dynamically use the at runtime installation full path of a component's file.
Binaries are used typically as temporary extracted files for custom actions or, in this case, sql files among other things.
Consider looking into the SQL Extension in wix. As a minimal example take a look at this code.
Add the sql namespace xmlns:sql="http://schemas.microsoft.com/wix/SqlExtension"
<Binary Id="CreateSchema" SourceFile="..\SQL\CreateSchema.sql" />
<sql:SqlDatabase Id="MyDB" Database="[DATABASE]" Server="[DATABASE_SERVER]" />
And in a component you can add
<sql:SqlScript Id="CreateSchemaScript" BinaryKey="CreateSchema" ExecuteOnInstall="yes" Sequence="1" SqlDb="MyDB"/>
Here is a link to the SQL Schema definition with all the available elements. I haven't done much with the SQL Extension so you may need to do some reading to get a better idea of what you will need to do to accomplish creating your DB on install.
As i mentioned i wanted to use both SQLCMD - since my SQL script is SSDT format, and binary file(so file will be deleted in end of the install).
After looking for answers i understood that i cannot use the WiX [#filekey], as binary file will not be extracted as long as there is no custom action that is running - using it explicitly.
So in the end i understood that the best way is to extract the binary file by my self.
the steps i used in one single custom action are:
extract binary SQL script from MSI binary table.
save this file locally
run SQLCMD with -i and new file path(the one i save to)
delete the SQL file
I encounter some issues, worth mentioned, if you save the file to INSTALLDIR than the directory may not exist at the tun time of the custom action, so consider save it to temp folder or to create directory beforehand.

PHP Mysql laravel 5.4 terminal issue

i am new to laravel i just figured out how to install composer laravel etc etc on my local machine MAMP on windows , Now i am confuse with the command on terminal which is
C:\project>mysql -uroot -proot
'mysql' is not recognized as an internal or external command,
operable program or batch file.
How can i fix this ?
setting Environment will solve the issue
Go to Control Panel -> System -> Advanced
Click - Environment Variables
Go to- System Variables find PATH and click on it.
add the path to your mysql\bin folder to the end paths. (ex: E:\xampp\mysql\bin) and add ; end of the line
Close all the command prompts you opens.
Re open and try it.
Setting the PATH to point to the MYSQL bin folder is normally the first thought, but I find that dangerous as things get left lying around when you change software.
I normally create a little batch file in the projects folder or in a folder that it already on your PATH, like this
mysqlpath.cmd
echo off
PATH=C:\mamp\path\to\the\mysql\bin;%PATH%
mysql -v
The mysql -v will output the mysql version number but it is a handy way of knowing that the correct folder has been added to the PATH. This adds the folder to the PATH but only for the life of the command window.
Then just run this from any command window when you want to use MYSQL from the command line
> phppath
You may also like to create one for PHP as well
phppath.cmd
echo off
PATH=C:\mamp\path\to\the\php\;%PATH%
php -v

mysql is not recognised as an inter or external command

I have installed xampp in c: drive.
I am able to run my sql from xampp,
but when I am trying to open it from cmd prompt its giving following error. Please help
C:\xampp\mysql\bin>mysql.ece 'mysql.ece' is not recognized as an
internal or external command, operable program or batch file.
I am not familiar with a MySQL command being called mysql.ece.
Should it be mysql.exe instead?
change mysql.ece to mysql.exe, you will see a file with mysql.exe in bin folder which is inside mysql folder.

mysql: SOURCE error 2?

When I tried to source for a particular .sql file, namely 'metropolises.sql' that I created and saved previously from a database, the following error is displayed:
Failed to open file 'metropolises.sql', error: 2
Any idea what went wrong?
Assuming you mean that you are trying to use the source command in order to execute SQL statements from a text file, the error number given appears to be passed through from the POSIX layer.
Therefore, using this resource, we can deduce that the error value of 2 means "no such file or directory".
In short, you got the path wrong.
Try providing an absolute path, as it's not clear what the current working directory will be in the context of your MySQL server. You may be assuming that it's the working directory of your shell, but it's not obvious that we should expect this to be true.
Just use the absolute path of the file and then, instead of using backslashes, use forward slashes.
Example:
with backslashes : source C:\folder1\metropolises.sql
with forward slashes : source C:/folder1/metropolises.sql
IF YOU ARE USING MYSQL INSIDE DOCKER
Note that if you are running MySQL inside docker then you must first copy the dump into your MySQL docker environment. To do that follow the steps below
First, check and copy the container ID for your MySQL docker by:
sudo docker ps
Copy the SQL dump file into your container using:
sudo docker cp /path/to/sql/file.sql MysqlDockerID:/
This will copy the dump file into the docker root folder if you want to copy the file inside any other directory/path inside docker replace the '/' after 'MysqlDockerID:' with the path you want appropriate one.
Now to interact with MySQL inside a running container run the following command:
sudo docker exec -it MysqlDockerID bin/bash
Now connect to the MySQL using the terminal by:
mysql -u yourUserName -p
This will now ask you for the password. Enter the correct password to proceed.
List the databases available by:
show Databases;
This will list out the available databases
Assuming your database name where you want to import dump to is 'MyDatabase'. Switch to that using:
use MyDatabase
Now you can import the file by typing:
source file.sql
Remember the above command works if you have copied your file into your root folder (using step 2). If you have copied it to any other path make sure you use that same path instead
Related issue I had getting error 2 running source command: filename must not be in quotes even if it contains spaces in name or path to file.
It's probably the file path to your file. If you don't know the exact location of the file you want to use, try to find your file in Finder, then drag the file into Terminal window
mysql> SOURCE dragfilePathHere
I first reach to the file
c:\windows>cd c:\akura\Db Scripts
c:\akura\Db Scripts>mysql -u root -p root
mysql>\. EXECUTER_NEW_USER.sql
here EXECUTER_NEW_USER.sql my file name
I've had the same error on Windows.
I solved it with (after on cmd: mysql -u root):
mysql> SOURCE C:/users/xxx/xxxx/metropolises.sql;
Be sure you type the right file path
On my windows 8.1, and mysql 5.7.9 MySQL Community Server (GPL),
I had to remove the ; after the file path.
This failed: source E:/jokoni/db/Banking/createTables.sql;
This Worked: source E:/jokoni/db/Banking/createTables.sql
(without termination, and forward slashes instead of windows' backslashes in path)
If you are using vagrant ensure that the file is on the server and then use the path to the file. e.g if the file is stored in the public folder you will have
sql> source /var/www/public/xxx.sql
Where xxx is the name of the file
I got this error in mysql command line using this query:
source `db.sql`;
I changed the above to the following to make it work:
source db.sql;
On my Mac, this is the only solution worked for me.
https://stackoverflow.com/a/45530305/5414448
1 - Download the .sql file and remember it's location.
2 - Open your mysql from command prompt or terminal.
3 - Create a database with the same name as that of the database present in the .sql file (create database your_database_name)
4 - Now exit out from the mysql command line client
5 - Now try and execute this command =>
mysql -u your_username -p your_database_name < your_sql_file_with_complete_location
example - mysql -u root -p trial < /home/abc/Desktop/trial.sql
here my .sql file is named trial and is present in the desktop, the database is also name trial
6 - You should now have your sql file imported to the corresponding mysql database.
I was having this issue and it turns out if you are using wamp server to run mysql, you have to use the file path within the wamp64 folder. So when the absolute path is: C:/wamp64/www/foldername/filename.sql
The path you have to use is:
www/foldername/filename.sql
May be the file name or path you are used may be incorrect
In my system i created file abcd.sql at c:\
and used command mysql> source c:\abcd.sql Then i got result
If you're on Debian 8 (Jessie) Linux, try to cd into the directory of the 'metropolises.sql'. Run mysql and execute SOURCE ./metropolises.sql;
Basically, try the relative path. I tried this and it works.
If you are running dockerized MySQL container such as ones from this official Docker Image registry: https://hub.docker.com/_/mysql/ You may encounter this issue as well.
For me it was because the file was located on a shared drive and it could not access, for some reason, to that path.
I cut the file and put it on my local drive in a path without spaces and it got resolved.
I got the same error when i used the command source and gave the sql file path by drag n dropping it.
Then I just had to remove those single quotes which appeared by default with drag and drop, a space before file extension and it worked.
soln:
source /home/xyz/file .sql ;(path and a space before file extension)
For Mac users, you can give the path as,
source /Users/YOUR_USER_NAME/Desktop/metropolises.sql;
*I have assumed that the file you need to source is in your desktop and the file name is metropolises.sql
If you type,
source metropolises.sql
then the file will be look in the default location,
/Users/YOUR_USER_NAME/metropolises.sql;
The solution for me was file permissions in Windows. Just give full control in the file to all users and it will work. After the import, get the permissions back to what it was before.
I had a problem on my Mac because one of the folder (directory) names in the path had a space in the name. BTW, the space was escaped by a \, but that was not understood by mySQL
solution - 1) Make sure you're in the root folder of your app. eg app/db/schema.sql.
solution - 2) open/reveal the folder on your window and drag&&drop in the command line next to keywork source (space) filesource. eg source User/myMAC/app/db/schema.sql
I get into this problem in my Xubuntu desktop. I fixed it by renaming all my files and folders so there is no space in the file path.
Passing the full path does not error, but if the folders have MySQL spaces it does not recognize the .sql file.
I have MySQL 8.0.21 on OS UNIX.
I've got the same error on Windows. I solved it running the code on MySQL command line
source c:\Users\xx\Downloads\data_file.sql
Remove spaces in the folder names of the path, It worked for my mac path.
(Eg: change the folder name MySQL Server 5.1 to MySQLServer5.1)
I also got the same message when I try from the MySQL console. However, when I open the command prompt and do the same steps it works.
C:\Users\SubhenduD>cd ../
C:\Users>cd ../
C:\>cd \xampp\mysql\bin
C:\xampp\mysql\bin>mysql -u -root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 68
Server version: 5.6.16 MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use balticktravels;
mysql> source balticktravels.sql;

Run exe file to downsize SQL Server to SQL Server CE

I'm trying to convert a SQL Server 2008 R2 database to SQL Server CE 4.
The only solution I can find is detailed here: http://erikej.blogspot.com/2010/02/how-to-use-exportsqlce-to-migrate-from.html.
However, I've downloaded the .exe and opened it - it just pops up with a command window which says something then immediately closes before I can read it; which I assume is just it installing.
However, if I then open a command prompt and type any of the instructions in that example and run e.g.
Export2sqlce "Data Source=(local);Initial Catalog=AdventureworksLT;Integrated Security=True" AW.sqlce
I just get "'Export2sqlce' is not recognized as an internal or external command, operable program or batch file."
What am I doing wrong?
EDIT: I needed to run the full path to the exe and the extension for it to work. Like this:
C:\Users\admin\Desktop\Export2SQLCE.exe "Data Source=(local);Initial Catalog=Northwind;Integrated Security=True" Northwind.sql
For any command lin utility, you must either locate yourself to the folder where it resides, use the full path or place the file in a folder that is in the PATH environment variable.