浅析SQLServer中的Scanf与Printf
网络编程 2021-07-05 13:42www.168986.cn编程入门
本篇文章是对SQLServer中的Scanf与Printf进行了详细的分析介绍,需要的朋友参考下
SQLServer中有两个扩展存储过程实现Scanf和Printf功能,恰当的使用它们可以在提取和拼接字符串时大幅度简化SQL代码。
1、xp_sscanf,用它可以分解格式相对固定的字符串,这对于厌倦使用一堆substring和charindex的朋友来说不错。比如前几天的一个帖子中提出的如何分解ip地址,相对简练且通用的代码应该是狼蚁网站SEO优化这样
if (object_id ('f_getip' ) is not null )
drop function f_getip
go
create function dbo . f_getip (@ ip varchar (100 ))
returns @ t table (a int , b int , c int , d int )
as
begin
set @ ip = replace (@ ip , '.' , ' ' )
declare
@ s1 varchar (3 ) , @ s2 varchar (3 ),
@ s3 varchar (3 ) , @ s4 varchar (3 )
exec xp_sscanf @ ip , '%s %s %s %s' , @ s1 output , @ s2 output , @ s3 output , @ s4 output
insert into @ t select @ s1 , @ s2 , @ s3 , @ s4
return
end
go
select from dbo . f_getip ('192.168.0.1' )
go
/
a b c d
----------- ----------- ----------- -----------
192 168 0 1
/
2、xp_sprintf,用它可以拼接出一个字符串而不用担心过多的加号很引号难以控制,比如一个动态执行sql语句的存储过程
if (object_id ('p_select' ) is not null )
drop proc p_select
go
create proc p_select (@ tb varchar (100 ), @ cols varchar (100 ), @ wherecol varchar (100 ), @ value varchar (100 ))
as
begin
declare @ s varchar (8000 )
exec xp_sprintf @ s output , 'select %s from %s where %s=''%s''' , @ cols , @ tb , @ wherecol , @ value
exec (@ s)
end
go
exec p_select 'sysobjects' , 'id,xtype,crdate' , 'name' , 'p_select'
/
id xtype crdate
----------- ----- -----------------------
898102240 P 2009-08-18 03:01:51.153
/
1、xp_sscanf,用它可以分解格式相对固定的字符串,这对于厌倦使用一堆substring和charindex的朋友来说不错。比如前几天的一个帖子中提出的如何分解ip地址,相对简练且通用的代码应该是狼蚁网站SEO优化这样
代码如下:
if (object_id ('f_getip' ) is not null )
drop function f_getip
go
create function dbo . f_getip (@ ip varchar (100 ))
returns @ t table (a int , b int , c int , d int )
as
begin
set @ ip = replace (@ ip , '.' , ' ' )
declare
@ s1 varchar (3 ) , @ s2 varchar (3 ),
@ s3 varchar (3 ) , @ s4 varchar (3 )
exec xp_sscanf @ ip , '%s %s %s %s' , @ s1 output , @ s2 output , @ s3 output , @ s4 output
insert into @ t select @ s1 , @ s2 , @ s3 , @ s4
return
end
go
select from dbo . f_getip ('192.168.0.1' )
go
/
a b c d
----------- ----------- ----------- -----------
192 168 0 1
/
2、xp_sprintf,用它可以拼接出一个字符串而不用担心过多的加号很引号难以控制,比如一个动态执行sql语句的存储过程
代码如下:
if (object_id ('p_select' ) is not null )
drop proc p_select
go
create proc p_select (@ tb varchar (100 ), @ cols varchar (100 ), @ wherecol varchar (100 ), @ value varchar (100 ))
as
begin
declare @ s varchar (8000 )
exec xp_sprintf @ s output , 'select %s from %s where %s=''%s''' , @ cols , @ tb , @ wherecol , @ value
exec (@ s)
end
go
exec p_select 'sysobjects' , 'id,xtype,crdate' , 'name' , 'p_select'
/
id xtype crdate
----------- ----- -----------------------
898102240 P 2009-08-18 03:01:51.153
/
编程语言
- 宿迁百度关键词排名指南:实现精准营销的关键
- 四川SEO优化怎么做网络推广
- 立昂技术备案老域名收购:如何为您的业务赋能
- 安徽百度关键词seo贵不贵,一般需要多少钱
- 吉林百度快照排名怎么做电话营销
- 多伦新手做SEO怎么做
- 甘肃优化关键词排名推广怎么做论坛营销
- 沙雅SEO网站推广:提升您的在线可见性
- 四川SEO优化如何提升销售额和销售量
- 聂荣网站排名优化:提升网站可见性的全方位指
- 涞水SEO:提升地方企业在线可见性的策略
- 辽宁百度seo排名怎样做网站排名
- 临湘哪有关键词排名优化:提升网站可见度的关
- 黑龙江百度网站优化有没有优惠
- 凉城优化关键词排名推广:提升您的网络可见性
- 萝北整站优化:提升您网站流量和排名的全面指