T-SQL - Scripting variable not defined - sql

I'm new to T-SQL and I'm trying to backup my databases (using SQL Server 2008).
When I try to run the script via sqlcmd -i inputfile I got this error messages:
'DATE' Scripting variable not defined.
The problem is I have a line like this:
...TO DISK = "FileName_$(ESCAPE_NONE(DATE)).BAK" ...
With a date in a filename, it will prevent it from replacing my old backups.
If I run it in management studio, it works, but if I run it in command line with the sqlcmd -i command, then it doesn't work.
EDIT:
I looked at the job history and I saw this error message:
"For SQL Server 2005 SP1 or later, you must use the appropriate ESCAPE_xxx
macro to update job steps containing tokens before the job can run"
I don't quite understand what that means. I've already used $ESCAPE_NONE(DATE), what's wrong?

Old question I know but this is one of the first results and if anyone else has the same problem the answer isn't particularly easy to find.
Including the -x switch to disable environment variables fixed the problem for me;
sqlcmd -x -i inputfile

If you're trying to backup your sql server databases and append the date to them using sqlcmd there's an easy thing you can try.
First, create the sp called sp_BackupDabases which you can find here:
http://support.microsoft.com/kb/2019698
You can invoke it from sql cmd using some command like this:
sqlcmd -U Damieh -P ilovechocolate -S (local) -Q "EXEC sp_BackupDatabases #backupLocation ='C:\MyBackups\', #BackupType='F'"
I'm sure you know this already, but just in case: -U is the user, -P is password, -S is server, and -Q is query. You can either backup all of your databases or some of them, there are parameters for that. You can find the stored proc parameters details on the same link I gave you.
The date will be automatically appended and you can play with the sp's code if you want it in a different place/way/format. I use this regularly on servers which don't have a non-express sqlserver (meaning that I can't schedule backups without using a .bat and task scheduler) with great success.
I apologize if this wasn't the answer you were looking for =). Have a nice day!

I know I'm coming along late on this thread, but you can use the following:
SQLCMD -S YourServer -E -d YourDatabase -i YourScript 2> nul
That will send the StdErrorOut to the bit bucket.

Related

Unable to run .sql file in SQL Server

I have a .sql dump file 20 gb and I am trying to run it on Mysql workbench using run script and after successful execution, using SSMA I'll migrate the data from Mysql workbench to SQL Server. I have migrated the data this way many times successfully however for 20 gb file it seems very time-consuming. Please let me know if there is any alternate way to achieve this quickly. I have followed the following link:
Steps to migrate mysql tables to sql server using SSMA!
From your Title "unable to run .sql file in SSMS" and "I have a .sql dump file 20 gb" are you trying to open a 20GB .sql in SSMS? That's never going to work. SSMS is a 32bit application, so the maximum addressable memory is 2GB. If you want to run your .sql file, I suggest using sqlcmd.
Open up Powershell, and then run the command below replacing the appropriate parts:
sqlcmd -S {Server Name/ServerIP} -U {Your Login} -i {Your full path to your script}
You'll be prompted for your password and then you the file will be run. So, as an example, you might run:
sqlcmd -S svSQL2017 -U Larnu -i \\svFileServer\SQLShare\Scripts\BigBatchFile.sql
If you are using integrated security, then don't pass the -U parameter for the command.
Edit: This answer is no relevant to the OPs question, as they were using "SSMS" as a synonym for SQL Server, which it is not. I have left this here for the moment so the OP can review my comments, and I will likely remove this answer at a later point.

How to run sql queries in SQL Developer or TOAD and extract result using bat file

Is there a way through which i can execute SQL query in SQL Developer/TOAD and extract the result in any format using .bat file.
I m working on a client machine so using any other software other than SQL Developer/TOAD is not a option.
Please suggest how to create a bat file for the same.
If SQL Developer is installed I venture to guess so is SQL Plus. SQL Developer is simply a graphic interface to the database connections. Try opening a command window, I assume you can since you would like to run .bat programs, and typing in sqlplus. If this comes back with version numbers and a prompt for a user name you should be able to use this for your script.
See this answer on Stack Overflow for more tips on how to run .bat programs from SQL Plus with native SQL Plus spooling.
What version of Toad? If the version of Toad you have has the Automation Designer then you can setup an action to export query results to many different formats. See my answer in this question for steps to export query results to XLS. In step #3 you can choose other formats. Your configured actions can be scheduled or executed by .bat file. Toad's help covers command line execution of these actions.
You can put the following types of sqlcmd statements into a batch file. After running the results are saved to a txt file. This example executes a SQL file already created and saves the results to a txt file.
Step 1: Create SQL file which you want to execute.
Step 2: Execute following sqlcmd command on prompt:
sqlcmd -i SQLFile.sql -S ServerLocation -E -o File.txt
If you are using username and password run following script
sqlcmd -i SQLFile.sql -S localhost -U username -P password -o File.txt
You can run something like this for SQL plus:
sqlplus user/pwd#mydb #SQLFile.sql > File.txt

Schedule .sql file

I need to schedule a .sql file. (that executes some queries)
I have sql express so I can not use SQL Agent because I have SQL Express so I wish to use Windows scheduler.
But the schedule just opens the file without execute it.
How can I solve it?
You'll have to call SQLCMD.EXE together with the related parameter.
For example:
SQLCMD.EXE -S .\sqlexpress -E -i C:\temp\myfile.sql
For a detailed explanation plese have a look at this blog post

SQL Server 2008 Job based on changing Stored Procedures

I have looked through the SQL Server questions and answers and I didn't see an answer to this one, if it is out there and I've missed it, please let me know.
Here's the situation:
I write stored procedures and views that are then run as reports (using Crystal) - this is not the problem. Before I am able to release the reports into Production, I need to have the end users run the reports and check them for errors, etc. In a perfect world, I would have a frozen test environment, but I don't live in a perfect world. Every night everything I place into my test environment is wiped out and every morning anything that is in end user testing needs to be re-added. This means that when I come in the first thing I do is run all of the stored procedure, along with a script that unhides the reports in the program we use.
What I'd like to be able to do is to write a package that would find all of the stored procedures in a folder and execute them to add them to the database and, then, run the script that unhides the reports.
I know how to set up an SSIS package to run a stored procedure, but I don't know how to set one up that would run an ever changing list of stored procedures. Is this even possible? And, if it is, how do I go about starting this up?
I should note that while I have more then 10 years of query writing experience, I haven't used VB since VB 6.0 and I very new to the SSIS and SSRS world.
Thanks in advance!
Good old nt shell will do the trick. Run this statement in the folder containing the files.
for %A in (*.sql) DO sqlcmd -i %A -S <myServer> -d <myDb> -E
if you want to include it in a batch file it could look like
#echo off
for %%A in (*.sql) DO sqlcmd -i %%A -S <myServer> -d <myDb> -E
sqlcmd -i script_to_update_config.sql -S <myServer> -d <myDb> -E
This actually sounds like it may be more of a deployment issue than a SQL one. Take a look at Jenkins CI. I believe it's mostly used for code build and deployment, but it can also be used for any automated task.
If you had one SQL file that listed all the changed procs and their associated files you can use that single script to run all the others http://www.devx.com/tips/Tip/15132. For that matter you could just use a scheduled task to run it every morning.
Adding one more step, you could build the file based on the contents of a folder (using a little Powershell script or the like).
I'm not sure trying to do this all within an SSIS package is the right tool for the job.

How to run .sql file from a batch file?

I am running sql server 2008 express and i need to schedule some stored procedures to run nightly...so i have built out these .sql files which i would want to run from .bat file...i need to know the command to execute these .sql files one by one and store their results i guess...can anyone help me out?
I answered this in this other question:
You should invoke the sqlcmd command-line tool from your batch file. Assuming your sql file is "backup.sql", the command line would be something like:
sqlcmd -E -S yoursqlinstance -i backup.sql
-E uses trusted connection, replace with -U and -P if you need to specify a SQL username and password. See also this article with examples.
See the sqlcmd utility:
http://msdn.microsoft.com/en-us/library/ms165702.aspx
This allows you to run sql scripts from the command line
osql:
http://www.di-mgt.com.au/osqlUtility.htm
I don't use SQL Server, but a batch file is just a list of DOS commands. So whatever you use to execute SQL files from the commandline can be used in a batch file.
A quick google search turns up:
sqlcmd -i <inputfile> -o <outputfile>
Hope this helps you :
sqlplus UserName/Password#DataBase #C:\sqlFolder\sqlFile.sql
P.S : Don't forget to add the command "commit;" at the end of sql file (sqlFile.sql), this command order Oracle to save performed changes in database