SQLCMD Session Run SQL Script - sql-server

I'm still pretty new to SQL Server but have a ton of experience with Oracle. How do I run a SQL script while I'm inside a SQLCMD session? Is their something analogous to "SQL>#C:[mypath]\myscript.sql" (Oracle) for SQL Server?
I'm sick of opening scripts and running them using bloated SQL Server Studio.

sqlcmd -i file executes a file.
Can't recommend enough to check all the options in the docs.

I found what I was looking for. You can use this in SQLCMD and in SSMS Query as long as you open it up in "SQLCMD Mode"
:r [path]\myscript.sql
TransactSQL to run another TransactSQL script
Thanks for your responses. Much thanks.

Related

Deploy a Database from a batch

I'm trying to deploy a database (.bak / sql server 2012) by a batch(.bat) file , could you give me an example or how could I do that?
Best Regards.
You can use the SqlCmd command-line tool in a batch-file to do that. This command-line tool is installed when you install SQL Server Management Tools – Basic.
There is plenty of online information, this is a pseudo-code example I found:
SqlCmd -E -S <Server_Name> –Q “RESTORE DATABASE [Name_of_Database] FROM DISK='X:PathToBackupFile[File_Name].bak'”

Create jobs in SQL Server Express edition

I am using SQL Server 2008 (Express Edition).
I want to create a job which will delete all data from the all the table(>50) in the DB everyday at night 1:00.
Instead of Deleteting i decide to restore the DB from the Script.
It would have been easy by using SQL Server Agent, But this is limitation in SQL Server Express.
I figured out that we can create jobs "manually" by creating batch files and SQL script files, and running them via Windows Task Scheduler.
I have no clue what i have to write in bat file and sql file.This is my first time where i am working so deeply in SQL configuration. Can someone help please?
Name of the script which i need to restore is test.sql.
If any one has different approach , please share.
Thanks
Prat
Your batch files needs to look like this. Change the path to your .sql file and also put in the sql server info. You can read more about sqlcmd HERE. Also note the case on the switches -S and -i as it does matter.
sqlcmd -S <ComputerName>\<InstanceName> -i C:\test.sql

Migrate a SQL Server database to a lower version

I am trying to downgrade a SQL Server 2014 database to a lower version (SQL Server 2012) by using the task Generating scripts found when right clicked on the database.
After I make the settings in order to generate the script, the server does that but when I'm trying to open the file (the script made) on a lower version instance of SQL Server, I get the following error.
System out of memory exception thrown
Could anyone provide some help? Thanks!
As suggested by #usr you can run the script from the command line using sqlcmd:
sqlcmd -S myServer\instanceName -i C:\myScript.sql
Alternatively you can download a trial edition of ApexSQL or Redgate SQL Compare (assuming you have not tried them already) and script over the changes using these tools.
The script is too big for SSMS (a shame!). Run it using SQL Server command line tools.
I find it easier to do this by using Redgate SQL Packager or the two compare tools. They can execute enormous scripts.

How to import .sql file into SQL Server Express

I have a plain sql file with some SQL INSERT statements.
Is it possible to import it in my local SQL Server Express instance?
You can use Management Studio Express edition. You can download the latest version here - which will work against SQL Express 2005, 2008 and 2008 R2.
If you don't want to install SSMSE then you can use sqlcmd at a command prompt, e.g. something like this (assuming Windows auth and an instance called "SQLEXPRESS"):
sqlcmd -S .\SQLEXPRESS -E -i "C:\path\file.sql"
The easiest way would be simply open the file in the Sql Management Studio and run it. Since the target table is already created, of course.
You can open it via Query analyser and run
Here is the tool Sql_Server_Script_Executor
You can add single/multiple file/folder and your files will comes up in the list. Click the execute button and done
It contains three transaction modes.
1. Execute scripts within one transaction
2. Execute scripts on separate transaction
3. No transaction
All you have to do is open Microsoft SQL Server 2008 Management Studio.
Then use File -> Open.
Open the file from the proper location and you'll get all the SQL statements there. After that you can execute them.
Hope this helps.

Run sql script from console and not from query analyzer in SQL server

I have written an sql script for updating a database that runs in SQL server 2005.
I want to make those changes to the production DB server but I dont want to run the query from the query analyzer. Is there a way to run the sql script from a console?
create a batch file that points to the script or scripts that you created and run that batch file.
Here follow this tutorial link
You can use sqlcmd. It gets installed together with installing the Query Analyzer.
The sqlcmd utility lets you enter
Transact-SQL statements, system
procedures, and script files at the
command prompt, in Query Editor in
SQLCMD mode, in a Windows script file
or in an operating system (Cmd.exe)
job step of a SQL Server Agent job.
This utility uses OLE DB to execute
Transact-SQL batches.

Resources