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

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.

Related

Peculiar happenings in SQL Server

I have just started an internship in I've had to learn a lot on my own. I'm learning MS SQL Server, but having a strange problem. I have a DB that has four small tables. Each one has a script to drop the table, recreate the table (I've avoided FK dependencies for the time being), and execute a demo query.
Problem 1: When I first started SQL Server Managmenent Studio would execute the script, but one table didn't show up in the Object Explorer. If I tried to execute a demo query from the same .sql file, it executed with no problem. If I tried to access it from another .sql, the table didn't exist. After many times of successfully executing the script, it finally just showed up.
Problem 2: Similar problem. When I updated one table, the changes wouldn't be reflected in queries.
Problem 3: Queries will fail, but if I click execute again with no changes being made, it will usually work correctly.
Problem 4: When I use an alias for a field name, sometimes the alias is recognized and sometimes it isn't. I've literally had single query in which the alias would work in one place, but not work in another and I had to use a fully qualified name.
I've tried the refresh and refresh local cache, but those seem to have no effect. If I exit Management Studio, that seems to usually fix the first two problems.
Am I going nuts or am I just in the dark about some weird specifics of SQL Server?
First of all, when you make a schema change you need to right-click on the "Tables" node for the database in management studio and hit refresh.
If you change a column or something in a table, right-click the table and refresh.
The refresh local cache only updates the intellisense stuff, and the refresh only updates the GUI. If you modify a table with SQL and do not refresh it in the UI, the query can still use the updated table.
If you query fails, you either have a bad query or it's not pointed at the database or connection you think it is.
For aliases, there are places where they will not work (update statements, for example) but if you don't post queries where they don't work we cannot read your mind and tell you what's wrong.
If you have specific queries that are failing, post them.

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

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

SSMS - scripting data on change

Does anyone know of a way of scripting the data changes made via SSMS, e.g. update a table row and it shows the update SQL for that change, likewise for inserts & deletes?
I'm aware of being able to script out the entire changes using publishing wizard or using redgate software & I know you can switch on scripting for schema changes. However, I'm looking for something along the lines of: I do something in SSMS and I get a instant SQL statement telling me what I've done. I remember SQLYog doing something like this for MYSQL.
I was just looking for the same thing on SO with no luck, so clicked a few buttons in SSMS and found this:
"Generate Change Script" can be clicked after you've made changes in the SSMS designer. Upon clicking this button, a dialog appears that lets you view the SQL required to be run on your database for those changes.
If you check "Automatically generate change script on every save", then the same dialog will appear each time you click the save button.
You'll note that sometimes SSMS generates some ridiculously long SQL statements for seemingly simple changes. Unfortunately, even simple things like reordering/renaming columns cannot be expressed in standard SQL DDL statement - all SSMS can do is drop and recreate the table.

Can I get SSMS to switch to the Messages tab programatically?

I'm writing a fairly long running script, which prints progress messages as it goes.
However, SQL Server Management Studio by default shows the resultset tab, not the messages tab, so the user will have to click the messages tab after starting the script to see the progress as it happens.
Is there a way for my script to tell SSMS to show the messages tab automatically?
I know SSMS will remove the resultset tab once the script is complete, since it doesn't generate any resultsets, but I want the messages tab shown immediately, not at the end.
I'm not looking for a setting within SSMS, since this script will not be run on my machine once released.
There is not anything that you can include in your script to change the default behavior of the SSMS IDE, you might be able to handle it with a setting, but you noted that isn't a valid solution.
you should forget about trying to do this in SSMS and use the sqlcmd utility
Tutorial: sqlcmd Utility
Using SQL Server 2005 sqlcmd Utility

Visual Foxpro SQL Server - Can't find the Call to SQL server in Foxpro

My DBA's are saying my foxpro application or .DBC (Database container) are hitting SQL server but searching all the code can't find the SQL call (FMTONLY ON/OFF).
This is the SQL command being sent:
FMTONLY ON/OFF
Getting called 16260 times every few minuets?
Any ideas how to find this or what could be causing it, maybe my DBC file?
If you can't find it embedded in the .DBC, but not entirely sure its NOT in there, you can use a VFP tool to dump its contents to a .prg file... GENDBC which is in your installation folder of {VFP}\Tools\GenDBC\GenDBC.prg
Open your database, then run that program, it will cycle through all the tables, indexes, relations, connections, etc and generate the code corresponding to everything in it... You could then look at the output .prg file and see if something in there might be triggering what you can't see otherwise.