Audit two different tables in SQL Server - sql-server

I have a form with data. Any changes or insertion , those data should be updated in tow different tables like name, salary in one table and address, mail id in another table.
Like the example above i have several columns in both tables.
Now i want to audit the table. So i think i have to create a view for the two tables and set up a trigger for the view. Is it correct?.
And also i need to know only the affected columns. How to get the only affected columns?
Please suggest me a solution.
Thanks!!

There are lots of ways to let the system handle all that grunt work for you - depending on the SQL Server version you're using:
How to: Use SQL Server Change Tracking (as of SQL Server 2008)
Introduction to SQL Server change tracking
Understanding SQL Server Audit (as of SQL Server 2008 R2)
Articles for SQL Server Auditing (various versions)
If you really must handle all the work yourself, you need to get familiarized with triggers - read up on them in Data Points: Exploring SQL Server Triggers.
Inside your trigger code, you have two "pseudo-tables":
Inserted is the table holding the values being inserted (in an INSERT trigger) or the new values (in an UPDATE trigger)
Deleted is the table holding the values being deleted (in a DELETE trigger) or the old values (in an UPDATE trigger)
With those two pseudo-tables, you can get access to all data you might need.

Related

Update table from one server to another

I have a table in one server and I need to update same table which is in another server, using a trigger is not a good idea because I was having issues and I read is not a good approach.
I was thinking to use either extended events or service broker, I'm using SQL Server 2014. I don't want to create a SQL Job that runs like every minute to check if table has an insert/update/delete to copy the data into the other server. Not sure which one is the best for my purpose, For example TableA on Server 1 is updated, I need to reflect this update into the other TableA on Server 2 but only when any insert/update/delete happens.

MS Access linked table to SQL Server update record

I have a table in SQL Server 2012 (Table1) and I have created an MS Access 2016 database and then created a linked table in MS Access to Table1 in SQL Server 2012 using a System DSN.
This works fine, I can open the linked Table1 in MS Access 2016 see the columns and update the values. I have then created a Form in MS Access 2016, added the columns from Table1 to the Form and I can see the data fine.
The problem I have is that the option to Add New Record is greyed out and no matter what I try I cannot add a new record to Table1 using the Form.
If I create a standard (not a linked table) in MS Access 2016 and add the columns to the Form rather than the columns from the Linked Table1 the Add New Record icon is then available.
My question is... Is it possible to use a Form in MS Access 2016, have the data source as a Linked Table to a table in SQL Server 2012 and add new records to it? (And if so, how do I achieve this as I've been at this now for hours and hours and cannot figure it out).
Many thanks in advance.
You note that you can update values when you click on the linked table and view the rows.
Can you ADD rows when using that linked table? And if you cannot, then open up the table using SQL Management Studio and try adding rows that way?
If you don’t have correct (or any) settings in SQL server that sets up or increments the primary key, then you can’t add rows (automatic in this context).
So make sure the table has a primary key, make sure you can add rows using SSMS. If you make changes to fix this SQL table, then you have to re-link the table.
I suggest you create a primary key in the SQL table, and make it a identity column with a increment of 1.
It is certainly possible; this is a very common approach. It sounds like the login you used in the System DSN to connect to SQL Server does not have INSERT permissions on the table. Are you able to review the permissions to check that?
The auto-insertion/update doesn't work for link-database. You need to write a VBA code behind your interface.
Below solution can give you a basic and simple heads up:
Inserting into SQL Server using MS Access
Have a similar situation. Linked Sql Server table, Access Form front end. I am able to insert/update/delete via the form. However, I then created a checkbox to implement a filter on the data. When the box is not checked, I can still edit the data. However, as soon as I check the box to activate the filter, I can no longer insert/update/delete. When I uncheck the box, I can again edit the data. So in my case, the problem is due to the filter, which is implemented via a VBA query involving both an outer join and a union. The query replaces the form's control source when activated, and I believe it is simply too complex for Access to treat it as updatable.

How can I find information about updating or inserting a sql table row in sql server 2008?

I am looking for information about my table history,
I have to know the time that specific row was inserted
is there someway to know it?
thanks.
Using Triggers
A Datetime column in that table and an After Insert,Update Trigger
which updates that column to GETDATE().
This will only give you the details about the very last
change(update/Insert).
CDC
Change Data Capture (CDC) was introduced in SQL Server 2008. Change
Data Capture records INSERTs, UPDATEs, and DELETEs applied to SQL
Server tables, and makes a record available of what changed, where,
and when, in simple relational ‘change tables’.
These "Change Tables" contain columns that reflect the column
structure of the source table you have chosen to track, along with the
metadata needed to understand the changes that have been made.
Read here more about CDC
Also it is only supported in Datacenter & Enterprise edition.

Importing data from different SQL Server to another when trigger fires

I need to create a trigger on one server to fire when a certain table is changed, after which I have to do some calculation with the data so I can import (it's no just copying) it into another table on a different server.
Also I only want to import the new data that hasn't been imported through a earlier trigger.
How would I go about this?
Create Linked Server to your target server, then use MERGE statement to perform action depending on existence of record in destination table.
I would refrain from creating triggers referring remote servers though, it might have performance and reliability implications. Consider using Service Broker if you want to achieve small latency and still make the update reliable.
You can create a linked server between two SQL Servers to send data from one to the other.
To create a linked server you need to use the system stored procedure sp_addlinkedserver, you can also do it through SQL Server Management Studio I believe.
Here is an example you can try:
EXECUTE sp_addlinkedserver #server=N'serverip/hostname', #provider=N'SQLNCLI'
You can view if your linked server has been created by querying sys.servers
You can query the linked server database with the following syntax:
SELECT x
FROM [linkedservername].[database].[schema].[table]
More information: http://msdn.microsoft.com/en-us/library/ff772782.aspx
For the trigger updating only data which hasn't been handled before. There are many ways you can do this. If your source table has an 'update date' type column, you can do it based on that. Alternatively, if your table has an identity column and you want to copy data incrementally you can store in a table the 'last id' which is copied over each time, then on the next run of the trigger you can tell it to start from that id+1 that way rows are only transferred once.

MS SQL Auditing

I have a problem with a Database at my work. There is currently auditing in place, but its clunky, requires a lot of maintence, and it falls short in a few regards. So I am replacing it.
I want to do this in as generic of a way as possible and have designed the tables, and how everything will link and be updated.
Now, thats all fine and good, but I want to be able to write a generic way to insert records into these audit tables. (Without having to enter a command for each column in each table being changed.)
Is there anyway within a Stored Procedure to iterate over all the columns in a table? And I would like to write this in such a way that it will work with several tables, and automatically pickup and audit added columns and such.
Any ideas?
EDIT: Guess I should Clarify. I will be auditing data that is in the tables. But I will be using the same table(s) to store the audited data for every table in the database.
And I can not use Triggers because usually, when an update occurs, it occurs across multiple tables, but I would like all of these updates to be part of a single Change Set.
That is not a problem, because I can do all the Updates from within a single Stored Proc. I would just prefer some way like a loop, that i can get all the updated fields, figure out which ones changed, and the insert those changed ones into the audit table.
And I would like to do this without have a long list of if statements and insert statements for each column. (By doing this in a generic loop, it will handle added columns automatically and not be bothered by deleted columns)
By "added columns" I guess you are looking to audit DDL. If you use SQL 2005, then you want this link.
If don't use SQL 2005, then you probably want to either use one of the many SQL schema comparison tools, like SQL Red Gates tool set probably has something in there.
If you don't have $ for tools, then you might just want to run periodic queries against information_schema.tables and information_schema.columns. By periodically capturing these in permenant tables, you can identify when they have gained or lost rows (and hence a schema changed occured)
If you are doing data audit instead, then you'll want want to code generate some triggers, again using information_schema.tables and information_schema.columns.
There would be performance considerations, but you could add insert and update triggers to all of your tables, and have the triggers insert into your audit tables.
Use DDL Triggers (assuming you have SQL Server 2005+)!
http://www.sqlteam.com/article/using-ddl-triggers-in-sql-server-2005-to-capture-schema-changes
http://technet.microsoft.com/en-us/library/ms189871.aspx
That could be done if you were using a data access layer that could trap which tables and columns are being update and generating the insert statements for the audit table. In a stored procedure? Which stored procedure? Do you have a single one that does updates? Or are you creating one per table?
If it's an option for you, just upgrade to sql server 2008 and turn on Change Data Capture.

Resources