I would like to write a query with data based on a SQL Server table and insert the results into a SQL Azure table. Is this possible in SQL Server Management Studio?
For example:
SELECT stuff
INTO AzureTable
FROM SqlServerTable
I suppose it's possible to do something similar to that with a linked server to your SQL Azure host, but using SELECT INTO explicitly will not work - you'd need the destination table to exist first (see this MSDN blog post where they describe why SELECT INTO is not supported in SQL Azure).
If the destination tables don't already exist, you can script the objects in Management Studio (as described here) or use tools like SQL Compare from Red Gate to do the grunt work for you.
Once the destination tables exist, you can just write insert statements (again assuming a linked server) or you could use a slightly different approach. For example, have you looked at the SQL Azure Migration Wizard on Codeplex or SQL Data Compare from Red Gate?
Related
I have a fairly straight way to copy selective data using SQL like so:
# Courses
DROP TABLE db_node.courses;
CREATE TABLE db_node.courses LIKE db_prod.course_sis;
INSERT INTO db_node.courses SELECT
*
FROM
db_prod.course_sis
WHERE
db_prod.course_sis.enabled = 1
AND db_prod.course_sis.hidden <> 1;
This is easy when I am on the same server with the same db, however I want to run this SQL to get put the final data on the SQL Server.
This isn't just a once off thing, it would need to be handled every hour or so. I am unable to change db's, one will always be MySQL and SQL Server because the data is used in a sharepoint app.
Thanks
There are several third-party tools to make this transition easier.
Check out SSMA here and here.
I'd also look at SQL Studio scheduled tasks to automate the process.
I'm querying a MySQL linked server from SQL Server.
I can query the linked server using OpenQuery as in the following example.
SELECT * FROM OPENQUERY(MyLinkedServer, 'SELECT * FROM SomeTable')
I have tried querying the linked server using a four-part name as in the following example.
SELECT * FROM MyLinkedServer.MyDatabase.DBO.SomeTable
That returns an error stating that "the provider does not expose the necessary interfaces to use a catalog or schema."
Is there any way to query a linked server without using OpenQuery?
Thanks!
Update: Per Shredder's answer, the correct syntax is as follows.
SELECT * FROM MyLinkedServer...SomeTable
It is possible, but MS recommends that you use the openquery:
This method lets SQL Server send the command directly to the linked
OLAP provider, without trying to parse it.
You can have a look at the examples here under "Linked server examples with four-part names"
Also, I found this:
If you receive these error messages, a table may be missing in the
[Linked DB] schema or you may not have permissions on that table.
I would verify that your mysql db has given access rights to your sql server.
I'm trying to use the SQL Server 2008 Change Tracking feature. Once the feature is enabled, you can make use of the CHANGETABLE(... function to query the change tracking history that is kept internally by SQL Server, e.g.:
SELECT
CT.ID, CT.SYS_CHANGE_OPERATION,
CT.SYS_CHANGE_COLUMNS, CT.SYS_CHANGE_CONTEXT
FROM
CHANGETABLE(CHANGES dbo.CONTACT,20) AS CT
where the SYS_CHANGE_CONTEXT column records the CONTEXT_INFO() session value. This column is useful for auditing who changed what etc.
Some of the statements that change data are executed using four-part notation by a remote SQL Server that has the home server as a linked server e.g.:
INSERT INTO [home server].[db name].[dbo].[CONTACT](id) values(#id)
My problem is that the CONTEXT_INFO() as set on the remote server in the session executing the query does not get picked up in my home server change tracking, i.e. it doesn't look like the CONTEXT_INFO spans a distributed query. This means that the following will not result in the CONTEXT_INFO being logged on the home server change tracking.
-- I'm running on a remote server
WITH CHANGE_TRACKING_CONTEXT (0x1256698477)
INSERT INTO [home server].[db name].[dbo].[CONTACT](id) values(#id)
Does anyone know whether this is a limitation or if there is a way to persist/communicate CONTEXT_INFO across the distributed query?
Thanks
I was thinking about using Context_Info to audit changes (web app). but after doing some tests understood its not good idea. Because of connection pooling context_info was not working the way i desired.
Ended up with using GUID identifier associated with each logical session + table, where is stored session GUID and information related to session + each table stores that identifier in separate column. Not as easy to code as it would be with context_info()..
And as far as i understood from documentation, change tracking is not designed for audit purposes (think that is what you trying to do).
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.
I have a mysql database full of data which I need to keep but migrate to SQL Server 2008.
I know end to end where the data should go, table to table but I have no idea how to go about moving the data. I've looked around the web but it seems there are 'solutions' which you have to download and run. I'd rather if possible do something myself in terms of writing scripts or code.
Can anyone recommend the best way to do this please?
You have several options here:
On the sql server side, you can set up a connection to your old mysql db using something called a linked server. This will allow you to write sql code for sql server that returns data from the mysql tables. You can use this to build INSERT or SELECT INTO statements.
You can write queries for mysql to export your data as csv, and then use the BULK INSERT features of sql server to efficiently import the csv data.
You can use Sql Server integration services to set move the data over from mysql.
Regardless of which you choose, non-data artifacts like indexes, foreign keys, triggers, stored procedures, and security will have to be moved manually.
Have you tried tool from MSFT called SQL Server Migration Assistance for MySQL ???
https://www.microsoft.com/download/en/details.aspx?id=1495
Try this tutorial it is very easy to perform migration to SQL Server from Mysql and is straightforward as mentioned
http://www.codeproject.com/Articles/29106/Migrate-MySQL-to-Microsoft-SQL-Server
Thanks
You can use the Import/Export Wizard that comes with SQL Server Standard Edition.
Select your 'data source' from MySQL using the ODBC data source. Note: You will need to first install the from ODBC driver for MySQL (ODBC Connector). Then, select your SQL Server destination. Select all tables, and fire it up. You will need to add your primary and foreign keys, and indexes manually.
A bit more automated means would be by using the SQL Server Migration Assistant for MySQL, also free. It has the benefit of recreating the relationships and indexes automatically for you. Probably your best bet.
I did it once, some time ago. First you could couple your mssql server to the mysql server using the odbc mysql connector
http://dev.mysql.com/downloads/connector/
After the connection is made you can write you database procedure as you would if it were two mssql db's. Probably easiest to write some sql batch scripts including a cursor where you run through every every row of a table an decide on a field basis where you will need the field in the future.
example of a cursor: http://www.mssqltips.com/tip.asp?tip=1599
If you decide to go with the cursor, you can play with the parameter to increase performance. I especially remember the FORWARD_ONLY parameter giving a big boost.