Sql Server Scripting Data Only: Workaround for CyclicalForeignKeyException? - sql-server

I have a SQL Server DB that I want to script data for from a single table. When I try this I get the CyclicalForeignKeyException- presumably because somewhere there is an FK cycle, which is fine. This seems to be an annoying limitation of SQL Server, and in my case, I'm using SQL Server 2008 R2.
The 3 suggestions I've read for this are:
Get rid of the cycles. (Not an option as I don't want to modify this DB.)
Temporarily remove the keys and reset them afterwards. (Not an option for the same reason.)
Script all objects in the DB. (This would be possible, but this DB is large so this it not an ideal solution.)
Another similar suggestion I read was to backup the DB and restore a temp copy, remove the FKs, then get the data. But again since the DB is large, this isn't ideal either.
Any one have another idea?

Wow- I can't believe this worked...
I have a similar version of the DB I was trying to get data for on another machine. This time when I ran the tool it worked. (By tool I mean: Tasks->Generate Scripts... select 1 table, in advanced, change "Types of data to script" to "Data only".)
At first I thought this other version of the DB must not have the cyclical keys, but then I realized that I was using SQL Server Management Studio 2012 Express. So then I closed SSMS, did a runas /netonly with the domain user and opened SSMS 2012 and connected to the SQL Server 2008 R2 DB. I retried to generate the script and it worked!
Apparently SSMS 2012 fixed this "issue" and you can even use it against other DB versions!

Related

How can I copy data from 1 existing database to another existing database on two remote servers?

I have Server_A with DB_A and Server_B with DB_B, both of these are remote servers that I have no root access to.
Server_A is SQL 2012 and DB_A is set in Compatibility level 2008
Server_B is SQL 2008 and DB_B is set in Compatibility level 2008, of course.
I need to copy the data, including relationships and keys from DB_A to DB_B. How can this be done?
Using the import or export wizard I've only been able to move the data and all the relationships are list. Please give a guy a hand and teach him something!
One way is to right-click on the source database in SSMS and select Tasks > Generate Scripts. This will show a wizard which when completed will produce a text file with all the SQL statements needed to replicate the database on a new system.
The neat thing about the wizard is that it gives you a set options so that you can decide what is in the final output. So you get to
pick if you just want the data or the table structures or both.
The final result might not be a good option for large databases but it is very portable.
Actually you can back up to a .bak file.
Where you want to restore it is totally up to you.
If it doesn't work for some reason (it should work fine) you can always try to copy your database like so.
These two steps will copy the database intierly. Including the relationships and keys.
This is the only alternative I've found for your problem since you don't have sysadmin rights.

How can I migrate database from SQL Server 2008 to SQL Server 2000

I am replacing an Access application with a web app, but the client is using SQL Server 2000, and I am using SQL Server 2008.
So, I have the database redesigned, with foreign keys, but now I need to get the data on the client's system.
Part of the problem is that they have images that are over 32k, so osql failed as the command buffer filled up.
I should be able to use osql to import the new schema at least, and perhaps all of the data except for the images.
The Export wizard just wouldn't work, even though I tried the Native SQL Driver and the OLE DB Sql Driver.
Flat files seems like a bad choice, as I don't know if it can do the images.
So, what is a good way to copy a 330M database from 2008 -> 2000?
Not sure about performance or time needed, but you could always try a tool like
Red-Gate SQL Compare / SQL Data Compare
Apex SQL Diff / SQL Data Diff
These will allow you to compare both the schema of two databases, as well as the data, and allow you to create synchronization scripts, or synchronize online.
Marc
I set the image column to null, which reduced the size of the insert statements.
This enabled me to import the data into the target database.

SQL Server 2008 retain last open databases

I use around 3 SQL Server 2008 databases. Every time I need to query on a database, I need to login to that db and then query. Is there a way to retain the last opened database in SQL Server 2008?
As an analogy, think this is like firefox allowing to display the last open websites.
SQL Server retains last opened database. It actually never closes them (auto_close and user instances not withstanding). Do you mean Management Studio by any chance? You can add an USE statement to your saved query. You can use sqlcmd extensions in your query to simply run the query in one shot on all servers/db. Or you can use something like SSMS Tools Pack, a free add on that enhances SSMS with things like query history.
You would need to have the same login for all 3 databases and have auto_close set to off

Database replication from SQLserver 2000 to SQLserver 2008

I'm trying to replicate a rather large database from SQLServer 2000 to SQLServer 2008, located on two different servers. I found an article about attempting this and have been trying to follow its direction. Here is the article.
Mixed Mode Bi-Directional Transactional Replication between SQL 2000 and SQL 2008
Here is the part I'm stuck on:
"So, to create a publication, you will
need to NOT use the publication wizard
that you get in SQL 2008. Instead, use
a generated publication script and for
each sp_addarticle line that you have
in it, make sure that the #ins_cmd,
#upd_cmd & #del_cmd parameters point
to the appropriate stored procedures
and run it on the SQL Server 2000
server. Once this is done, go ahead
and create a subscription to SQL
Server 2008’s database normally."
I was able to get the stored procedures in place for all of the tables but need some direction on creating a generated publication script. Does anyone have some direction or a good example of a generated publication script?
On the final step of the publication wizard you will have the two options:
1. Create the publication
2. Generate a script file with steps to create the publication
The article referenced here is suggesting that you do not let the wizard create the publication but instead only choose to generate the script file. At that point you can edit the script file as instructed in the article.
You can run through the SQL Publication Wizard in 2008 and have it output to a script and then use that as a base model to modify with the appropriate arguments for the sp parameters listed in the question.
I'm just doing an upgrade of SQL 2000 to SQL 2008 R2 and I'm finding the publication wizard works just fine without any need to modify the scripts generated.
The article suggests creating some stored procs because it does not work bi-directionally out of the box. However, when it works brilliantly both directions, and very quickly too, just using the wizard. This is using SQL 2008 R2 which may be the reason, it may have improved since the original SQL 2008.

How can I copy data records between two instances of an SQLServer database

I need to copy some records from our SQLServer 2005 test server to our live server. It's a flat lookup table, so no foreign keys or other referential integrity to worry about.
I could key-in the records again on the live server, but this is tiresome. I could export the test server records and table data in its entirety into an SQL script and run that, but I don't want to overwrite the records present on the live system, only add to them.
How can I select just the records I want and get them transferred or otherwise into the live server? We don't have Sharepoint, which I understand would allow me to copy them directly between the two instances.
If your production SQL server and test SQL server can talk, you could just do in with a SQL insert statement.
first run the following on your test server:
Execute sp_addlinkedserver PRODUCTION_SERVER_NAME
Then just create the insert statement:
INSERT INTO [PRODUCTION_SERVER_NAME].DATABASE_NAME.dbo.TABLE_NAME (Names_of_Columns_to_be_inserted)
SELECT Names_of_Columns_to_be_inserted
FROM TABLE_NAME
I use SQL Server Management Studio and do an Export Task by right-clicking the database and going to Task>Export. I think it works across servers as well as databases but I'm not sure.
An SSIS package would be best suited to do the transfer, it would take literally seconds to setup!
I would just script to sql and run on the other server for quick and dirty transferring. If this is something that you will be doing often and you need to set up a mechanism, SQL Server Integration Services (SSIS) which is similar to the older Data Transformation Services (DTS) are designed for this sort of thing. You develop the solution in a mini-Visual Studio environment and can build very complex solutions for moving and transforming data.

Resources