In a SQL Server 2017 or Azure SQL Database database, is there a way to identify rows in the history table that were deleted from the current table, without specifically executing a query that finds all primary key values in the history table that do not exist in the current table? I suspect there isn't but wanted to make sure I'm not overlooking anything.
Related
I need to move one table from one database (used for development) to another (used by user applications/the purpose of the database).
So far I do it with the select or into statement.
That works fine with just one drawback.
When doing this I lose my primary (PK) and foreign keys (FK).
Now I'm handling that by alter the tables after copying them, see the example below, but, it would be beautiful and more effective (and secure copying process) if I could do a copy of the tables with PK and FK intact all the way.
select *
into DB2.dbo.Ta
from DB1.dbo.Ta
alter table DB2.dbo.Ta add primary key (id)
alter table DB2.dbo.Ta add foreign key (Tb_id) references DB2.dbo.Tb
(Where the attribute Tb_id is the PK in table Tb, already existing in the database DB2.)
I work the Microsoft SQL server standard version 2017 and Microsoft SQL server express database engines with Microsoft SQL Studio Management version 2018.
I have a system-versioned table in sql server 2016. I want to store Modifier ID in its temporal (history) table while users are performing delete or update action on the table.
Is there any built-in solution by sql server 2016 to do that?
You can use Audit
Introduction to sql server 2008 audit
No it is not possible because the Temporal tables and their History tables has to have exactly the Same Schema i.e
number of columns
column names
data types
and even the ordering of the columns
So unless you are capturing the Modifier's ID in the Temporal tables itself, you cannot add this information to the history table.
How would you capture this Modifier's ID information in the Temporal table itself, well this can be handled on the application layer.
I have a unknown job that is deleting records from a table on SQL server 2005. We can't find it yet. Any of you have a clue of how can we know wish stored procedure or job is deleting this records in a X table?. Is there any way to know or record on database that can tell us specifically, "this sp or job delete records from that table in this date and hour"?.
2 different approaches:
Add a trigger (For Delete) to gather the name of the logged-in user: suser_sname() or the running app. You will need to create a table to store this information (commonly known as an Audit table).
Use the Profiler to watch all traffic on the database.
The trigger approach is better and you can even block/reject/abort delete commands on a per-table basis.
I am using MERGE REPLICATION on my server and now all tables have a rowguid, the last models generated before this change is working very good, but now the new table I imported (using database-first) get the rowguid and making impossible to update, I deleted this column in Model.edmx and I got this error.
Error 3023: Problem in mapping fragments starting at line 551: Column Location.rowguid in table Location must be mapped: It has no default value and is not nullable.
You can backup your database then restore it on another computer without preserving replication settings which will remove all replication traces inclusing the added rowguid columns, then you can generate your entity from the restored database.
Situation
I have 5 Access DB files, each one has 10 tables, 40 queries and 8 macros. All 5 Access DB files have same table name, table structure, same queries and same macros. The only different is the data contain in the table. If it matters, some tables on each database has rows between few hundreds to 100K+.
What I am trying to achieve
I am migrating these 5 Access DB files to single SQL Server (2008) database. Edit: After migrating, I do need to know which tables belong to which database since each original Access DB is associated with company's department so I need to keep track of this.
My Solutions or Options
Tables will be imported to SQL Server as tables. Queries will be imported as Stored Procedures. Macro will be imported as new Stored Procedures.
Import each Access DB's tables and queries to SQL Server DB and rename each tables and queries by giving them prefix to identify which tables belong to which database.
Same as #1, however, only import tables. As for the queries, only import one set of queries (40 queries) and modify them to dynamically select, insert, update or delete from the tables.
Import table A from 1st Access DB, table A from 2nd Access DB, table A from 3rd Access DB and so on, to one new table in SQL Server and give them unique identifier to identify which row of data belong to which database.
What do you think is the best approach? Please tell me if there is better way to do this than what I have listed. Thanks!
I would migrate them to MS SQL like so:
Import all tables from database 1 into corresponding tables from SQL Server, but add a new primary key with the name of the old one, rename the old pk and identifier for the database.
Update all foreign keys to the new pk field using the old pk and the identifier.
Repeat for databases 2-5
Either delete the identifier or keep it, depending if you need to know where the rows came from (same for old primary keys)
Only import queries/macros once, as they are the same.
When doing it this way, you keep the pk-fk relations and the queries intact and still know where the rows came from.
I would say number 3. You would get no duplication code and much easier maintenance.
One example of easier maintenance is performance tuning. You say the queries are the same in the 5 access DBs: say you detect one of the queries runs too slow and you decide that you need to create an index on an underlying table. In option #1 and #2 this would mean recreating the same index on 5 "twin" tables.
In access for each of these databases, you could assign each of the department field id (new field) with it's on identifier in a new table (each department has different value), and then add this value to each of the tables that is to be imported. Create a new table that has the department information in it, then create join table that connect these tables. Thus, each department is differentiated between each other.