SQL Server Management Studio console window - sql-server

I'd like to have a console window inside SSMS where I can run quickly a query to see what columns are in a specific table or test a query before writing it into a script and such things.
There are some addons for Visual Studio that provide cmd or powershell consoles where one can run commands. That's what I need in SSMS for SQL.
It'd be nice if that console would understand sql directly but I'd also be glad to have a powershell window embedded in SSMS if there's no other solution.

One trick that I like to use the default keyboard shortcut of Alt+F1 for sp_help. If you highlight a table name in a query window and hit that shortcut, it'll run sp_help with the table name as a parameter. This will give you all sorts of information on the table (columns, indexes, constraints).

Related

pgAdmin4: How do I edit the SELECT query generated by "View/Edit Data > All Rows"?

I recently migrated from MariaDB and there was a useful shortcut to construct a query in MySQL Workbench - you would right click the table and select "Select rows - Limit 1000". pgAdmin4 has an identical shortcut however I cannot edit the query in the Query Editor after the query has run. The query is grayed out/disabled.
Is there a way to "unlock" the input and make it editable, so I can adjust the query and re-run it?
Any help would be appreciated. Thanks!
The View/Edit Data page is just a limited version of the Query Tool, according to pgAdmin's documentation1:
The Query Tool and View/Edit Data tools are actually different operating modes of the same tool. Some controls will be disabled in either mode. Please see The Query Tool Toolbar for a description of the available controls.
So use Query Tool (accessible from the same context menu) instead of using View/Edit Data. You can edit the query there.
You can use Scripts > SELECT Script which opens the Query Tool with a pre-filled script that does the same as View/Edit Data > All Rows

SQL Server Mangement Studio Alter Script (2005)

Is it possible to generated ALTER sql script from the changes you perform in Server Studio. This is possible in different tools for mysql, basically you get the log of executed statements. Is similar thing possible here, or how do you go about finding out what the modification (ALTER) script is?
Thanks.
-- MB
You will be able to auto generate change scripts in SSMS. Take a look at this article, I believe it might be what you are looking for. http://www.mssqltips.com/tip.asp?tip=1723
from the article
As a part of my best practices, I always save the T-SQL scripts used for creation and modification of objects in SQL Server. When creating and modifying tables using SQL Server Management Studio designer it is easy to right click in the designer and select "Generate Change Script...", but is there a way to automatically script the creation and/or modification of tables made through of SQL Server Management Studio (SSMS) designer? This tip shows you an option that exists within SSMS to automatically generate these scripts for all table changes when using the table designer.
If you're altering a table (by right-clicking and selecting Design, then there is a button named Generate Change Script on the Table Designer toolbar.
If you're talking about changes made in a properties window, there is a script button at the top of each of those that can generate scripts for changes you make through the UI.
Not sure about SQL Server 2005, but in SQL Server Management Studio 2008 and later, you can definitely do this.
When you modify a table in the table designer (right-click on a table in your Object Explorer and pick "Design" from the menu), you can have SSMS generate the ALTER script for you by right-clicking in the designer and choosing "Generate Change Script":

Visual Studio DataSet Designer Refresh Tables

In visual studio datasource designer is there any way to refresh a table and its relations/foreign key constraints while keeping the custom queries?
The way I am doing it at the moment is removing the table and adding it again. This adds all the relations and refreshes all fields.
Also if I change a fields data type, is there a way to automatically refresh all the fields in the datasource? Again without deleting the table and adding it again.
Reason for this is because some of my TableAdapters have quite a number of complex queries attached to them and when I remove the table the adapter gets removed as well including all its queries.
I am using Visual Studio 2008 and connecting to a MySQL database.
Any1 have an idea?
Each table has a default query (The one on top with the check on it). When you dragged your tables in to the dataset to create the query, it wrote a SQL statement which it uses to schema your table. Keep that query simple, you might not actually use it in code, and you can always edit that query to update the table schema.
Every time you open the default query it connects to your datasource and allows you to select new columns that weren't in there before. If you want to update your existing columns, delete all the columns out of the table before you attempt to open the query. When you save the query, your updated columns get added back.
Make sure your connection string has permissions to view column information.
I reported this to MSFT but no response. The designer hangs all the time on the simplest of SQL statements. What I found that works for me is.
Add a new table to the designer.
Save it.
Shut down visual studio 2010.
Start VS 2010.
Add one or two more SQL statements and follow steps 2-4 again.
This is a pain in the neck but the only thing that stops Visual Studio dataset designer from hanging. I experienced in this same issue in VS 2008. I am connecting to Oracle but still shutting down VS and starting it back up works, but really, this is nonsense.
You can add/change/remove fields and relation ships, but i would suggest looking into NHibernate.
You should be able to right-click the dataset in solution explorer and select "Run Custom Tool" to refresh the table and it's query/relationships.
If that command is not there, check that the dataset properties has "MSDataSetGenerator" in the Custom Tool field.
Right click on your DataSet name and select Dataset Properties
Below the Query box you will see a button for Refresh Fields.
Click on Query Designer and the new field should show in your table list.

SQL Server, execute batch T-SQL script on multiple databases

Our SQL Server 2000 instance hosts several databases which are all similar, one for each of our client. When comes the time to update them all, we use Red Gate SQL Compare to generate a migration script between the development database and a copy of the current state DB of all the clients database.
SQL Compare generates a script which is transactional, if one step fails, the script rolls back everything. But currently our system uses a method that splits the script on batch separators (the GO statement) and then runs each command separately, which ruins all the transactional stuff. The GO statement is not supported when querying the database by programmation (in classic ASP)
I want to know how I could run that script (keeping the transactions) on all those databases (like 250 DB), programmatically or manually in a tool? In Query Analyzer, we need to select each DB and press Run which is quite long for the number of DB we have.
If you can use SSMS from SQL 2005 or 2008, then I'd recommend the free SSMS Tool pack
I use external sqlcmd command line tool. I have the same situation on the server I work.
I have the script in *.sql file and the list of databases on the 2nd file. I have small *.bat script which iterate through all the databases and execute script using sqlcmd command.
In more details I have like this:
DB.ini file with all the databases on which I want to deploy my script
sql/ directory where I store all scripts
runIt.bat - script which deploys scripts
The command line looks more-less like this:
sqlcmd -S <ComputerName>\<InstanceName> -i <MyScript.sql> -d <database_name> -T
In SQL Server 2000 it was osql utility
UPDATE
Red Gate now have a tool called SQL Multi Script, which basically does exactly what you want. I supports SQL 2000 to 2008 R2 and running queries on multiple databases in parallel which improve performance.
7 years later i had the same issue so many times so I made it and published the project:
TAKODEPLOY
Here are some features:
Get all databases from a single instance and apply a name filter. Or just a single direct connection.
Mix database sources as much as you want. Example, two direct and one full instance with or withut a filter.
Script editor (Avalon Text, same monodevelop uses)
Scripts are parsed and errors are detected before executing.
Scripts are 'splitted' by GO statements.
Save your deployment into a file
Get a list of all databases before deploying.
See in realtime what is happening (PRINT statements are recommended here!).
Automatic rollback to independent database if any error occurs.
Transparent Updates via Squirrel.
You can get it at: https://github.com/andreujuanc/TakoDeploy
Not sure if this will work, but try replacing the GO statements with semicolons, and running the entire statement in one batch.
If I recall, you can also create a script in SQL Compare to change everything back to the state it started in. You might want to generate both.
When I did this sort of deployment (it's been awhile), I first loaded to a staging server that was made exactly like prod before I started to make sure the scripts would work on prod. If anything failed (usually because of the order that scripts were run, can't set a foreign key to a table that doesn't exist yet for instance). I also scripted al table changes first, then all view changes, then all UDF changes, then all stored proc changes. This cut down greatly onthe failures due to objects not yet existing, but I still usually had a few that needed to be adjusted.

Copy table to a different database on a different SQL Server

I would like to copy a table from one database to another. I know you can easily do the following if the databases are on the same SQL Server.
SELECT * INTO NewTable FROM existingdb.dbo.existingtable;
Is there any easy way to do this if the databases are on two different SQL Servers, without having to loop through every record in the original table and insert it into the new table?
Also, this needs to be done in code, outside of SQL Server Management Studio.
Yes. add a linked server entry, and use select into using the four part db object naming convention.
Example:
SELECT * INTO targetTable
FROM [sourceserver].[sourcedatabase].[dbo].[sourceTable]
If it’s only copying tables then linked servers will work fine or creating scripts but if secondary table already contains some data then I’d suggest using some third party comparison tool.
I’m using Apex Diff but there are also a lot of other tools out there such as those from Red Gate or Dev Art...
Third party tools are not necessary of course and you can do everything natively it’s just more convenient. Even if you’re on a tight budget you can use these in trial mode to get things done….
Here is a good thread on similar topic with a lot more examples on how to do this in pure sql.
SQL Server(2012) provides another way to generate script for the SQL Server databases with its objects and data. This script can be used to copy the tables’ schema and data from the source database to the destination one in our case.
Using the SQL Server Management Studio, right-click on the source database from the object explorer, then from Tasks choose Generate Scripts.
In the Choose objects window, choose Select Specific Database Objects to specify the tables that you will generate script for, then choose the tables by ticking beside each one of it. Click Next.
In the Set Scripting Options window, specify the path where you will save the generated script file, and click Advanced.
From the appeared Advanced Scripting Options window, specify Schema and Data as Types of Data to Script. You can decide from here if you want to script the indexes and keys in your tables. Click OK.
Getting back to the Advanced Scripting Options window, click Next.
Review the Summary window and click Next.
You can monitor the progress from the Save or Publish Scripts window. If there is no error click Finish and you will find the script file in the specified path.
SQL Scripting method is useful to generate one single script for the tables’ schema and data, including the indexes and keys. But again this method doesn’t generate the tables’ creation script in the correct order if there are relations between the tables.
Microsoft SQL Server Database Publishing Wizard will generate all the necessary insert statements, and optionally schema information as well if you need that:
http://www.microsoft.com/downloads/details.aspx?familyid=56E5B1C5-BF17-42E0-A410-371A838E570A
Generate the scripts?
Generate a script to create the table then generate a script to insert the data.
check-out SP_ Genereate_Inserts for generating the data insert script.
Create the database, with Script Database as... CREATE To
Within SSMS on the source server, use the export wizard with the destination server database as the destination.
Source instance > YourDatabase > Tasks > Export data
Data Soure = SQL Server Native Client
Validate/enter Server & Database
Destination = SQL Server Native Client
Validate/enter Server & Database
Follow through wizard

Resources