惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
S
Securelist
Y
Y Combinator Blog
云风的 BLOG
云风的 BLOG
A
About on SuperTechFans
Martin Fowler
Martin Fowler
量子位
D
Docker
G
Google Developers Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
U
Unit 42
S
Secure Thoughts
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
K
Kaspersky official blog
NISL@THU
NISL@THU
T
Tailwind CSS Blog
I
Intezer
T
Threat Research - Cisco Blogs
P
Privacy & Cybersecurity Law Blog
D
DataBreaches.Net
Engineering at Meta
Engineering at Meta
C
Cyber Attacks, Cyber Crime and Cyber Security
F
Full Disclosure
Microsoft Security Blog
Microsoft Security Blog
Cisco Talos Blog
Cisco Talos Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Simon Willison's Weblog
Simon Willison's Weblog
Vercel News
Vercel News
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
F
Fortinet All Blogs
W
WeLiveSecurity
C
Cisco Blogs
Security Latest
Security Latest
PCI Perspectives
PCI Perspectives
L
LangChain Blog
P
Palo Alto Networks Blog
IT之家
IT之家
aimingoo的专栏
aimingoo的专栏
Apple Machine Learning Research
Apple Machine Learning Research
宝玉的分享
宝玉的分享
Blog — PlanetScale
Blog — PlanetScale
Help Net Security
Help Net Security
Microsoft Azure Blog
Microsoft Azure Blog
Project Zero
Project Zero
G
GRAHAM CLULEY
Recent Commits to openclaw:main
Recent Commits to openclaw:main
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
L
Lohrmann on Cybersecurity
Cloudbric
Cloudbric
D
Darknet – Hacking Tools, Hacker News & Cyber Security

博客园 - Sherrys

Content-Type 参数 Sql 2000 中行转列的查询方法 DotNet Framework 小技巧 通过 INotifyPropertyChanged 实现观察者模式 使用 .NET 2.0 SecureString 类保护敏感数据 API参数说明符前缀详解 C#中调用Windows API的要点 - Sherrys - 博客园 SQL 处理简单的 Xml Special Considerations When Using Query Notifications 利用.NET Framework使用RSS feed SQL远程连接 SQL 事务的隔离 SELECT 语句收藏(2000 & 2005) SQL事务的使用 用JS让文章内容指定的关键字加亮 - Sherrys - 博客园 查询SQL连接数的方法 对象的继承方案 使用 ICallbackEventHandler aspx页面中的DropDownList 的 SelectValue 出现中文导致不回调方法的问题 如何利用SQL Server 2005数据库快照形成报表
自动处理 SQL 2005 表格数据
Sherrys · 2007-03-02 · via 博客园 - Sherrys

如果有这样一种情况:

有很大量的数据,而且重要的是表非常的多,但是需要经过很多的处理才可以使用,而且表的数据结构又不都相同。如果在这些数据里面存在一些无用的数据和字符,比如空格一些非法字符的话,要处理起来是否是一件很头痛的事情呢?

    在SQL Server 2005的INFORMATION_SCHEMA模式中有大量的有用的系统视图, 而可以帮助处理的是INFORMATION_SCHEMA.COLUMNS。表里面一些有用的信息如:

  • Table_Catalog:数据库名

  • Table_Schema:模式

  • Table_Name:表名

  • Column_Name:列名

  • Ordinal_Position:列数

  • Column_Default:默认值

  • Is_Nullable:有无数据

  • Data_Type:指明该列的数据类型

SELECT * FROM INFORMATION_SCHEMA.COLUMNS

WHERE TABLE_NAME = 'ProductDescription'

通过上面的代码,可以查询出有关 ProductDescription 表的一些有趣的信息:

TABLE_CATALOG

TABLE_SCHEMA

TABLE_NAME

COLUMN_NAME

AdventureWorks

Production

ProductDescription

ProductDescriptionID

AdventureWorks

Production

ProductDescription

Description

AdventureWorks

Production

ProductDescription

rowguid

AdventureWorks

Production

ProductDescription

ModifiedDate

现在使用SQL产生UPDATE语句用来处理数据表中的所有数据。

现在我只处理列中含有char和varchar这两种数据类型的数据。

SELECT

    'Update '+ Table_Name +' SET '+ Column_Name +' = Ltrim(RTrim( '+ Column_Name +'))'FROM INFORMATION_SCHEMA.COLUMNS

WHERE

    TABLE_NAME ='Contact' AND Data_Type IN('char','varchar','nchar','nvarchar')

    然后出来的结果是:

Update Contact SET Title = Ltrim(RTrim( Title))

Update Contact SET FirstName = Ltrim(RTrim( FirstName))

Update Contact SET MiddleName = Ltrim(RTrim( MiddleName))

Update Contact SET LastName = Ltrim(RTrim( LastName))

Update Contact SET Suffix = Ltrim(RTrim( Suffix))

Update Contact SET EmailAddress = Ltrim(RTrim( EmailAddress))

Update Contact SET Phone = Ltrim(RTrim( Phone))

Update Contact SET PasswordHash = Ltrim(RTrim( PasswordHash))

Update Contact SET PasswordSalt = Ltrim(RTrim( PasswordSalt))

那么之后需要只需要把这些语句整体起来,一起运行就可以了。Contact 表可以通过SELECT * FROM Person.Contact查询出来。

如果在一个模式中处理所有数据表,只要简单的改一下WHERE条件语句的table_name用Schema_Name来代替,如:

SELECT

    'Update '+ Table_Name +' SET '+ Column_Name +' = Ltrim(RTrim( '+ Column_Name +'))'

FROM

    INFORMATION_SCHEMA.COLUMNS C

WHERE

    TABLE_SCHEMA ='Production'

AND

Data_Type IN('char','varchar','nchar','nvarchar')

    但是此查询结果却包含着视图,对视图的更新却又毫无意义,怎么过滤它呢?在这里可以通过INFORMATION_SCHEMA.TABLES视图来处理,如:

SELECT

    'Update '+ Table_Name +' SET '+ Column_Name +' = Ltrim(Trim( '+ Column_Name +'))'

FROM

    INFORMATION_SCHEMA.COLUMNS C

WHERE

    TABLE_SCHEMA ='Production'

AND

    Data_Type IN('char','varchar','nchar','nvarchar')

AND

    EXISTS

    (

        SELECT

            Table_Name

        FROM

            INFORMATION_SCHEMA.TABLES T

        WHERE

            C.Table_Name = T.Table_Name

        AND

            Table_Type <>'VIEW'

    )

    或者让Table_Type = 'BASE TABLE'也可以达到目的。

    通过此方式,还可以产生许多批量的SQL语句。可以让重复进行的工作简单化了。