如果你正在运行SQL Server 2008 R2,在SSMS中内置的选项就像上面描述的marc_s一样发生了一些变化。而不是像他的图中那样选择Script data = true,现在在“表/视图选项”分组上方有一个名为"Types of data to script"的新选项。在这里,您可以选择只脚本数据、模式和数据或模式。效果非常好。
首先学习如何从源db命令行客户端创建spool文件或导出结果集。
第二,学习如何在目标db上执行sql语句
最后,通过在源数据库上运行sql脚本,为目标数据库创建插入语句(以及任何其他语句)。
例如< / p >
SELECT '-- SET the correct schema' FROM dual;
SELECT 'USE test;' FROM dual;
SELECT '-- DROP TABLE IF EXISTS' FROM dual;
SELECT 'IF OBJECT_ID(''table3'', ''U'') IS NOT NULL DROP TABLE dbo.table3;' FROM dual;
SELECT '-- create the table' FROM dual;
SELECT 'CREATE TABLE table3 (column1 VARCHAR(10), column2 VARCHAR(10));' FROM dual;
SELECT 'INSERT INTO table3 (column1, column2) VALUES (''', table1.column1, ''',''', table2.column2, ''');' FROM table1 JOIN table2 ON table2.COLUMN1 = table1.COLUMN1;
SET NOCOUNT ON;
DECLARE @out nvarchar(max) = ''
DECLARE @row nvarchar(1024)
DECLARE @first int = 1
DECLARE cur CURSOR FOR
SELECT '(' + CONVERT(CHAR(1),[Stage]) + ',''' + [Label] + ''')'
FROM CV_ORDER_STATUS
ORDER BY [Stage]
PRINT 'SET IDENTITY_INSERT dbo.CV_ORDER_STATUS ON'
PRINT 'GO'
PRINT 'INSERT INTO dbo.CV_ORDER_STATUS ([Stage],[Label]) VALUES';
OPEN cur
FETCH NEXT FROM cur
INTO @row
WHILE @@FETCH_STATUS = 0
BEGIN
IF @first = 1
SET @first = 0
ELSE
SET @out = @out + ',' + CHAR(13);
SET @out = @out + @row
FETCH NEXT FROM cur into @row
END
CLOSE cur
DEALLOCATE cur
PRINT @out
PRINT 'SET IDENTITY_INSERT dbo.CV_ORDER_STATUS OFF'
PRINT 'GO'
$ pip install mssql-scripter
# Generate DDL scripts for all database objects and DML scripts (INSERT statements)
# for all tables in the Adventureworks database and save the script files in
# the current directory
$ mssql-scripter -S localhost -d AdventureWorks -U sa --schema-and-data \
-f './' --file-per-object