Avoid replicating DELETE statements in SQL Server - sql

Consider the following scenario:
One PC is running an Access database. An old legacy script will copy over all the contents from this Access database to a SQL Server (A) over LAN. But before it does this, it will delete all contents from the destination database, so it doesn't have to deal with existing records. Previously existing records are never altered.
On the SQL Server (A), replication is defined and it acts as a publisher. It will publish/replicate the data to SQL Server (B) over WAN.
This all works very well, but the only problem I'm facing is when there's a problem during the copying of contents to SQL Server (A), SQL Server (B) will be empty or missing records, even the records that were replicated a long time ago.
There are two solutions I've already considered:
Adapt the legacy script to only copy over new records, since the updates are incremental of nature.
Configure the replication to avoid DELETE statements.
The first solution is in this scenario not possible. The application is closed-source, and there's really nothing we can change.
The second solution would be ideal, but (A) would try to replicate records that already exist on (B), and I'm not quite sure how to handle that.
Surely there's a sound concept to this problem, I just haven't figured it out yet..

In SSMS go to Replication -> Local Publications and right click on your publication and select Properties. In the Publication Properties window click on 'Articles' and select the relevant article. Go to "Article Properties" and select "Set Properties of Highlighted Table Article". In the article properties window, change the "DELETE delivery format" to "Do not replicate DELETE statements".
After the change click OK and you will see a prompt. As the article property has been changed the subscriptions need to be reinitialized. Click "Mark for Reinitialization" which causes the snapshot to be applied to the subscriber.
In SSMS, navigate to Replication and right click and select "Launch Replication Monitor". Go to your publication and click View Details to see the snapshot progress.

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.

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.

Creating a blank copy of a SQL Server 2012 database on the same Instance

I'm trying to determine the best way of creating a blank copy of a SQL Server 2012 database, renaming it & placing it on the same server instance. I could restore/rename a backup copy, and then delete all the data, but there are quite a few tables/views, stored procedures, triggers, etc.
Are there any drawbacks with the "generate scripts for database objects" wizard? Just trying to figure out the most efficient way to create a blank copy & would appreciate anyone's experience/wisdom on the topic.
I am running a SQL Server 2012 Enterprise edition. We have a database that our colleagues like, so we are planning to create a copy & populate with their data instead of our data.
Go to Object Explorer ,Right Click the database you want to create a blank copy of.
Right Click --> Tasks --> Generate Scripts
Select the entire Database, you also have the option to select specific objects, In advance option select schema only.
I would prefer this method over any other, as Restoring a Copy of database and then deleting data means doing a lot of work which was never required to begin with. Backing up data that was never required, restoring unwanted data and then finally making sql server to delete it, a lot of unnecessary work.
Another reason why scripts are better over other options, These scripts will use fresh pages to write all of this data. Imagine you having to write something on a brand new note book as compare to a note book where you had to erase first and then use it to write data on it.

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!

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

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.