Get the inserts - sql-server-2008

I want to know if I can get all the INSERTs that are inserted at my database, from once.
Writing just once ... I want that shows me all the inserts with the data and everything that was inserted in the moment of insertion. Not just the data, but everything including the INSERTs, is this possible?
I'm using SQL server 2008.

I can only think of two methods:
1) You could try using the new auditing features of SQL Server 2008 How to: Create a Server Audit and Database Audit Specification
2) write an INSERT trigger for each table and send the INSERTED table to a common log.

Related

SQL Server rows not editable for Access after Insert

I have this problem: I'm using a SQL Server 2008R2 backend and MS Access 2000 frontend where some tables are connected via ODBC.
Following Structure (Tables all on SQL-Server):
Import (not connected to Access)
Products (connected via ODBC to Access)
Pricing (connected via ODBC to Access)
I want to fill the Pricing table automatically with some data from Products and Import. This is supposed to run as a SQL Agent job with a T-SQL script.
I want to insert the data from "Products" with following command:
INSERT INTO Pricing (Productnr, Manufacturernr)
(SELECT Productnr, Manufacturernr
FROM Products
WHERE Valid = 1
AND Productnr NOT IN (SELECT Productnr FROM Pricing ));
Right after that the inserted rows are locked for Access, I can't change anything. If I execute sql queries with SQL Server Management Suite or if i start queries as SQL Agent jobs everything works fine.
Why are the rows locked in ms access after the query ran (even if it finished successfully)? and how can I unlock them or make it unlock itself right after the query/job ran?
Thanks
When SQL Server inserts new rows, those new rows are in fact exclusively locked to prevent other transactions from reading or manipulating them - that's by design, and it's a good thing! And it's something you cannot change - you cannot insert without those locks.
You can unlock them by committing the transaction that they're being inserted under - once they're committed to SQL Server, you can access them again normally.
The error message i get says, that the dataset has been changed by another user and if i save it, i would undo the changes of the other user. (and asks me for copying into clipboard).
This is different from "locked", and completely normal.
If you have a ODBC linked table (or form based on the table) open, and change data in the backend, Access doesn't know about the change.
You need to do a full requery (Shift+F9) in Access to reload the data, afterwards all records can be edited again.
Got the solution for my Problem now.
I had to add a timestamp row into the pricing table, so access could recognize the change.
Access loads the data into the front end when the table is first accessed. If something in the backend changes the data, you need Access to refresh it first, before you can edit it from the front end (or see the changes).
Do this by (in Access) by closing and reopening the table, or switching to the table and pressing shift-F9 as Andre suggested, or programmatically using a requery statement. You must requery, not refresh, for it to release the locks and register the changes made in SQL.

Sql does not queue queries to linked server

i have a table in sql server and a table with the same name and fields in mysql server. i connected them trhough a linked server using the following trigger:
CREATE TRIGGER items_insert ON [prueba]
FOR INSERT
AS
BEGIN
declare #tmp table (a int, b varchar(10))
insert #tmp (a, b) select ID, Name from inserted
COMMIT
SET XACT_ABORT ON
INSERT INTO OPENQUERY(WEBDB, 'SELECT * FROM prueba')
SELECT a, b FROM #tmp
begin tran
end
my problem is that when i take offline the mysql serverm and i insert a record in sql server, it obviously does not insert in mysql, but when i put the mysql server it does not either. i want a queue of sorts, so that when the connection between servers drop, any new records during that time are inserted in mysql when the connection is restored. How could i achieve this?, i am new to sql server and triggers
NOTE: the trigger has the #tmp declarations according to this tutorial because i was getting a weird error about transactional errors
Triggers will never queue and using linked servers inside a trigger is a bad idea. You will find hundreds of people burning their fingers with this one I did too.
For any queue type system you will need to implement service broker or as Nilesh pointed out use a job which queue the tables.
Your current setup is going to be very problematic as I used the same approach several years ago in a attempt to get data from SQL2005 to a MySQL server. Incidental in SQL2000 you could actually replicate the data from MSSQL to any other ODBC datasource. Microsoft discontinued this in SQL2005.
So you have two choices here.
Learn Service Broker: Service broker is a awesome but little used piece of SQL. It is a asynchronous queuing technology that allows you to send message to other remote systems check this link for much more information. However this is going to take time and effort to implement as you will have to learn quiet a bit i.e. steep learning curve.
Create a queue table and process on a schedule. Create a table that has the data you want to insert into MySQL with a processed flag. In the trigger insert this data into the table. Create a SQL server job that runs every minute and inserts the data from the queuing table into the MySQL database. On successful insertion mark it as processed.
Add a processed flag to the original table. Create a job that uses the table to get all items that have not been inserted and insert them on a schedule. This is like option 2 but you dont create a additional table.

Querying MySQL and MSSQL databases at the same time

I'm getting data from an MSSQL DB ("A") and inserting into a MySQL DB ("B") using the date created in the MSSQL DB. I'm doing it with simple logics, but there's got to be a faster and more efficient way of doing this. Below is the sequence of logics involved:
Create one connection for MSSQL DB and one connection for MySQL DB.
Grab all of data from A that meet the date range criterion provided.
Check to see which of the data obtained are not present in B.
Insert these new data into B.
As you can imagine, step 2 is basically a loop, which can easily max out the time limit on the server, and I feel like there must be a way of doing this must faster and during when the first query is made. Can anyone point me to right direction to achieve this? Can you make "one" connection to both of the DBs and do something like below?
SELECT * FROM A.some_table_in_A.some_column WHERE
"it doesn't exist in" B.some_table_in_B.some_column
A linked server might suit this
A linked server allows for access to distributed, heterogeneous
queries against OLE DB data sources. After a linked server is created,
distributed queries can be run against this server, and queries can
join tables from more than one data source. If the linked server is
defined as an instance of SQL Server, remote stored procedures can be
executed.
Check out this HOWTO as well
If I understand your question right, you're just trying to move things in the MSSQL DB into the MySQL DB. I'm also assuming there is some sort of filter criteria you're using to do the migration. If this is correct, you might try using a stored procedure in MSSQL that can do the querying of the MySQL database with a distributed query. You can then use that stored procedure to do the loops or checks on the database side and the front end server will only need to make one connection.
If the MySQL database has a primary key defined, you can at least skip step 3 ("Check to see which of the data obtained are not present in B"). Use INSERT IGNORE INTO... and it will attempt to insert all the records, silently skipping over ones where a record with the primary key already exists.

MySQL trigger to update SQL Server table

Is it possible to update a table in SQL Server using a trigger in MySQL. The scenario is like this.
When a table is updated in MySQL, a corresponding update is to be done in SQL Server for the purpose of syncing certain fields. I understand that the reverse is possible using a linked server in SQL Server.
You can write a UDF function that would update data in the SQL Server. Then call this UDF from the trigger.
Here's some information which may be of help regarding linking SQL Server and MySQL
http://forums.mysql.com/read.php?60,123221,123221
I'm afraid I've no personal experience in the matter as I thankfully have MySQL servers across the board, however had a scan through the links in the above and at least one of them should be able to assist.

Updating records in MYSQL with SSIS

I am writing an SSIS package that has a conditional split from a SQL Server source that splits records to either be updated or inserted into a MYSQL database.
The SQL Server connection has provider .NET Provider for OldDB\SQL Server Native Client 10.0.
The MYSQL connection is a MYSQL ODBC 5.1 ADO.NET connection.
I was thinking about using the OLE DB Command branching off of the conditional split to update records but I connect use this and connect to the MYSQL database.
Does anyone know how to accomplish this task?
I would write to a staging table for updates including the PK and columns to be updated and then execute an UPDATE SQL statement using that table and the table to be updated. The alternative is to use the command for every row and that just doesn't seem to perform that well in my experience - at least compared to a nice fat batch insert and a single update command.
For that matter, I guess you could do without the conditional split altogether, write everything to a staging table and then use an UPDATE and INSERT in SQL back to back.
Probably, the following MSDN blog link might help you. I haven't tried this.
How do I UPDATE and DELETE if I don’t have an OLEDB provider?
The post suggests the following three options.
Script Component
Store the data in a Recordset
Use a custom component (like Merge destination component)
The author also had posted two other articles about MySQL prior to posting the above article.
Connecting to MySQL from SSIS
Writing to a MySQL database from SSIS
Hope that points you in the right direction.