简体   繁体   English

将本地SQL数据库复制到Azure

[英]Copy local SQL database onto Azure

I want to do the - should be simple - task of copying a database from local to live... 我想做 - 应该很简单 - 将数据库从本地复制到实时的任务...

I have all the data and table structure I want in my local database. 我在本地数据库中拥有所需的所有数据和表结构。

I have a currently running database live database that has all the backup stuff assigned to it etc so I don't just want to create a brand new database (not that I can find how to do this either)... 我有一个当前运行的数据库实时数据库,其中包含分配给它的所有备份内容等,所以我不只是想创建一个全新的数据库(不是我可以找到如何做到这一点)...

I just want to remove all tables from that database and then copy all my data and tables from the local into the live azure sql database. 我只想从该数据库中删除所有表,然后将我的所有数据和表从本地复制到live azure sql数据库中。

How do I do this??? 我该怎么做呢???

you can try to achieve this with SQL Server Management Studio. 您可以尝试使用SQL Server Management Studio实现此目的。

在此输入图像描述

  1. If you goto SQL Management Studio, right click on the database you want to copy data for... 如果您转到SQL Management Studio,请右键单击要复制数据的数据库...
  2. Goto Tasks Generate Script 转到任务生成脚本
  3. Select just your tables not the entire database object 只选择您的表而不是整个数据库对象
  4. Open up your azure database in Visual Studio that you want to create the copy into 在Visual Studio中打开要创建副本的azure数据库
  5. Open a new query for that database and paste the generated script in 打开该数据库的新查询并粘贴生成的脚本
  6. Execute and pray to the gods 执行并向众神祈祷
  7. Jump around because it worked, now run the following command on the azure table to disable foreign key migrations: 跳转因为它工作,现在在azure表上运行以下命令以禁用外键迁移:

    DECLARE @sql NVARCHAR(MAX) = N'';

     ;WITH x AS ( SELECT DISTINCT obj = QUOTENAME(OBJECT_SCHEMA_NAME(parent_object_id)) + '.' + QUOTENAME(OBJECT_NAME(parent_object_id)) FROM sys.foreign_keys ) SELECT @sql += N'ALTER TABLE ' + obj + ' NOCHECK CONSTRAINT ALL; ' FROM x; EXEC sp_executesql @sql; 
  8. now go into sql server management studio and right click on your local database and goto tasks and then export data 现在进入sql server management studio并右键单击本地数据库并转到任务然后导出数据

  9. export to your azure database but make sure to edit the mappings and tick the identity box. 导出到您的azure数据库,但请确保编辑映射并勾选标识框。
  10. the data is moved, now set the foreign keys back using this on your azure database: 移动数据,现在在azure数据库上使用此键设置外键:

    DECLARE @sql NVARCHAR(MAX) = N'';

    ;WITH x AS ( SELECT DISTINCT obj = QUOTENAME(OBJECT_SCHEMA_NAME(parent_object_id)) + '.' + QUOTENAME(OBJECT_NAME(parent_object_id)) FROM sys.foreign_keys ) SELECT @sql += N'ALTER TABLE ' + obj + ' WITH CHECK CHECK CONSTRAINT ALL; ' FROM x;

     EXEC sp_executesql @sql; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM