Attaching and detaching databases in Management Studio does not result in unloading old database and loading the new one - sql

I have two versions of the same database, say DB1 and DB2. DB1 is a copy made of the mdb and the log file a month ago. The database structure and data has changed since then. I need to switch back and forth between these two copies in SQL Server Management Studio.
The structure of the Customer table in these versions is different. So it is easy to see which version is loaded in Management Studio.
I detach DB1 and attach DB2 and do select * on Customer and see the structure still belonging to DB1. How do I switch to DB2 properly?
I am using the right use DB statement and have the right db selcted in the dropdown on the left hand side for selecting databases.

The drop-down at the top controls which DB you are using, if the DBs are on the same instance of SQL Server. If they are different instances, use the "change connection" button at the top left, then pick your DB from the drop-down.

Somehow Management studio was caching the location of the file. When attaching the database, I had to go to the "current file path", and edit the wrong path and point to the right one. This had to be done even when after clicking the add button I had already chosen the right mdb file with the right path.

Related

Associate SQL Script with Database

I have been using DBeaver as a replacement for SQL Server Management Studio, and I am loving it. The only thing I cannot figure out is how to associate a script with a database. For example, in SSMS I can right click a database (let's call it A) in the object explorer and click "New Query" (or ctrl+n) to open a script that is active within database A. If I open another script in a different database (B), that script is associated with the database B. When I switch back to the original script, I am back to working with database A without having to manually select from the database list or executing a USE statement. Obviously, switching back to the second script will make database B active again.
Unfortunately, in DBeaver, there appears to be only one active database for all scripts. Is there a way to set this up in DBeaver to act like SSMS in this manner?
Edit: DBeaver refers to individual databases as catalog/schema. That is what I am trying to associate with individual scripts.
I am using DBeaver community and i have using with multiple DB's. You have to just select Data base from drop down to change DB, it is for current script that open .
i current not have my company system connected , find one sample image from internet .
I don't know that DBeaver has this capability, but you could always Preface your scripts with the USE DBNAME statement...
UPDATE
Version 6.3.1 (2019-12-22) now supports this by default! Here is the first note of the description for 6.3.1:
SQL editor:
Active database/schema change now affects current editor only
Note: This does seem to have changed some behavior such as "Set active" from the database navigator as well as the "Auto-sync xx with navigator." These two used to take affect to the active database/schema. Now they are tied to the current connection.
Original Answer
For anyone who finds this in the future, I did find a work around that provides the desired behavior. The answer is to use projects. Unfortunately, this means creating a duplicate connection to the server.
There is also another catch here. If you want to set a schema for a specific script, you should avoid setting the schema via the "Database Navigator." This will, however, work if you want to change the schema for a script that is already in the active project. If the script is not in the active project, and you set an active schema through the Database Navigator, it will change the active schema for all open scripts associated with that server. To keep it simple, I try to avoid the Database Navigator all together.
All is not lost by avoiding the Database Navigator. By default, the "Projects" window is a tab right next to the Database Navigator. If you expand (click the + next to the name) [Project Name] -> Connections -> [Connection Name], you will have your list of databases/schema right there. Use this as your new Database Navigator and you are all set.

Creating a local database from server database in visual studio

I have a rather large database I am working with and I am about ready to break something. To prevent this affecting live data, how would I use the live database to setup a local database? Not sure if this is even possible but I do know you can setup a local db.
You can create a SQL Server Data Tools database project type, then right click the project file and do an "Import..." to import the database to your local machine. Then you can deploy the local DB and it will be available in the SQL Server Object Explorer locally. This way you don't have to install SQL server on your machine - everything's in Visual Studio. Hopefully you are developing with a small set of data locally.
Answer
Use Visual Studio's Data Comparison tool to synchronize data to your target database from your source after you've created the database (schema only, no data) in your local database server.
Steps
From the Visual Studio's SQL Server Object Explorer:
A. Create the local database
Add two SQL Server Objects: One that connects to your production server and one that connects to a local (development/testing) server. If you need help setting up a local server then take a look at SQL Server LocalDB
Add a New database in your local server to receive the data (don't over think this step).
B. Migrate the Schema
Right-click the source (production) database and click Schema Compare...
From the SQlSchemaCompare tab that opens, use the Select Target dropdown to select your local database as the target.
From the SQlSchemaCompare tab, click Compare.
Uncheck everything in the comparison results except for the Tables, Views, and Procedures (unless you know what you're doing) then click Update.
C. Migrate the Data
Right-click the source (production) database and click Data Comparison...
Follow through the prompts to select the Tables to migrate then click Finish.
From the SQlDataCompare tab that opens, review the comparison results (it should make sense to you) then click Update Target
That's it! Either your local database is ready with data, or you confused your target/source and wiped out all of your data in production. Either way, you're done for the day.

How to copy database in ssms Express version

I just now joined my first job (two days ago) and I am using Microsoft sql server management studio express version here. I want to create a new database by copying data, indexes and all properties from production database(We don't have a QA database) so that I can practice on it. How can I do that. I tried by taking backup of prod db and importing it into new database but it didn't worked and I got this error:
.
I am using Express version so no copy database option.
This might be a possible duplicate of this question: ssms copy database but I am using express version.
Can somebody tell me how can I do this.
Thanks
This is not hard to do in SSMS when working with SQLEXPRESS databases. I just executed these steps myself to verify that it works.
Create a full backup of the database you want to duplicate. To do this, right click on the database you want to duplicate, select Tasks, then Back Up .... Accept the defaults (or alter them if you wish) and click the "OK" button.
Now we will restore this backup to a new database. In SSMS, right click on the Databases folder icon. Choose Restore Database .... In the To database: text box, give your new database a name. I used "test". The name should not duplicate the name of any existing database. In the From database: dropdown, select the backup you created in Step 1. Click the "OK" button.
You should now have a copy of your original database, and it is named "test". To see it you may need to right click on the Databases folder icon and choose Refresh.
Hope it helps. Good luck!

Cannot create new SQL data tables in Visual Studio Express 2012

I have created a new SQL database in my Visual Studio Express 2012 website. This appears in the AppData folder. I can open the database in the Database explorer and now I want to add some empty data tables.
I right click on the Tables folder and select Add New Table. I get the screen (below). When I have finished I click Update and it appears to do something but then the table has not appeared in the Tables folder.
I have also tried clicking the save button, and saving the table manually in the AppData folder, but yet it still does not appear in the Tables folder.
What am I doing wrong please?
Here is the solution-
https://www.youtube.com/watch?v=HwA-tNvU3hw
Steps 1-Change the name of table [dbo].[table---(change this)]
Step 2-Don't save the table after creating them rather choose option update table and then update database.
Step 3-On refreshing the tables you will be able to see your table.
In my case, Visual Studio was saving the table to my documents. It should be writing the table to the MDF file. I used the designer to generate the T-SQL, then I copied the T-SQL right clicked on the tables folder, chose 'New Query' pasted the T-SQL commands, set the target database as shown in the image and ran the query. Finally!
In the 'New Query' window within VS 2012 Express, there is an 'available databases' drop down. Choose your database that is part of your solution and execute the T-SQL
Have you tried refreshing the tables list on the right hand side?
Make following change:
CREATE TABLE [dbo][change_this_name]
click the update button on top left corner.
I was up against this same problem in Visual Studio 2013 Community. The solution was to go to Tools -> Extensions and Updates and then download the SQL server compact / sql lite toolbox. VS2013 will prompt you to restart for the new extension to take effect.
After the restart, your databases will have a red "x" on them. Right click them, select modify connection and then take the defaults.
Visual Studio will then pull its head out of its hind parts, and those tables you've been saving that never appeared in the tables folder before will be auto-magically there.
Hope this helps.

How do I back up SQL 2008 tables & stored procedures?

Last night I got completely hosed by a worm from Dilbert.com (so be careful there). It is invasive enough where I am going to do a complete system restore. The only thing I need to save on the pc is a database (consisting of just tables & stored procs) in SQL 2008. How can I export them so that, once I restore, I can easily import them again. I thought about doing Access, but that is never completely clean (data types get changed and such).
You should just be able to back up the database from within SQL Server Management Studio. Right-click on the database in the Object Explorer and use the context menu to begin a backup operation.
In addition to the backup operation, which restores everything, you can also use the Generate Scripts option in SQL Server Management Studio to create a text dump of all the tables, keys, stored procedures, users, and so on. This option is available by selecting your database (left click once), right click to get the option menu, then choose "Tasks" (which opens a new submenu) and then "Generate Scripts...". Note that the Generate Scripts wizard is NOT the came as the "Script Database As" option. The "Script Database As" option is very limited, and won't give you many of the database entities you need for a complete restore of the schema.
Alternatively since you are going to take the machine down anyway, just detach the database. Then you can copy the file to an external hard drive or another machine.