Tải bản đầy đủ - 0 (trang)
8-7. Analyzing SQL Server Column Metadata

8-7. Analyzing SQL Server Column Metadata

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

Chapter 8 ■ Metadata



CalcDefinition VARCHAR(4000) NULL,

CHARACTER_SET_NAME VARCHAR(250) NULL,

IsPersisted BIT NULL,

IsCharCol INT NULL,

IdentitySeedValue INT NULL,

IdentityIncrementValue INT NULL,

IdentityLastValue BIGINT NULL,

DateCreated datetime NULL,

DateModified datetime NULL,

FullTextTypeColumn INT NULL,

IsIndexable INT NULL,

IsRowGuidCol INT NULL,

IsXmlIndexable INT NULL,

UsesAnsiTrim INT NULL

)

INSERT INTO #Metadata_ColumnTableType

(

SERVER_NAME

,DATABASE_NAME

,TABLE_SCHEMA

,TABLE_NAME

,COLUMN_NAME

,column_id

,DataType

,ColLength

,precision

,scale

,CollationName

,IsNullable

,IsIdentity

,IsComputed

,IsFileStream

,IsSparse

,IsColumnSet

,object_id

,default_object_id

,ColDefinition

,DefaultName

,DefaultDefinition

,IsFulltextIndexed

,CalcDefinition

,CHARACTER_SET_NAME

,IsPersisted

,IsCharCol

,IdentitySeedValue

,IdentityIncrementValue

,IdentityLastValue

,DateCreated

,DateModified

,FullTextTypeColumn



446

www.it-ebooks.info



Chapter 8 ■ Metadata



,IsIndexable

,IsRowGuidCol

,IsXmlIndexable

,UsesAnsiTrim

)

SELECT DISTINCT

@SERVER_NAME

,@DATABASE_NAME

,SCH.name AS TABLE_SCHEMA

,TBL.name AS TABLE_NAME

,COL.name AS COLUMN_NAME

,COL.column_id

,TYP.name AS DataType

,CASE

WHEN TYP.name IN ('nchar','NVARCHAR') THEN (COL.max_length / 2)

ELSE COL.max_length

END AS ColLength

,COL.precision

,COL.scale

,COL.collation_name AS CollationName

,COL.is_nullable AS IsNullable

,COL.is_identity AS IsIdentity

,COL.is_computed AS IsComputed

,COL.is_filestream AS IsFileStream

,COL.is_sparse AS IsSparse

,COL.is_column_set AS IsColumnSet

,COL.object_id

,COL.default_object_id

,CAST('' AS VARCHAR(MAX)) AS ColDefinition

,CAST('' AS VARCHAR(250)) AS DefaultName

,CAST('' AS VARCHAR(MAX)) AS DefaultDefinition

,CAST(NULL AS BIT) AS IsFulltextIndexed

,CAST('' AS VARCHAR(4000)) AS CalcDefinition

,CAST('' AS VARCHAR(250)) AS CHARACTER_SET_NAME

,CAST(0 AS BIT) AS IsPersisted

,CASE

WHEN TYP.collation_name IS NOT NULL THEN 1

ELSE 0

END AS IsCharCol

,CAST(0 AS INT) AS IdentitySeedValue

,CAST(0 AS INT) AS IdentityIncrementValue

,CAST(0 AS BIGINT) AS IdentityLastValue

,OBJ.create_date AS DateCreated

,OBJ.modify_date AS DateModified

,COLUMNPROPERTY(TBL.object_id, COL.name, 'FullTextTypeColumn') AS FullTextTypeColumn

,COLUMNPROPERTY(TBL.object_id, COL.name, 'IsIndexable') AS IsIndexable

,COLUMNPROPERTY(TBL.object_id, COL.name, 'IsRowGuidCol') AS IsRowGuidCol

,COLUMNPROPERTY(TBL.object_id, COL.name, 'IsXmlIndexable') AS IsXmlIndexable

,COLUMNPROPERTY(TBL.object_id, COL.name, 'UsesAnsiTrim') AS UsesAnsiTrim



447

www.it-ebooks.info



Chapter 8 ■ Metadata



FROM



sys.types AS TYP

INNER JOIN sys.columns AS COL ON TYP.user_type_id = COL.user_type_id

INNER JOIN sys.tables AS TBL

INNER JOIN sys.schemas AS SCH

ON TBL.schema_id = SCH.schema_id

INNER JOIN sys.objects AS OBJ

ON TBL.object_id = OBJ.object_id

ON COL.object_id = TBL.object_id



WHERE



TBL.type_desc = 'USER_TABLE'

AND TBL.is_ms_shipped = 0



ORDER BY TABLE_SCHEMA, TABLE_NAME, COL.column_id

IF OBJECT_ID('tempdb..#Tmp_ColFileStream') IS NOT NULL

DROP TABLE tempdb..#Tmp_ColFileStream;

SELECT

SCH.name AS TABLE_SCHEMA

,TTP.name AS TABLE_NAME

,COL.name AS COLUMN_NAME

,COL.is_filestream AS IsFileStream

INTO



#Tmp_ColFileStream



FROM



sys.columns COL

INNER JOIN sys.table_types TTP

ON COL.object_id = TTP.type_table_object_id

INNER JOIN sys.schemas SCH

ON TTP.schema_id = SCH.schema_id



IF OBJECT_ID('tempdb..#Tmp_ColCalculated') IS NOT NULL

DROP TABLE tempdb..#Tmp_ColCalculated;

SELECT

SCH.name AS TABLE_SCHEMA

,TTP.name AS TABLE_NAME

,COL.name AS COLUMN_NAME

,COL.is_persisted AS IsPersisted

,COL.definition AS CalcDefinition

INTO



#Tmp_ColCalculated



FROM



sys.table_types TTP

INNER JOIN sys.schemas AS SCH

ON TTP.schema_id = SCH.schema_id

INNER JOIN sys.computed_columns COL

ON TTP.type_table_object_id = COL.object_id



448

www.it-ebooks.info



Chapter 8 ■ Metadata



-- Column Defaults

IF OBJECT_ID('tempdb..#Tmp_ColDefault') IS NOT NULL

DROP TABLE tempdb..#Tmp_ColDefault;

SELECT

SCH.name AS TABLE_SCHEMA

,TTP.name AS TABLE_NAME

,COL.name AS COLUMN_NAME

,SDC.name AS DefaultName

,SDC.definition AS DefaultDefinition

INTO



#Tmp_ColDefault



FROM



sys.default_constraints SDC

INNER JOIN sys.columns COL

ON SDC.parent_object_id = COL.object_id

AND SDC.parent_column_id = COL.column_id

INNER JOIN sys.table_types TTP

ON COL.object_id = TTP.type_table_object_id

INNER JOIN sys.schemas SCH

ON SCH.schema_id = TTP.schema_id



-- Identity Details

IF OBJECT_ID('tempdb..#Tmp_IdentityDetails') IS NOT NULL

DROP TABLE tempdb..#Tmp_IdentityDetails;

SELECT

SCH.name AS TABLE_SCHEMA

,TTP.name AS TABLE_NAME

,ICL.name AS COLUMN_NAME

,ICL.column_id

,CAST(ICL.seed_value AS INT ) AS seed_value

,CAST(ICL.increment_value AS INT ) AS increment_value

,CAST(ICL.last_value AS BIGINT ) AS last_value

,ICL.object_id

INTO



#Tmp_IdentityDetails



FROM



sys.table_types TTP

INNER JOIN sys.schemas SCH

ON TTP.schema_id = SCH.schema_id

INNER JOIN sys.identity_columns ICL

ON TTP.type_table_object_id = ICL.object_id



-- Update with Filestream data

UPDATE

SET



DM

DM.IsFileStream = FS.IsFileStream



449

www.it-ebooks.info



Chapter 8 ■ Metadata



FROM



#Metadata_ColumnTableType DM

INNER JOIN #Tmp_ColFileStream FS

ON FS.TABLE_SCHEMA = DM.TABLE_SCHEMA

AND FS.TABLE_NAME = DM.TABLE_NAME

AND FS.COLUMN_NAME = DM.COLUMN_NAME



-- Update with Calculated data

UPDATE



DM



SET



DM.CalcDefinition = CC.CalcDefinition

,DM.IsPersisted = CC.IsPersisted



FROM



#Metadata_ColumnTableType DM

INNER JOIN #Tmp_ColCalculated CC

ON DM.TABLE_SCHEMA = CC.TABLE_SCHEMA

AND DM.TABLE_NAME = CC.TABLE_NAME

AND DM.COLUMN_NAME = CC.COLUMN_NAME



-- Update with Defaults

UPDATE



DM



SET



DM.DefaultDefinition = CONVERT(VARCHAR(MAX),CD.DefaultDefinition)

,DM.DefaultName = CD.DefaultName



FROM



#Metadata_ColumnTableType DM

INNER JOIN #Tmp_ColDefault CD

ON DM.TABLE_SCHEMA = CD.TABLE_SCHEMA

AND DM.TABLE_NAME = CD.TABLE_NAME

AND DM.COLUMN_NAME = CD.COLUMN_NAME



-- Update FullText indicator

UPDATE

SET

FROM



Tmp

Tmp.IsFulltextIndexed = 1

#Metadata_ColumnTableType Tmp

INNER JOIN sys.fulltext_index_columns FTI

ON Tmp.object_id = FTI.object_id

AND Tmp.Column_id = FTI.Column_id



-- Update Identity Details

UPDATE

SET



FROM



DM

DM.IdentitySeedValue = Tmp.seed_value

,DM.IdentityIncrementValue = Tmp.increment_value

,DM.IdentityLastValue = Tmp.last_value

#Metadata_ColumnTableType DM

INNER JOIN #Tmp_IdentityDetails TMP

ON DM.object_id = Tmp.object_id

AND DM.column_id = Tmp.column_id



SELECT * from #Metadata_ColumnTableType;



-- Once again I have wickedly used SELECT *



450

www.it-ebooks.info



Chapter 8 ■ Metadata



The output is shown in Figure 8-4.



Figure 8-4. Column metadata returned by the script in Recipe 8-7



How It Works

Returning column metadata can be done, quite simply, using this script, which I hope you find suitably

exhaustive. The column metadata returned is shown in Table 8-7.

Table 8-7. Column Metadata



Field Name



Description



SERVER_NAME



The server name.



DATABASE_NAME



The database name.



TABLE_SCHEMA



The schema of the table.



TABLE_NAME



The table name.



COLUMN_NAME



The column name.



column_id



The internal column ID used by the system metadata.



DataType



The SQL Server data type.



ColLength



The column length.



precision



The column’s numeric precision (the total number of digits).



scale



The column’s numeric scale (the number of digits after the decimal place).



CollationName



The SQL Server collation.



IsNullable



The column can contain NULL values.



IsIdentity



The column is an IDENTITY column.



IsComputed



The column contains a computed value.



IsFileStream



The column is a FILESTREAM column.

(continued)



451

www.it-ebooks.info



Chapter 8 ■ Metadata



Table 8-7.  (continued)



Field Name



Description



IsSparse



The column contains sparse data.



IsColumnSet



The column is part of a column set.



object_id



The internal object ID used by the system metadata.



default_object_id



The ID of any stand-alone default.



ColDefinition



The column definition.



DefaultName



The name of a column default, if there is one.



DefaultDefinition



The column default.



IsFulltextIndexed



The column is indexed using full text indexing.



CalcDefinition



The column calculation for a calculated column.



CHARACTER_SET_NAME



The character set used.



IsPersisted



The column value is persisted in the table.



IsCharCol



The column is a text type.



IdentitySeedValue



The IDENTITY start value.



IdentityIncrementValue



The IDENTITY increment value.



IdentityLastValue



The last IDENTITY value.



DateCreated



The date the column was created.



DateModified



The date the column was modified.



FullTextTypeColumn



The column is part of a full-text index.



IsIndexable



The column can be indexed.



IsRowGuidCol



The column contains a Row GUID.



IsXmlIndexable



The column can support XML indexing.



UsesAnsiTrim



The column uses ANSI TRIMs.



Like the script in the previous recipe, the script takes the @SERVER_NAME and @DATABASE_NAME input

parameters to define the server and to analyze the database. The variables default to the current server and

database, but you can alter this. It then creates a session-scoped temporary table to hold all the required

metadata for column-level information. An initial trawl through the metadata uses sys.tables, sys.types,

sys.schemas, and sys.objects—as well as many COLUMNPROPERTY functions to return a core set of column

metadata. Then a series of secondary processes returns FILESTREAM information, calculated column metadata,

defaults, and IDENTITY metadata, as well as full-text metadata. This data is then used to update the temporary table.



452

www.it-ebooks.info



-



Chapter 8 ■ Metadata



Hints, Tips, and Traps





The data returned from this script can be output to disk, if you prefer, by using

SELECT...INTO instead of a simple SELECT at the end of the script.







You may find that this script returns data that is too detailed for your requirements.

Feel free to reduce the amount of information returned if you do not need it.







Remember that you can use SSMS to view basic metadata. Well, yes—sort of. To begin

with the blindingly obvious, let’s remember that if you can connect to an SQL Server

database in SSMS (and if you have sufficient rights), you can expand a table to see the

column names, data type, and length. You can also expand keys, constraints, triggers,

indexes, and statistics.



8-8. Displaying Microsoft Access Metadata

Problem

You want to analyze metadata for an Access database from which you will be extracting data.



Solution

Use the Access Documenter to provide a rich, detailed, and legible report describing the metadata for some or all

of the tables in an Access database. The following steps explain how.

1.



In Access, click Tools ➤ Analyze ➤ Documenter.



2.



In the Documenter dialog, select the table(s) and queries you wish to analyze.

The resulting dialog box looks something like Figure 8-5.



Figure 8-5.  The Access Documenter dialog box

3.



Click OK. After a few seconds, you should get the Documenter output (see Figure 8-6).



453

www.it-ebooks.info



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



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

8-7. Analyzing SQL Server Column Metadata

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

×