Tải bản đầy đủ - 0 (trang)
5-7. Porting a Tiny Amount of Data Between SQL Server Databases

5-7. Porting a Tiny Amount of Data Between SQL Server Databases

Tải bản đầy đủ - 0trang

Chapter 5 ■ SQL Server Sources



Figure 5-4.  Scripting data from SSMS

4.



Click Next, and then click Advanced. Find the Types of Data to Script option. Select

Data Only, as shown in Figure 5-5.



260

www.it-ebooks.info



Chapter 5 ■ SQL Server Sources



Figure 5-5.  Data scripting options in SSMS

5.



Click OK. Define a file in which to save the script. Then click through Next, Next, and

Finish.



If you open the file you defined in step 5, you see a (very long-winded) series of insert statements, like the

following shortened version of an output file.

SET IDENTITY_INSERT [dbo].[Client] ON

INSERT [dbo].[Client] ([ID], [ClientName], [Address1], [Address2], [Town], [County],

[PostCode], [Country], [ClientType], [ClientSize], [ClientSince], [IsCreditWorthy],

[DealerGroup], [MapPosition])

VALUES (3, N'John Smith', N'4, Grove Drive', NULL, N'Uttoxeter', N'Staffs', NULL, 1,

N'Private', N'M', NULL, 1, NULL, NULL)

INSERT [dbo].[Client] ([ID], [ClientName], [Address1], [Address2], [Town], [County],

[PostCode], [Country], [ClientType], [ClientSize], [ClientSince], [IsCreditWorthy],

[DealerGroup], [MapPosition])

VALUES (7, N'Slow Sid', N'2, Rue des Bleues', NULL, N'Avignon', N'Vaucluse', NULL, 3,

N'Private', N'M', NULL, 1, NULL, NULL)

SET IDENTITY_INSERT [dbo].[Client] OFF



261

www.it-ebooks.info



Chapter 5 ■ SQL Server Sources



How It Works

If you have only (relatively) small amounts of data to transfer between SQL Server, then you can always use SSMS

to script out the data from one or more tables. This creates a series of INSERT . . . VALUES statements—one for

each record in the data source.

Hopefully you can see why this is not recommended for large quantities of data. However, for small reference

tables that have to be inserted into database creation scripts, it is an incredibly useful technique to know and use.



Hints, Tips, and Traps





You might be tempted to use the “script for server version” to try and copy data over to an

older version of SQL Server. If the current version of SQL Server contains data types (such

as GEOGRAPHY, GEOMETRY, or HIERARCHYID) that are incompatible with the chosen SQL

Server version, then the script creation fails.







There is no option to choose the columns to script out. Equally unfortunately, you cannot

script the data from a view. So if you want to subset a table’s data vertically, you have to

perform a SELECT. . .INTO to export the data into a new table, script out the data from this

“staging” table, and then delete it.



5-8. Copying and Pasting Between Tables

Problem

You want to copy a tiny amount of data between a couple of tables without any hassle or time spent on the

process.



Solution

Copy and paste the data.

Incredible as it may sound, this can be done as follows:

1.



In SSMS, expand the database containing the source table, expand Tables, right-click

the table whose data (all or in part) you want to copy, and select Edit Top 200 Rows.



2.



Click inside the data table that is displayed and select Query Designer ➤ Pane ➤ SQL

(or click the Show SQL toolbar button).



3.



Add a suitable WHERE clause to the SQL SELECT statement to display the data you wish

to copy, and alter TOP (200) to match the approximate number of records that you

plan to copy.



4.



Click the Execute toolbar button or select Query ExecuteSQL.



5.



Select all the output data (by clicking in the gray square to the left of the first column

header in the grid). You should see something like Figure 5-6.



262

www.it-ebooks.info



Chapter 5 ■ SQL Server Sources



Figure 5-6.  Copying and pasting data in SSMS

6.



Copy the data (I will not insult your intelligence by saying how).



7.



Right-click the destination table. Select Edit Top 200 Rows.



8.



Select a blank record (for instance, at the bottom of the record set) and paste the data.



How It Works

I imagine that the thought of simply copying and pasting hundreds or thousands of rows of data into an SQL

Server table has most developers’ eyebrows raised. Well, it is possible—and it can be a valuable time-saving

technique when deploying databases and synchronizing development environments. You will note that I am not

suggesting this as a production technique. It is nonetheless worth looking at, as you can do in SQL Server what

Access developers have been doing for 20 years.

You need a destination table identical in structure to the source data—right down to column order.

This destination table must not contain any duplicate key data.



Hints, Tips, and Traps





You can define a view as the data source, and copy from this. Your destination table must

be identical in structure to the view, however.







This is not (really, truly not) recommended for large amounts of data.







I doubt that you would want to use this in a mission-critical environment.



5-9. Loading Data as Fast as Possible in an ETL Process

Problem

You want to load data as fast as possible on a regular basis as part of an ETL process.



Solution

Use the SSIS SQL Server Destination as part of an SSIS project. This requires you to do the following:

1.



Create a new SSIS package.



2.



Add an OLEDB source connection manager. Configure it to use the ADAM02\Remote

server and the CarSales database.



263

www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

5-7. Porting a Tiny Amount of Data Between SQL Server Databases

Tải bản đầy đủ ngay(0 tr)

×