如何在SQL中写正则表达式(适用于Ms SQL Server 2000以上版本)

由于数据库的一个表字段中多包含html标签,现在需要修改数据库的字段把html标签都替换掉。当然我可以通过写一个程序去修改,那毕竟有点麻烦。直接在查询分析器中执行,但是MS SQL Server并没有提供正则表达式,替换html标签可不那么容易。我们可以通过CLR托管来实现一个通过正则表达式来替换html标签的自定义函数。(注:SQL CLR只能在MS SQL Server 2005或以上版本中适用)

第一步:通过Visual Studio 2008新建一个SQL Server项目

 


第二步:配置连接的数据库

点击上一步确定后,出现如下提示框


可以通过 添加新引用 来添加一个数据库连接(只适用MS SQL Server 2005或以上版本,再次提醒哦),添加以后选择其中一个数据库点击确定。

第三步:添加一个自定义函数


然后编写自定义函数的类(需要注意的是类的方法必须是静态的)


第四步:部署,类编写完成后进行部署


部署了以后,你指定的连接数据库下面会生成一个自定义函数和一个程序集(如图)

然后就可以调用自定义函数了,不过调用自定义函数前,你还得启用一些配置

EXEC sp_configure 'allow updates',0
EXEC sp_configure 'show advanced options', 1 -- make them available
reconfigure
EXEC sp_configure 'clr enabled', 1 -- turn on OLE
reconfigure

启用这些配置后,你就可以调用刚刚自定义的函数了.

调用

select dbo.ReplaceHtml('<p>aaa</p>','<(.[^>]*)>','')

结果为:aaa

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PHP 手册 Mehdi Achour Friedhelm Betz Antony Dovgal Nuno Lopes Hannes Magnusson Georg Richter Damien Seguy Jakub Vrana 其他贡献者 2007-12-10 Philip Olson 中文翻译人员: 王远之 肖理达 肖盛文 黄啸宇 宋琪 陈伯乐 刘铭 © 1997-2007 PHP 文档组 版权信息 前言 入门指引 简介 简明教程 安装与配置 安装前需要考虑的事项 Unix 系统下的安装 Mac OS X 系统下的安装 Windows 系统下的安装 PECL 扩展库安装 还有问题? 运行时配置 语言参考 基本语法 类型 变量 常量 表达式 运算符 控制结构 函数 类与对象(PHP 4) 类与对象(PHP 5) Namespaces 异常处理 引用的解释 安全 简介 总则 以 CGI 模式安装时 以 Apache 模块安装时 文件系统安全 数据库安全 错误报告 使用 Register Globals 用户提交的数据 魔术引号 隐藏 PHP 保持更新 特点 用 PHP 进行 HTTP 认证 Cookie 会话 处理 XForms 文件上传处理 使用远程文件 连接处理 数据库永久连接 安全模式 PHP 的命令行模式 函数参考 .NET — .NET 函数 Apache — Apache 特有函数 APC — APC 可选 PHP 缓存 APD — APD 高级 PHP 调试器 Arrays — Array 数组函数 Aspell — Aspell 函数(已废弃) BBCode — BBCode Functions BC math — BC math 高精度数学函数 bcompiler — BCompiler PHP 字节码编译器 Bzip2 — Bzip2 压缩函数 Calendar — Calendar 日历函数 CCVS — CCVS API 函数(已废弃) Classes/Objects — Classes/Objects 类/对象函数 Classkit — Classkit 函数 ClibPDF — ClibPDF Functions [deprecated] COM — COM 和 .Net(Windows)函数 Crack — Crack Functions ctype — Character Type Functions CURL — CURL, Client URL Library Functions Cybercash — Cybercash Payment Functions CyberMUT — Credit Mutuel CyberMUT functions Cyrus IMAP — Cyrus IMAP administration Functions Date/Time — Date/Time 日期/时间函数 DB++ — DB++ Functions dba — Database (dbm-style) Abstraction Layer Functions dBase — dBase Functions DBM — DBM Functions [deprecated] dbx — dbx Functions Direct IO — Direct IO Functions Directories — Directory 目录函数 DOM — DOM Functions DOM XML — DOM XML Functions enchant — enchant Functions Errors and Logging — Error Handling and Logging Functions Exif — Exif Functions Expect — Expect Functions fam — File Alteration Monitor Functions FDF — Forms Data Format Functions Fileinfo — Fileinfo Functions filePro — filePro Functions Filesystem — Filesystem 文件系统函数 Filter — Filter Functions Firebird/InterBase — Firebird/InterBase Functions Firebird/Interbase (PDO) — Firebird/Interbase Functions (PDO_FIREBIRD) FriBiDi — FriBiDi Functions FrontBase — FrontBase Functions FTP — FTP 函数 Function handling — Function Handling Functions GeoIP — GeoIP Functions gettext — Gettext Functions GMP — GMP Functions gnupg — gnupg 函数 gopher — Net_Gopher haru — Haru PDF Functions hash — hash Functions HTTP — HTTP 函数 Hyperwave — Hyperwave Functions Hyperwave API — Hyperwave API Functions i18n — i18n 函数 IBM (PDO) — IBM Functions (PDO_IBM) ibm_db2 — IBM DB2, Cloudscape and Apache Derby Functions iconv — iconv Functions id3 — ID3 Functions IIS Functions — IIS Administration Functions Image — Image 图像函数 Imagick Image Library IMAP — IMAP, POP3 and NNTP Functions Informix — Informix Functions Informix (PDO) — Informix Functions (PDO_INFORMIX) Ingres II — Ingres II Functions IRC Gateway — IRC Gateway Functions Java — PHP / Java Integration JSON — JSON 函数 kadm5 — KADM5 LDAP — LDAP Functions libxml — libxml Functions Lotus Notes — Lotus Notes Functions LZF — LZF Functions Mail — Mail 邮件函数 Mailparse — Mailparse Functions Math — Math 数学函数 MaxDB — MaxDB PHP Extension MCAL — MCAL Functions mcrypt — Mcrypt Encryption Functions MCVE — MCVE (Monetra) Payment Functions Memcache — Memcache Functions mhash — Mhash Functions Mimetype — Mimetype Functions Ming (flash) — Ming functions for Flash Misc. — Miscellaneous Functions mnoGoSearch — mnoGoSearch Functions MS SQL Server — Microsoft SQL Server Functions MS SQL Server (PDO) — Microsoft SQL Server and Sybase Functions (PDO_DBLIB) Msession — Mohawk Software Session Handler Functions mSQL — mSQL Functions Multibyte String — Multibyte String Functions muscat — muscat Functions MySQL — MySQL 函数 MySQL (PDO) — MySQL Functions (PDO_MYSQL) mysqli — MySQLi 扩展库 Ncurses — Ncurses Terminal Screen Control Functions Network — Network Functions Newt — Newt Functions NSAPI — NSAPI-specific Functions Object Aggregation — Object Aggregation/Composition Functions Object overloading — Object property and method call overloading OCI8 — Oracle 函数 ODBC — ODBC Functions (Unified) ODBC and DB2 (PDO) — ODBC and DB2 Functions (PDO_ODBC) OGG/Vorbis — oggvorbis openal — OpenAL Audio Bindings OpenSSL — OpenSSL Functions Oracle — Oracle 函数(已废弃) Oracle (PDO) — Oracle Functions (PDO_OCI) Output Control — Output Control 输出控制函数 OvrimosSQL — Ovrimos SQL Functions Paradox — Paradox File Access Parsekit — Parsekit Functions PCNTL — Process Control Functions PCRE — Perl 兼容正则表达式函数 PDF — PDF 函数 PDO — PDO Functions phar — Phar archive stream and classes PHP Options/Info — PHP Options&Information POSIX — POSIX Functions POSIX Regex — POSIX 扩展正则表达式函数 PostgreSQL — PostgreSQL 数据库函数 PostgreSQL (PDO) — PostgreSQL Functions (PDO_PGSQL) Printer — Printer Functions Program Execution — Program Execution Functions PS — PostScript document creation Pspell — Pspell Functions qtdom — qtdom Functions radius — Radius Rar — Rar Functions Readline — GNU Readline Recode — GNU Recode Functions RPMReader — RPM Header Reading 函数 runkit — runkit Functions SAM — SAM - Simple Asynchronous Messaging Satellite — Satellite CORBA client extension [deprecated] SCA — SCA Functions SDO — SDO Functions SDO DAS XML — SDO XML Data Access Service Functions SDO-DAS-Relational — SDO Relational Data Access Service Functions Semaphore — Semaphore, Shared Memory and IPC Functions SESAM — SESAM Database Functions Session PgSQL — PostgreSQL Session Save Handler Sessions — Session 会话处理函数 shmop — Shared Memory Functions SimpleXML — SimpleXML functions SNMP — SNMP 函数 SOAP — SOAP Functions Sockets — Socket Functions SPL — Standard PHP Library (SPL) Functions SQLite — SQLite Functions SQLite (PDO) — SQLite Functions (PDO_SQLITE) ssh2 — Secure Shell2 Functions Statistics — Statistics Functions Streams — Stream Functions Strings — String 字符串处理函数 SVN — Subversion 函数 SWF — Shockwave Flash Functions swish — Swish Functions Sybase — Sybase Functions TCP Wrappers — TCP Wrappers Functions tidy — Tidy Functions Tokenizer — Tokenizer Functions Unicode — Unicode Functions URLs — URL 函数 Variables — Variable 变量函数 Verisign Payflow Pro — Verisign Payflow Pro Functions vpopmail — vpopmail Functions W32api — W32api 函数 WDDX — WDDX Functions win32ps — win32ps Functions win32service — win32service Functions xattr — xattr Functions xdiff — xdiff Functions XML — XML 语法解析函数 XML-RPC — XML-RPC 函数 XMLReader — XMLReader functions XMLWriter — XMLWriter Functions XSL — XSL functions XSLT — XSLT Functions YAZ — YAZ Functions YP/NIS — YP/NIS Functions Zip — Zip File Functions Zlib — Zlib Compression Functions PHP at the Core: A Hacker's Guide to the Zend Engine The PHP 5 build system Extension structure Memory management Working with variables Writing functions Working with classes and objects Working with resources Working with INI settings Working with streams PDO Driver How-To Extension FAQs Zend Engine 2 API reference Zend Engine 1 The future: PHP 6 and Zend Engine 3 FAQ — FAQ:常见问题 一般信息 邮件列表 获取 PHP 数据库问题 安装 — 安装常见问题 编译问题 使用 PHP PHP 和 HTML PHP 和 COM PHP 和其它语言 从 PHP/FI 2 移植到 PHP 3 从 PHP 3 移植到 PHP 4 从 PHP 4 移植到 PHP 5 杂类问题 附录 PHP 及其相关工程的历史 Migrating from PHP 5.1.x to PHP 5.2.x Migrating from PHP 5.0.x to PHP 5.1.x 从 PHP 4 移植到 PHP 5 从 PHP 3 移植到 PHP 4 从 PHP/FI 2 移植到 PHP 3 PHP 的调试 配置选项 php.ini 配置选项 所支持时区列表 扩展库分类 函数别名列表 保留字列表 资源类型列表 支持的协议/封装协议列表 可用过滤器列表 所支持的套接字传输器(Socket Transports)列表 PHP 类型比较表 解析器代号列表 Userland Naming Guide 关于本手册 开放出版许可协议 函数索引 补充说明

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值