Tải bản đầy đủ - 0 (trang)
8-8. Displaying Microsoft Access Metadata

8-8. Displaying Microsoft Access Metadata

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

Chapter 8 ■ Metadata



Figure 8-6.  Access Documenter output

4.



If you want to save an electronic copy, click File ➤ Export and select Rich Text Format

as the destination format.



You can then open the resulting file with (say) Microsoft Word, and peruse the metadata at your leisure.



How It Works

Although considerably more limited in its range of data types than commercial SQL databases, Access has

metadata too, and there are times when it helps to obtain this before—or during—a data load process. You can

write complex ADOX code to return Access metadata if you wish, but Access can do this for you quite easily, so I

prefer this simpler approach. This presumes that Microsoft Access is installed, and that the database you wish to

query is accessible to (linked to or directly opened by) Access.



454

www.it-ebooks.info



Chapter 8 ■ Metadata



Hints, Tips, and Traps





In the Tools ➤ Options menu’s View tab, make sure that System Objects is checked.

Tables are listed in the MySysObjects table.



8-9. Reading MySQL Metadata

Problem

You want to display metadata from a MySQL database to better understand the source data that you have

to import.



Solution

Use the INFORMATION_SCHEMA views in MySQL to obtain a detailed description of the source metadata.

Follow this process:

1.



In a new or existing SSIS project, create a new SSIS package.



2.



Add a Data Flow task on to the Control Flow pane, and double-click to open it.



3.



Right-click in the Connection Managers tab. Select New Connection.



4.



Select ODBC from the list of connection managers. This is shown in Figure 8-7.



Figure 8-7.  Adding an ODBC connection manager



455

www.it-ebooks.info



Chapter 8 ■ Metadata



5.



Click Add.



6.



Click New to create a new data connection.



7.



Click Use Connection String.



8.



Enter a connection string like the following:

DRIVER = {MySQL ODBC 5.1 Driver};SERVER = localhost;DATABASE = INFORMATION_SCHEMA;

UID = root



9.



Of course, you should use your own server and user ID (UID). It is important, however,

to specify the INFORMATION_SCHEMA database. Add the current password.  The dialog

box should look like Figure 8-8.



Figure 8-8.  Selecting an existing DSN

10.



Click OK twice.



11.



Right-click the Data Flow pane. Select Variables. Add a new variable named

MySQLMetadata. Ensure that the variable’s data type is Object.



12.



Add an ODBC source to the Data Flow pane, and double-click to Edit.



13.



Select the connection manager that you created in steps 4 to 10.



14.



Select SQL Command as the data access mode.



456

www.it-ebooks.info



Chapter 8 ■ Metadata



15.



Enter or paste the following SQL into the SQL Command text field:

SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES;



16.



The dialog box should look like Figure 8-9. Click OK to confirm the creation of the

data source.



Figure 8-9.  The ODBC Source to return MySQL metadata

17.



Add a Recordset Destination onto the Control Flow pane.



18.



Drag a connector from the ODBC source to the Recordset Destination.



19.



Double-click to edit the Recordset Destination.



20.



Select the variable (MySQLMetadata) that you created in step 11 as the Variable Name.



21.



Click the Input Columns tab and select the columns you wish to output.



22.



Click Refresh. The dialog box should look like Figure 8-10.



457

www.it-ebooks.info



Chapter 8 ■ Metadata



Figure 8-10.  Selecting MySQL metadata columns

23.



Click OK.



24.



Right-click the Data Flow path that connects the source to the destination. Select

Enable Data Viewer.



Now, when you run the package, the MySQL metadata will appear in the Data Viewer Grid, as shown

in Figure 8-11.



458

www.it-ebooks.info



Chapter 8 ■ Metadata



Figure 8-11.  MySQL metadata in SSIS



How It Works

MySQL (from version 5) also has an INFORMATION_SCHEMA that you can query to get. The only difference from

the other databases seen so far is that you have to access this metadata over ODBC, in the absence of an OLEDB

provider from the supplier (though you may find that third-party providers do the trick). Also, the way that the

metadata is obtained is a little more quirky, as you can see.

The following code snippet will return MySQL metadata over an ODBC connection:

SELECT

FROM



TABLE_NAME

OPENROWSET('MSDASQL', 'DSN=MySQLMetadata',

'SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES')



If you are working a lot with MySQL, then it could be worth your while to add a linked server (as described

in Recipe 4-9) and use an OPENQUERY to get INFORMATION_SCHEMA data. The following code snippet is an example,

and presumes that the linked server name is MYSQL:

SELECT

FROM



TABLE_NAME

OPENQUERY(MYSQL,'select TABLE_NAME FROM INFORMATION_SCHEMA.TABLES')



The MySQL INFORMATION_SCHEMA data, as stated earlier, only works with MySQL databases from version 5.

However, if you are faced with data from a previous version of MySQL, you can use the following SQL to query the

metadata, in place of that given earlier:

SELECT *

FROM



OPENROWSET('MSDASQL', 'DSN=MySQLMetadata',

'SHOW TABLES')



459

www.it-ebooks.info



Chapter 8 ■ Metadata



Without going into all the arcane details of the MySQL metadata possibilities, Table 8-8 shows a few of the

essential tables and fields that you are likely to need when querying MySQL for its metadata.

Table 8-8.  MySQL Metadata



Table



Column



Notes



TABLES



TABLE_SCHEMA



The database containing the table.



TABLE_NAME



The name of the table.



TABLE_CATALOG



The database containing the table.



TABLE_SCHEMA



The schema owning the table.



TABLE_NAME



The name of the table.



COLUMN_NAME



The column name.



DATA_TYPE



The data type.



IS_NULLABLE



Nullable flag.



COLUMN_DEFAULT



Default (if one exists).



CHARACTER_MAXIMUM_LENGTH



Maximum length in characters.



NUMERIC_PRECISION



Numeric precision (numbers only).



NUMERIC_SCALE



Numeric scale (numbers only).



COLUMNS



CHARACTER_SET_NAME

The closest that I can get to “generic” SQL for MySQL to return all important metadata is the following

(C:\SQL2012DIRecipes\CH08\MySQLMetadata.sql):

SELECT

CO.TABLE_CATALOG

,CO.TABLE_SCHEMA

,CO.TABLE_NAME

,CO.COLUMN_NAME

,CO.ORDINAL_POSITION

,CO.COLUMN_DEFAULT

,CO.IS_NULLABLE

,CO.DATA_TYPE

,CO.CHARACTER_MAXIMUM_LENGTH

,CO.CHARACTER_OCTET_LENGTH

,CO.NUMERIC_PRECISION

,CO.NUMERIC_SCALE

,CO.CHARACTER_SET_NAME

,CO.COLUMN_TYPE

,CO.COLUMN_KEY

,KCU.CONSTRAINT_NAME

,KCU.REFERENCED_TABLE_NAME

,KCU.REFERENCED_COLUMN_NAME



460

www.it-ebooks.info



Chapter 8 ■ Metadata



FROM

LEFT JOIN



COLUMNS CO

KEY_COLUMN_USAGE KCU

ON CO.COLUMN_NAME = KCU.COLUMN_NAME

AND CO.TABLE_NAME = KCU.TABLE_NAME

AND CO.TABLE_SCHEMA = KCU.TABLE_SCHEMA;



This gives most of the base metadata that you are likely to need for data ingestion, and it can be used either

in OPENQUERY or in SSIS. The REFERENCED_TABLE_NAME and REFERENCED_COLUMN_NAME columns let you deduce

foreign keys.

Other information that you may require are triggers, which can be obtained from the Triggers

INFORMATION_SCHEMA table. A basic query would be:

SELECT TRIGGER_SCHEMA, TRIGGER_NAME, EVENT_MANIPULATION, EVENT_OBJECT_SCHEMA,

EVENT_OBJECT_TABLE, ACTION_STATEMENT, ACTION_TIMING

FROM INFORMATION_SCHEMA.TRIGGERS;

You might need to use this more than you think because MySQL up to and including version 5 does not

support CHECK constraints, and many MySQL developers use triggers to emulate CHECK constraint behavior. So

looking into existing triggers can indicate what constraints on data are (or the developer thinks should be) in place.

You can copy the results of the query from the Data Viewer Grid using the Copy Data button. If you require a

more permanent capture of the metadata you can replace the Recordset destination with an OLEDB destination

and store the output in SQL Server. This approach is described (for DB2) in Recipe 8-13.



Hints, Tips, and Traps





When using SSIS to return MySQL metadata, you can use an ODBC DSN instead of

entering a connection string. However, you must be sure to use a 64-bit DSN if you are

working in a 64-bit environment. The ODBC connection manager lists all available

DSNs without indicating if they are 64-bit or 32-bit. See Recipes 4-8 and 4-9 for more

information on creating 32-bit and 64-bit DSNs for MySQL.







Setting the SSIS project Properties for Run64BitRuntime to False will not solve the 32-bit

DSN problem.







If you click Build in step 7 and then follow the process to create a System DSN (described

in Recipe 4-8), you will not only create the correct connection string, but also create a DSN

of the correct “bitness.” You will need to run SSIS with administrative privileges to do this.



8-10. Displaying Oracle Metadata from SSIS

Problem

You need to analyze the Oracle metadata for a database that you have to import into SQL Server.



Solution

Use SSIS to query the Oracle metadata contained in the Oracle system dictionary. The steps that follow explain

how to do it.

1.



Create a new SSIS package.



2.



Add a Data Flow task to the Control Flow pane.



461

www.it-ebooks.info



Chapter 8 ■ Metadata



3.



Click the Data Flow tab.



4.



Add an OLEDB source task to the Data Flow pane.



5.



Double-click to open it.



6.



Select the OLEDB connection manager that you have already created.



7.



Select SQL Command as the data access mode.



8.



Paste the following code into the Command text box (C:\SQL2012DIRecipes\CH08\

OracleMetadata.sql):

SELECT COLUMN_NAME, DATA_TYPE, DATA_LENGTH

, CAST(DATA_PRECISION AS NUMBER(38,0)) AS DATA_PRECISION, NULLABLE

, HIGH_VALUE, LOW_VALUE

FROM all_tab_columns

WHERE table_name='SOURCEDATA'



9.



You should have a dialog box similar to Figure 8-12.



Figure 8-12.  Using SSIS to return Oracle metadata



462

www.it-ebooks.info



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

8-8. Displaying Microsoft Access Metadata

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

×