Stopping Powershell script to continue after an SQL error inside of the script fails? - sql

I have a PS script which connects to MS SQL and makes changes in the DB and if the SQL script fails, my ps script still continues with the next steps. So I wonder what is a good way to stop the script after it failed on SQL side?
My Powershell didn't recognize the normal abortion from SQL, so is the another way?

If you find a way to check if your SQL script worked or not (if you donĀ“t know how you should provide a little more info on what you are doing), you can use Break to stop your powershell script.

Take a look at $ErrorActionPreference. You can set this at a script level to Stop.

This is basic error handling. See if this help:
http://www.microsoftvirtualacademy.com/Content/ViewContent.aspx?et=3447&m=3443&ct=18350#?fbid=8N34OvP1Y5K

Related

Is there a way to set SSMS only allows to run when code is selected

I am pretty new to SSMS and I am starting to make scripts. Sometimes I click on Run without selecting my code and you can imagine that it has consequences on the database.
I wanted to ask you if there is a possible configuration on SSMS so that the run button is not accessible when we have code selected?
Kind Regards
Setting SET NOEXEC ON before the query guaranties that the SQL Server engine will check the syntax and object reference errors (during compilation), but it will not execute the actual query.
If you want prevent distract F5 or EXEC without have checked if you're executing something dangerous, put it before each script that you are making.

How to get the path of a directory of a script using sql query?. The SQL query is run by same script

I have a SQL query which is running by script while I am publishing the code in .net,
My problem is that, I need a SQL query to find the directory of same script.
Please help me to do so.
I don't think that the SQL Server client cares where the script is being executed from. Your best bet would be to figure out what the path of the script is before you execute it and pass it in as a parameter, or embed it in the script itself.

Running Sql Scripts On The Start Of The Application

Hi Guys i wanna know how to run few sql script on our application startup
i am using windows Forms to create a desktop application and have to use this approach for certain task.
database server used is MS SQL
we have to make a method which call the DB script then script will run and do his work. if your next process is depend on that particular script then you have to first call the script and do your work if your next action is not depend on that script so you can keep those script in thread and you can do your work so it will not take a time to load your start point

Find which line of the SQL is running in SQL Server Management Studio?

I am executing a very long SQL script in SQL Server Management Studio, normally it takes a few second to be done.
But this time it is taking forever and never completes. Other people running this script has no problem.
So I wonder if there is anyway I could debug in SQL Server Management Studio to see which line in this long SQL Script is currently executing and taking forever?
Easiest way is to fire up SQL Server Profiler and trace your SQL with that. As a bonus, you'll get a lot more useful information than simply which line is causing the problem.
If Profiler doesn't give you what you want, have a go with the Activity Monitor. Find your session in the Processes part, and then pull up the details to see the last T-SQ command batch.

See queries that hit SQL

Is there a way using sql 2008 Management Studio to look at the queries that hit the server? I'm trying to debug a program and I get messages like "Incorrect syntax near the keyword 'AND'". Since the queries are being dynamically generated it's a hassle to figure out what is going to the server.
Any help is appreciated!
There is a tool called Profiler that will tell you all information that you'll need. MSDN: http://msdn.microsoft.com/en-us/library/ms187929.aspx
I'm not aware of any method to do this using SQL Server Management Studio, but if you installed SSMS then you probably also installed the SQL Profiler. If you fire that up and run the TSQL_SPs profiler template, you can see every statement that's hitting the database.
Since the queries are being dynamically generated it's a hassle to figure out what is going to the server.
Why not just put the query that's generated into a message box, or print it to the console, or webpage, etc. ??
Trying to catch it at the DB server seems to be the long-way-around to debugging some simple ad-hoc queries.
Go to Management...Activity Monitor in the object explorer.
It's not live though, you will have to refresh it manually.
start up profiler from SSMS (Tools-->SQL Server Profiler), run a trace and select the T-SQL events
One option is to use SQL Server Profiler to run a trace. However, in some shops SQL Server permissions are set so only DBAs can run traces.
If you don't have sufficient rights to run a trace, then another option is to view the network traffic between the application that generates the SQL and box SQL Server is running on. WireShark works great for that.