我有两个数据库LIVE
和DEVEL
。我目前正在DEVEL
工作,并创建并修改了许多存储过程,但是我的测试数据用完了所以我需要找到一种方法将数据从LIVE
复制到DEVEL
但我不想丢失我的存储过程。
我正在使用SQL Server 2008 Management Studio并尝试过Tasks -> Export Data
。然后我选择Drop and recreate new destination tables
,但是我得到的错误如下:
违反PRIMARY KEY约束'PK_Currency_Rate'。无法插入 对象'dbo.Currency_Rate'中的重复键。
所以它不会丢弃并重新创建表格。
我需要经常将数据从LIVE
迁移到DEVEL
,但还没有找到一种方法,这会阻碍我,因为我无法测试已实现的功能。
有没有经验丰富的人能指出我正确的方向?我对RedGate SQL Compare等付费第三方工具不感兴趣所以请不要建议
答案 0 :(得分:3)
我会确保我的所有SP都编写成可以按需重新运行的文件。
VS中有比较工具允许您编写更改脚本,或者您可以手动进入DEVEL数据库,展开Programmability
> Stored Procedures
个节点,右键点击新的SP并选择Script Stored Procedure
> Create to
> File
并将其保存在适合您项目的地方。
假设是这种情况,如果只是简单地将您的LIVE数据库恢复到DEVEL DB的顶部,您可以在DEVEL上重新运行SP脚本,这将解决问题。
答案 1 :(得分:2)
对您的表执行此查询,复制输出数据并在另一个数据库上运行它。
<强>查询:强>
DECLARE
@TableName SYSNAME
, @ObjectID INT
, @IsImportIdentity BIT = 1
DECLARE [tables] CURSOR READ_ONLY FAST_FORWARD LOCAL FOR
SELECT
'[' + s.name + '].[' + t.name + ']'
, t.[object_id]
FROM (
SELECT DISTINCT
t.[schema_id]
, t.[object_id]
, t.name
FROM sys.objects t WITH (NOWAIT)
JOIN sys.partitions p WITH (NOWAIT) ON p.[object_id] = t.[object_id]
WHERE p.[rows] > 0
AND t.[type] = 'U'
) t
JOIN sys.schemas s WITH (NOWAIT) ON t.[schema_id] = s.[schema_id]
WHERE t.name IN ('<your table name>')
OPEN [tables]
FETCH NEXT FROM [tables] INTO
@TableName
, @ObjectID
DECLARE
@SQLInsert NVARCHAR(MAX)
, @SQLColumns NVARCHAR(MAX)
, @SQLTinyColumns NVARCHAR(MAX)
WHILE @@FETCH_STATUS = 0 BEGIN
SELECT
@SQLInsert = ''
, @SQLColumns = ''
, @SQLTinyColumns = ''
;WITH cols AS
(
SELECT
c.name
, datetype = t.name
, c.column_id
FROM sys.columns c WITH (NOWAIT)
JOIN sys.types t WITH (NOWAIT) ON c.system_type_id = t.system_type_id AND c.user_type_id = t.user_type_id
WHERE c.[object_id] = @ObjectID
AND (c.is_identity = 0 OR @IsImportIdentity = 1)
AND c.is_computed = 0
AND t.name NOT IN ('xml', 'geography', 'geometry', 'hierarchyid')
)
SELECT
@SQLInsert = 'INSERT INTO ' + @TableName + ' (' + STUFF((
SELECT ', [' + c.name + ']'
FROM cols c
ORDER BY c.column_id
FOR XML PATH, TYPE, ROOT).value('.', 'NVARCHAR(MAX)'), 1, 2, '') + ')'
, @SQLTinyColumns = STUFF((
SELECT ', ' + c.name
FROM cols c
ORDER BY c.column_id
FOR XML PATH, TYPE, ROOT).value('.', 'NVARCHAR(MAX)'), 1, 2, '')
, @SQLColumns = STUFF((SELECT CHAR(13) +
CASE
WHEN c.datetype = 'uniqueidentifier'
THEN ' + '', '' + ISNULL('''''''' + CAST([' + c.name + '] AS VARCHAR(MAX)) + '''''''', ''NULL'')'
WHEN c.datetype IN ('nvarchar', 'varchar', 'nchar', 'char', 'varbinary', 'binary')
THEN ' + '', '' + ISNULL('''''''' + CAST(REPLACE([' + c.name + '], '''''''', '''''''''''' ) AS NVARCHAR(MAX)) + '''''''', ''NULL'')'
WHEN c.datetype = 'datetime'
THEN ' + '', '' + ISNULL('''''''' + CONVERT(VARCHAR, [' + c.name + '], 120) + '''''''', ''NULL'')'
ELSE
' + '', '' + ISNULL(CAST([' + c.name + '] AS NVARCHAR(MAX)), ''NULL'')'
END
FROM cols c
ORDER BY c.column_id
FOR XML PATH, TYPE, ROOT).value('.', 'NVARCHAR(MAX)'), 1, 10, 'CHAR(13) + '', ('' +')
DECLARE @SQL NVARCHAR(MAX) = '
SET NOCOUNT ON;
DECLARE
@SQL NVARCHAR(MAX) = ''''
, @x INT = 1
, @count INT = (SELECT COUNT(1) FROM ' + @TableName + ')
IF EXISTS(
SELECT 1
FROM tempdb.dbo.sysobjects
WHERE ID = OBJECT_ID(''tempdb..#import'')
)
DROP TABLE #import;
SELECT ' + @SQLTinyColumns + ', ''RowNumber'' = ROW_NUMBER() OVER (ORDER BY ' + @SQLTinyColumns + ')
INTO #import
FROM ' + @TableName + '
WHILE @x < @count BEGIN
SELECT @SQL = ''VALUES '' + STUFF((
SELECT ' + @SQLColumns + ' + '')''' + '
FROM #import
WHERE RowNumber BETWEEN @x AND @x + 9
FOR XML PATH, TYPE, ROOT).value(''.'', ''NVARCHAR(MAX)''), 1, 2, CHAR(13) + '' '') + '';''
PRINT(''' + @SQLInsert + ''')
PRINT(@SQL)
SELECT @x = @x + 10
END'
EXEC sys.sp_executesql @SQL
FETCH NEXT FROM [tables] INTO
@TableName
, @ObjectID
END
CLOSE [tables]
DEALLOCATE [tables]
<强>输出:强>
INSERT INTO [Person].[Address] ([AddressID], [AddressLine1], [AddressLine2], [City], [StateProvinceID], [PostalCode], [rowguid], [ModifiedDate])
VALUES
(1, '1970 Napa Ct.', NULL, 'Bothell', 79, '98011', '9AADCB0D-36CF-483F-84D8-585C2D4EC6E9', '2002-01-04 00:00:00')
, (2, '9833 Mt. Dias Blv.', NULL, 'Bothell', 79, '98011', '32A54B9E-E034-4BFB-B573-A71CDE60D8C0', '2003-01-01 00:00:00')
, (3, '7484 Roundtree Drive', NULL, 'Bothell', 79, '98011', '4C506923-6D1B-452C-A07C-BAA6F5B142A4', '2007-04-08 00:00:00')
, (4, '9539 Glenside Dr', NULL, 'Bothell', 79, '98011', 'E5946C78-4BCC-477F-9FA1-CC09DE16A880', '2003-03-07 00:00:00')
, (5, '1226 Shoe St.', NULL, 'Bothell', 79, '98011', 'FBAFF937-4A97-4AF0-81FD-B849900E9BB0', '2003-01-20 00:00:00')
, (6, '1399 Firestone Drive', NULL, 'Bothell', 79, '98011', 'FEBF8191-9804-44C8-877A-33FDE94F0075', '2003-03-17 00:00:00')
, (7, '5672 Hale Dr.', NULL, 'Bothell', 79, '98011', '0175A174-6C34-4D41-B3C1-4419CD6A0446', '2004-01-12 00:00:00')
, (8, '6387 Scenic Avenue', NULL, 'Bothell', 79, '98011', '3715E813-4DCA-49E0-8F1C-31857D21F269', '2003-01-18 00:00:00')
, (9, '8713 Yosemite Ct.', NULL, 'Bothell', 79, '98011', '268AF621-76D7-4C78-9441-144FD139821A', '2006-07-01 00:00:00')
, (10, '250 Race Court', NULL, 'Bothell', 79, '98011', '0B6B739D-8EB6-4378-8D55-FE196AF34C04', '2003-01-03 00:00:00');
INSERT INTO [Person].[Address] ([AddressID], [AddressLine1], [AddressLine2], [City], [StateProvinceID], [PostalCode], [rowguid], [ModifiedDate])
VALUES
(11, '1318 Lasalle Street', NULL, 'Bothell', 79, '98011', '981B3303-ACA2-49C7-9A96-FB670785B269', '2007-04-01 00:00:00')
, (12, '5415 San Gabriel Dr.', NULL, 'Bothell', 79, '98011', '1C2C9CFE-AB9F-4F96-8E1F-D9666B6F7F22', '2007-02-06 00:00:00')
, (13, '9265 La Paz', NULL, 'Bothell', 79, '98011', 'E0BA2F52-C907-4553-A0DB-67FC67D28AE4', '2008-01-15 00:00:00')
, (14, '8157 W. Book', NULL, 'Bothell', 79, '98011', 'A1C658AE-C553-4A9D-A081-A550D39B64DF', '2004-01-05 00:00:00')
, (15, '4912 La Vuelta', NULL, 'Bothell', 79, '98011', 'F397E64A-A9D8-4E57-9E7C-B10928ACADD6', '2007-12-20 00:00:00')
, (16, '40 Ellis St.', NULL, 'Bothell', 79, '98011', '0312B65F-CB60-4396-9EC7-A78B2EAC1A1B', '2006-12-11 00:00:00')
, (17, '6696 Anchor Drive', NULL, 'Bothell', 79, '98011', 'CE9B3B47-9267-4727-BCD2-687C47482C06', '2007-12-10 00:00:00')
, (18, '1873 Lion Circle', NULL, 'Bothell', 79, '98011', '963854F7-E3CB-46A1-A3DB-1B05F71D6473', '2008-01-01 00:00:00')
, (19, '3148 Rose Street', NULL, 'Bothell', 79, '98011', '6B7ACB0F-CDBF-44FD-BA14-EB08A56C1582', '2008-05-04 00:00:00')
, (20, '6872 Thornwood Dr.', NULL, 'Bothell', 79, '98011', '4B1F1ED4-97A4-43FD-BB1E-9E05817718E8', '2003-03-09 00:00:00');
答案 2 :(得分:0)
如果您需要复制数据库,请备份数据库(右键单击数据库 - &gt; Tasks
- &gt; Back Up...
),然后将其还原(右)点击Databases
- &gt; Restore Database...
答案 3 :(得分:0)
我会为这项工作准备SSIS任务。
作为每项任务的第一步,我将确保从目标表中删除数据,然后我将复制数据。