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

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

Related

Updating multiple tables data from different databases having same column name

I have 11 databases in which I'm having tables contains User Details i.e. all employee details. There I have a column "Status"(which is 1 for Active and 0 for Inactive). I have a regular tasks for updating "Status" column value 0 or 1 for mentioned employees and for that, I have to go through all the databases then User table then I have to update. The same task i have to do for all the database and it consumes a lot of time.
If I will get a short Query or Procedure that I have to run once and will do all updation at once then, it would be a great help.
I see a couple of possible options.
You could build an SSIS package to connect to each database and do the necessary updates provided the criteria of which employees to update and what to update them to could be found within the database or some external source such as a text file.
Alternatively, you could use SQLCMD mode in SQL Server Management Studio and then within your SQL script use CONNECT command to switch to each server and database something like this...
:CONNECT Server1
USE Database1
--put your update SQL script
:CONNECT Server2
USE Database2
--put your update SQL script
...
These links provide some further information on using SQLCMD mode...
Connecting to multiple servers in a Query Window using SQLCMD
SQL Server SQLCMD Basics
Noel
As you mentioned, you have 11 databases.
Problem : First, you are using very bad approach for database design,
What really Happens : When you are using multiple databases and you need to check in every database, then the server needs to connect to different database again and again, which takes very more time compared to switching into the tables, because of connection handling.
Solution : In your case, you have only one option to connect different databases in loops and then run the query in the loop for every DB.
Suggestion : you should keep all the data in the same database, you can use an extra column in tables to keep track your data to different entities.

Can I use SQL Server Project to compare differences ONLY in certain stored procedures?

Let's say I have x number of stored procs in my SQL Server Project in Visual Studio 2015.
e.g. 5 stored procedures.
I want to do a comparison between these 5 stored procedures and what's on my database server. I just want to compare what differences there are between the 5 stored procedures in my project and of that in the database similar to a schema compare.
This project is not linked to any database at the moment because I am worried it will screw up the existing database. The existing database has hundreds of stored procedures but I'm only interested in finding out the difference between these X or 5 stored procedures.
Is this possible using SQL Server project?
There are many ways to compare store procedures in SQL Server.
compare scripts of two procedures
select procedure script by using the following query
SELECT OBJECT_DEFINITION(p.object_id) FROM sys.procedures p
& then compare with script of another procedure.
2 :
SELECT p.modify_date FROM sys.procedures p
select modify date of procedure and compare them.
I'd try to make a script to dump all procedure's definitions that you want to compare on the database to separate files named with the procedure's names and then I'd make another script to run a diff between the scripts dumped in a specific directory vs the scripts under version control and direct the output to a file. Maybe using PowerShell is a good way to go to do it or even batch. All that this script should do is to iterate through the directory contents of the dumped scripts and fetch the script with the same name on the version controlled directory tree, and the script would compare one by one directing the outputs to a file showing the diffs.

Run SQL script on multiple DB connections at same time in Oracle SQL Developer

I have 4 different database connections in Oracle SQL Developer. All of them have the exact same set of packages and procedures. Every time I change something in my scripts I have to run it on all of the connections one-by-one. Is there no way to run it on all the connections at the same time?
I'm afraid that you can only execute SQL developer queries on different connections using the GUI.
You should, however, be able to acheive what you want using SQLPlus instead.
You can use "create database link" function:
CREATE DATABASE LINK linkDB_1
CONNECT TO xxxx IDENTIFIED BY xxxx
USING 'xxxxx';
SELECT *
FROM tablename#linkDB_1;
DROP DATABASE LINK linkdb_1;
I tested on my SQL Navigator, and it works.

Sql: export database using TSQL

I have database connection to database DB1. The only thing I could do - execute any t-sql statements including using stored procedures. I want to export the specific table (or even the specific rows of specific table) to my local database. As you can read abve, DBs are on diffrent servers meaning no direct connection is possible. Therefore question: Is it possible to write query that returns the other query to execute on local server and get data? Also note, that table contains BLOBs. Thanks.
If you have SQL Server Management Studio, you can use the data import function on your local database to get the data. It works as long as you have Read/Select access on the tables you are trying to copy.
If you have Visual Studio you can use the database tools in there to move data between two servers as long as you can connect to both from your workstation.
Needs Ultimate or Premium though:
http://msdn.microsoft.com/en-us/library/dd193261.aspx
RedGate has some usefull tools too:
http://www.red-gate.com/products/sql-development/sql-compare/features
Maybe you should ask at https://dba.stackexchange.com/ instead.
If you can login to the remote db (where you can only issue t-sql), you may create linked server on your local server to the remote and use it later directly in queries, like:
select * from [LinkedServerName].[DatabaseName].[SchemaName].[TableName]

How do I make a script in SQL Management Studio 2005?

I have a table in an MS SQL Server db. I want to create a script that will put the table and all records into another db. So I right-click the table in Management Studio and select Create-To new query editor... but all I get is the table structure.
How exactly do I get the values too?
One of the things I really like about the tools for MySQL that SQL Server is missing out of the box to be certain.
You can use a script to do it however.
You might also want to consider using something like Red-Gate SQL Compare and Red-Gate SQL Data Compare. They aren't cheap tools, priced at $395 each (for the standard editions), but there are 14 day free trials available for download, and they make copying schema and data from one SQL Server to another very easy.
If both are on the same machine (or on different machines but the servers are linked)
you can create the table with the script you can generate automatically and do this to copy the data:
INSERT INTO [destinationdb].[dbo].[destinationtable] SELECT *
FROM [originaldb].[dbo].[originaltable]
(Prepend [servername] to the database name if you'll be using linked servers)
Another option is to enable xp_cmdshell (do with care, it's relaxing security constraints) and use the bcp command line utility from the management studio to create copies you can then import into the other database/server. You can do that directly from the shell as well and do not need to enable xp_cmdshell in that case, of course.
it doesn't really create a "SQL script" but it does the job :
select the database in the object explorer
right click
select import/export data
follow the wizard
at the end of the process you can save the "integration service package" to reuse it
later you can modify the details by opening the .dtsx
(it will take care of security, and won't cost one more penny, it's seems we have to compete with other answers :) )
hope it helps.