SQL Server PRINT and Messages export to a .txt file - sql

When executing IF/THEN queries in SQL Server I am using a Print Statement to let the user/myself know what has happened.
Also when I run a query SQL Server mentions how many rows have been affected.
After searching I have only come across MySQL functions that allow PRINT statements to be exported via SELECT into OUTFILE. Does SQL Server have a way to send the PRINT statements and or messages to a .txt file for logging?

If you run your script as a SQL Agent job, you can specify Output file on the Advanced settings of a step.
You don't have to use SQL Agent as a scheduler, you can run the job manually, or use sp_start_job.

Related

How to log values of variables during a sql script execution

I have a complex sql script which I am running using db2 +v -txf sqls/connection.sql This script is part of a unix service which is running lot of other scripts as well. The script is querying a temporary session table so I cannot run the script manually (since the table is gone by that time). I want to be able to run the script as part of the service but would like to log the values of variables being calculated in the sql file. For eg: The sql script as the following line timestampdiff(1, char(max(END_TS) - min(START_TS))) as ELAPSED_TIME, I would like to know the values of END_TS and START_TS.
What I have tried:
I tried adding -v to the the db2 command and it printed the entire sql being executed but not the exact values.
If the Db2-server runs on Linux/unix/windows, you can use set serveroutput on; along with call dbms_output.PUT_LINE('......'); which lets you see what you logged as output when the script ends.
dbms_output.put_line docs.
The dbms_output module contains other useful services, and people familiar with Oracle will recognize it.
If the Db2-server runs on a Z/OS or i-series you should tag your question as db2-zos or db2-400 , because the answer can depend on the platform.

How to check if an SQL Script executed successfully in MS SQL Server?

I have created multiple SQL DB Maintenance scripts which I am required to run in a defined order. I have 2 scripts. I want to run the 2nd script, only on successful execution of 1st script. The scripts contain queries that creates tables, stored procedures, SQL jobs etc.
Please suggest an optimal way of achieving this. I am using MS SQL Server 2012.
I am trying to implement it without using an SQL job.
I'm sure I'm stating the obvious, and it's probably because I'm not fully understand what you meant by "executed successfully", but if you meant no SQL error while running:
The optimal way to achieve it is to create a job for your scripts, then create two steps - one for the first script and for the second. Once both steps are there, you go to the advanced options of step 1 and set it up to your needs.
Screenshot
Can you create a SQL Server Agent Job? You could just set the steps to be Step 1: Run first script, Step 2: run second script. In the agent job setup you can decide what to when step 1 fails. Just have it not run step 2. You can also set it up to email you, skip to another step, run some other code etc... If anything the first code did failed with any error message, your second script would not run. -- If you really needed to avoid a job, you could add some if exists statements to your second script, but that will get very messy very fast
If the two scripts are in different files
Add a statement which would log into a table the completion and date .Change second script to read this first and exit,if not success
if both are in same file
ensure they are in a transaction and read ##trancount at the start of second script and exit ,if less than 1
SQL Server 2005’s job scheduling subsystem, SQL Server Agent, maintains a set of log files with warning and error messages about the jobs it has run, written to the %ProgramFiles%\Microsoft SQL Server\MSSQL.1\MSSQL\LOG directory. SQL Server will maintain up to nine SQL Server Agent error log files. The current log file is named SQLAGENT .OUT, whereas archived files are numbered sequentially. You can view SQL Server Agent logs by using SQL Server Management Studio.

SQL Server - How to Copy a Stored Procedure to Another Database

I have a stored procedure with the exact same path on multiple servers (live, development and test) and I would like to modify/alter it simultaneously. I was thinking this would happen via altering one and then copying that and overwriting it over the other two servers.
Would this be easily achieved?
If you can connect to all the environments from a single SSMS (unlikely in many companies because of security), you can register all 3 SQL Server instances in a single group and execute the same script on all instances simultaneously
(refer to: https://msdn.microsoft.com/en-us/library/bb964743.aspx)
You can script out your stored procedure and use SQLCMD tool to execute it from file. Again, if you can connect to all instances from a single server, you can just duplicate the command in the script, but connect to multiple instances i.e. just use 3 SQLCMD lines
You can use any schema compare tool, Such as Red Gate SQL Compare
Also check alternative tools to Red Gate: Here
Also youo can check this opensource tool: Open DBdiff

Oracle SQL Developer how to dynamicly run sql scripts

I have the following challange;
I would like to execute a batch of *.sql files on one database. The sql files are assumed to be named in ascending order of their execution sequence. So the main sql script should do a 'dir /s *.sql', then start each of the found scripts in order.
Is this possible ?
Below is something I found for SQL Server, but I want something similar for Oracle SQL Developer.
http://pradeep1210.wordpress.com/2012/03/15/executing-a-set-of-sql-script-files-sql-on-a-group-of-sql-server-databases/
Thanks in advance.
Raymond
Create a folder for eg:Batch_Files in you local machine ,which will contain all the sql script that you want to execute ,
Then open you sql developer .Create a file called batch.sql in your Batch_Files folder .
In Batch.sql add the sql files that you want to execute in sequence.
#file1.sql
#file2.sql
:
:
#fileN.sql
These files contains the code that you need to run in sequence .This is a very basic example.You can do various changes according to your need ,you can add anonymous block to print something after execution of files .I have not tested this is SQL-DEVELOPER ,but i think this will surely work for you .

No way to execute SQL script from SQL Server Query Manager like #{file.sql} oracle sqlplus syntax?

Like the title says, in oracle you can issue the following command in SQL*Plus:
SQL> select something from anothertable; #sql
SQL> #{/home/me/somescript.sql}; #load sql from file and execute it
SQL> do something else in script; #other sql
Without having to file->open the sql script to load it to the UI.
Is there an equivalent in SQL Server Query Manager? I've stumbled upon many situation where i could have used it but i couldn't be able to find a way to accomplish it.
You're not really comparing like for like Tools here.
The equivalent tool to SQL*Plus in SQL Server is the SQLCMD Utility.
In particular you will be interested in the -i switch as this allows you to provide a .sql file as input.
Edit:
In response to your comment, you could look to use the system stored procedure xp_cmdshell to launch a prompt form within a T-SQL batch that allows you to use SQLCMD. Not the most elegant solution in my opinion but it should work.
If using latter versions of SQL Server (2005 & 2008), see if the :r command in SQLCMD works for you:
:r <filename>
Parses additional Transact-SQL statements and sqlcmd commands from the file specified by <filename> into the statement cache.
If the file contains Transact-SQL statements that arenot followed by GO, you must enter GO on the line that follows :r.
From sqlcmd Utility
Use isql utility http://msdn.microsoft.com/en-us/library/aa214007(SQL.80).aspx
issql ... -iinputfile
There is also SQLS*Plus tool that you can use to execute scripts within a script