SQL操作全集

SQL分类:
DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE)
DML—数据操纵语言(SELECT,DELETE,UPDATE,INSERT)
DCL—数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK)
首先,简要介绍基础语句:
1、说明:创建数据库
CREATE DATABASE database-name
2、说明:删除数据库
drop database dbname
3、说明:备份sql server
--- 创建 备份数据的 device
USE master
EXEC sp_addumpdevice 'disk', 'testBack', 'c:/mssql7backup/MyNwind_1.dat'
--- 开始 备份
BACKUP DATABASE pubs TO testBack
4、说明:创建新表
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)
根据已有的表创建新表:
A:create table tab_new like tab_old (使用旧表创建新表)
B:create table tab_new as select col1,col2… from tab_old definition only
5、说明:删除新表drop table tabname
6、说明:增加一个列
Alter table tabname add column col type
注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。
7、说明:添加主键: Alter table tabname add primary key(col)
说明:删除主键: Alter table tabname drop primary key(col)
8、说明:创建索引:create [unique] index idxname on tabname(col….)
删除索引:drop index idxname
注:索引是不可更改的,想更改必须删除重新建。
9、说明:创建视图:create view viewname as select statement
删除视图:drop view viewname
10、说明:几个简单的基本的sql语句
选择:select * from table1 where 范围
插入:insert into table1(field1,field2) values(value1,value2)
删除:delete from table1 where 范围
更新:update table1 set field1=value1 where 范围
查找:select * from table1 where field1 like ’%value1%’ ---like的语法很精妙,查资料!
排序:select * from table1 order by field1,field2 [desc]
总数:select count * as totalcount from table1
求和:select sum(field1) as sumvalue from table1
平均:select avg(field1) as avgvalue from table1
最大:select max(field1) as maxvalue from table1
最小:select min(field1) as minvalue from table1
11、说明:几个高级查询运算词
A: UNION 运算符
UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。当 ALL 随 UNION 一起使用时(即 UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。
B: EXCEPT 运算符
EXCEPT 运算符通过包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表。当 ALL 随 EXCEPT 一起使用时 (EXCEPT ALL),不消除重复行。
C: INTERSECT 运算符
INTERSECT 运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表。当 ALL 随 INTERSECT 一起使用时 (INTERSECT ALL),不消除重复行。
注:使用运算词的几个查询结果行必须是一致的。
12、说明:使用外连接
A、left outer join:
左外连接(左连接):结果集几包括连接表的匹配行,也包括左连接表的所有行。
SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
B:right outer join:
右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。
C:full outer join:
全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。

其次,大家来看一些不错的sql语句
1、说明:复制表(只复制结构,源表名:a 新表名:b) (Access可用)
法一:select * into b from a where 1 <>1
法二:select top 0 * into b from a

2、说明:拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用)
insert into b(a, b, c) select d,e,f from b;

3、说明:跨数据库之间表的拷贝(具体数据使用绝对路径) (Access可用)
insert into b(a, b, c) select d,e,f from b in ‘具体数据库’ where 条件
例子:..from b in '"&Server.MapPath(".")&"/data.mdb" &"' where..

4、说明:子查询(表名1:a 表名2:b)
select a,b,c from a where a IN (select d from b ) 或者: select a,b,c from a where a IN (1,2,3)

5、说明:显示文章、提交人和最后回复时间
select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b

6、说明:外连接查询(表名1:a 表名2:b)
select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c

7、说明:在线视图查询(表名1:a )
select * from (SELECT a,b,c FROM a) T where t.a > 1;

8、说明:between的用法,between限制查询数据范围时包括了边界值,not between不包括
select * from table1 where time between time1 and time2
select a,b,c, from table1 where a not between 数值1 and 数值2

9、说明:in 的使用方法
select * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’)

10、说明:两张关联表,删除主表中已经在副表中没有的信息
delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )

11、说明:四表联查问题:
select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where .....

12、说明:日程安排提前五分钟提醒
SQL: select * from 日程安排 where datediff('minute',f开始时间,getdate())>5

13、说明:一条sql 语句搞定数据库分页
select top 10 b.* from (select top 20 主键字段,排序字段 from 表名 order by 排序字段 desc) a,表名 b where b.主键字段 = a.主键字段 order by a.排序字段

14、说明:前10条记录
select top 10 * form table1 where 范围

15、说明:选择在每一组b值相同的数据中对应的a最大的记录的所有信息(类似这样的用法可以用于论坛每月排行榜,每月热销产品分析,按科目成绩排名,等等.)
select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)

16、说明:包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重复行而派生出一个结果表
(select a from tableA ) except (select a from tableB) except (select a from tableC)

17、说明:随机取出10条数据
select top 10 * from tablename order by newid()

18、说明:随机选择记录
select newid()

19、说明:删除重复记录
Delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)

20、说明:列出数据库里所有的表名
select name from sysobjects where type='U'

21、说明:列出表里的所有的
select name from syscolumns where id=object_id('TableName')

22、说明:列示type、vender、pcs字段,以type字段排列,case可以方便地实现多重选择,类似select 中的case。
select type,sum(case vender when 'A' then pcs else 0 end),sum(case vender when 'C' then pcs else 0 end),sum(case vender when 'B' then pcs else 0 end) FROM tablename group by type
显示结果:
type vender pcs
电脑 A 1
电脑 A 1
光盘 B 2
光盘 A 2
手机 B 3
手机 C 3

23、说明:初始化表table1
TRUNCATE TABLE table1

24、说明:选择从10到15的记录
select top 5 * from (select top 15 * from table order by id asc) table_别名 order by id desc

随机选择数据库记录的方法(使用Randomize函数,通过SQL语句实现)
对存储在数据库中的数据来说,随机数特性能给出上面的效果,但它们可能太慢了些。你不能要求ASP“找个随机数”然后打印出来。实际上常见的解决方案是建立如下所示的循环:
Randomize
RNumber = Int(Rnd*499) +1

While Not objRec.EOF
If objRec("ID") = RNumber THEN
... 这里是执行脚本 ...
end if
objRec.MoveNext
Wend

这很容易理解。首先,你取出1到500范围之内的一个随机数(假设500就是数据库内记录的总数)。然后,你遍历每一记录来测试ID 的值、检查其是否匹配RNumber。满足条件的话就执行由THEN 关键字开始的那一块代码。假如你的RNumber 等于495,那么要循环一遍数据库花的时间可就长了。虽然500这个数字看起来大了些,但相比更为稳固的企业解决方案这还是个小型数据库了,后者通常在一个数据库内就包含了成千上万条记录。这时候不就死定了?
采用SQL,你就可以很快地找出准确的记录并且打开一个只包含该记录的recordset,如下所示:
Randomize
RNumber = Int(Rnd*499) + 1

SQL = "SELECT * FROM Customers WHERE ID = " & RNumber

set objRec = ObjConn.Execute(SQL)
Response.WriteRNumber & " = " & objRec("ID") & " " & objRec("c_email")

不必写出RNumber 和ID,你只需要检查匹配情况即可。只要你对以上代码的工作满意,你自可按需操作“随机”记录。Recordset没有包含其他内容,因此你很快就能找到你需要的记录这样就大大降低了处理时间。
再谈随机数
现在你下定决心要榨干Random 函数的最后一滴油,那么你可能会一次取出多条随机记录或者想采用一定随机范围内的记录。把上面的标准Random 示例扩展一下就可以用SQL应对上面两种情况了。
为了取出几条随机选择的记录并存放在同一recordset内,你可以存储三个随机数,然后查询数据库获得匹配这些数字的记录:
SQL = "SELECT * FROM Customers WHERE ID = " & RNumber & " OR ID = " & RNumber2 & " OR ID = " & RNumber3

假如你想选出10条记录(也许是每次页面装载时的10条链接的列表),你可以用BETWEEN 或者数学等式选出第一条记录和适当数量的递增记录。这一操作可以通过好几种方式来完成,但是 SELECT 语句只显示一种可能(这里的ID 是自动生成的号码):
SQL = "SELECT * FROM Customers WHERE ID BETWEEN " & RNumber & " AND " & RNumber & "+ 9"

注意:以上代码的执行目的不是检查数据库内是否有9条并发记录。


随机读取若干条记录,测试过
Access语法:SELECT top 10 * From 表名 ORDER BY Rnd(id)
Sql server:select top n * from 表名 order by newid()
mysqlelect * From 表名 Order By rand() Limit n
Access左连接语法(最近开发要用左连接,Access帮助什么都没有,网上没有Access的SQL说明,只有自己测试, 现在记下以备后查)
语法elect table1.fd1,table1,fd2,table2.fd2 From table1 left join table2 on table1.fd1,table2.fd1 where ...
使用SQL语句 用...代替过长的字符串显示
语法:
SQL数据库:select case when len(field)>10 then left(field,10)+'...' else field end as news_name,news_id from tablename
Access数据库:SELECT iif(len(field)>2,left(field,2)+'...',field) FROM tablename;

Conn.Execute说明
Execute方法
该方法用于执行SQL语句。根据SQL语句执行后是否返回记录集,该方法的使用格式分为以下两种:
1.执行SQL查询语句时,将返回查询得到的记录集。用法为:
Set 对象变量名=连接对象.Execute("SQL 查询语言")
Execute方法调用后,会自动创建记录集对象,并将查询结果存储在该记录对象中,通过Set方法,将记录集赋给指定的对象保存,以后对象变量就代表了该记录集对象。

2.执行SQL的操作性语言时,没有记录集的返回。此时用法为:
连接对象.Execute "SQL 操作性语句" [, RecordAffected][, Option]
·RecordAffected 为可选项,此出可放置一个变量,SQL语句执行后,所生效的记录数会自动保存到该变量中。通过访问该变量,就可知道SQL语句队多少条记录进行了操作。
·Option 可选项,该参数的取值通常为adCMDText,它用于告诉ADO,应该将Execute方法之后的第一个字符解释为命令文本。通过指定该参数,可使执行更高效。

 

 

/*----------------------------------------------可用函数------------------------------------------------------*/

 

/*
返回汉字大写首字母select dbo.fun_getPYFirst('杭州大厦')
*/

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER  function [dbo].[fun_getPYFirst]
 (
    @str nvarchar(4000)
 )
returns nvarchar(4000)
as
begin

  declare @word nchar(1),@PY nvarchar(4000)

  set @PY=''

  while len(@str)>0
  begin
    set @word=left(@str,1)

    --如果非汉字字符,返回原字符
    set @PY=@PY+(case when unicode(@word) between 19968 and 19968+20901
               then ( 
                            select top 1 PY 
                            from 
                            ( 
                             select 'A' as PY,N'驁' as word
                             union all select 'B',N'簿'
                             union all select 'C',N'錯'
                     union all select 'D',N'鵽'
                     union all select 'E',N'樲'
                     union all select 'F',N'鰒'
                     union all select 'G',N'腂'
                     union all select 'H',N'夻'
                     union all select 'J',N'攈'
                     union all select 'K',N'穒'
                     union all select 'L',N'鱳'
                     union all select 'M',N'旀'
                     union all select 'N',N'桛'
                     union all select 'O',N'漚'
                     union all select 'P',N'曝'
                     union all select 'Q',N'囕'
                     union all select 'R',N'鶸'
                     union all select 'S',N'蜶'
                     union all select 'T',N'籜'
                     union all select 'W',N'鶩'
                     union all select 'X',N'鑂'
                     union all select 'Y',N'韻'
                     union all select 'Z',N'咗'
                      ) T 
                   where word>=@word collate Chinese_PRC_CS_AS_KS_WS 
                   order by PY ASC
                          ) 
                      else @word 
                 end)
    set @str=right(@str,len(@str)-1)
  end

  return @PY

end

 

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

----------------------------------------------------------------------------------
/*
生成汉字全拼
select dbo.fun_getPYFull('杭州大厦')
*/

ALTER function [dbo].[fun_getPYFull](@str varchar(100))
returns varchar(4000)
as
begin
if(isnull(@str,'') = '')
 return('')
 declare @re varchar(4000)


 --生成临时表
 declare @t table(chr nchar(1) collate  Chinese_PRC_CS_AS_KS_WS,py nvarchar(20))
 insert into @t select'吖','a'
 insert into @t select'厑','aes'
 insert into @t select'哎','ai'
 insert into @t select'安','an'
 insert into @t select'肮','ang'
 insert into @t select'凹','ao'
 insert into @t select'八','ba'
 insert into @t select'挀','bai'
 insert into @t select'兡','baike'
 insert into @t select'瓸','baiwa'
 insert into @t select'扳','ban'
 insert into @t select'邦','bang'
 insert into @t select'勹','bao'
 insert into @t select'萡','be'
 insert into @t select'陂','bei'
 insert into @t select'奔','ben'
 insert into @t select'伻','beng'
 insert into @t select'皀','bi'
 insert into @t select'边','bian'
 insert into @t select'辪','uu'
 insert into @t select'灬','biao'
 insert into @t select'憋','bie'
 insert into @t select'汃','bin'
 insert into @t select'冫','bing'
 insert into @t select'癶','bo'
 insert into @t select'峬','bu'
 insert into @t select'嚓','ca'
 insert into @t select'偲','cai'
 insert into @t select'乲','cal'
 insert into @t select'参','can'
 insert into @t select'仓','cang'
 insert into @t select'撡','cao'
 insert into @t select'冊','ce'
 insert into @t select'膥','cen'
 insert into @t select'噌','ceng'
 insert into @t select'硛','ceok'
 insert into @t select'岾','ceom'
 insert into @t select'猠','ceon'
 insert into @t select'乽','ceor'
 insert into @t select'叉','cha'
 insert into @t select'犲','chai'
 insert into @t select'辿','chan'
 insert into @t select'伥','chang'
 insert into @t select'抄','chao'
 insert into @t select'车','che'
 insert into @t select'抻','chen'
 insert into @t select'阷','cheng'
 insert into @t select'吃','chi'
 insert into @t select'充','chong'
 insert into @t select'抽','chou'
 insert into @t select'出','chu'
 insert into @t select'膗','chuai'
 insert into @t select'巛','chuan'
 insert into @t select'刅','chuang'
 insert into @t select'吹','chui'
 insert into @t select'旾','chun'
 insert into @t select'踔','chuo'
 insert into @t select'呲','ci'
 insert into @t select'嗭','cis'
 insert into @t select'从','cong'
 insert into @t select'凑','cou'
 insert into @t select'粗','cu'
 insert into @t select'汆','cuan'
 insert into @t select'崔','cui'
 insert into @t select'邨','cun'
 insert into @t select'瑳','cuo'
 insert into @t select'撮','chua'
 insert into @t select'咑','da'
 insert into @t select'呔','dai'
 insert into @t select'丹','dan'
 insert into @t select'当','dang'
 insert into @t select'刀','dao'
 insert into @t select'恴','de'
 insert into @t select'揼','dem'
 insert into @t select'扥','den'
 insert into @t select'灯','deng'
 insert into @t select'仾','di'
 insert into @t select'嗲','dia'
 insert into @t select'敁','dian'
 insert into @t select'刁','diao'
 insert into @t select'爹','die'
 insert into @t select'哋','dei'
 insert into @t select'嚸','dim'
 insert into @t select'丁','ding'
 insert into @t select'丟','diu'
 insert into @t select'东','dong'
 insert into @t select'吺','dou'
 insert into @t select'剢','du'
 insert into @t select'耑','duan'
 insert into @t select'叾','dug'
 insert into @t select'垖','dui'
 insert into @t select'吨','dun'
 insert into @t select'咄','duo'
 insert into @t select'妸','e'
 insert into @t select'奀','en'
 insert into @t select'鞥','eng'
 insert into @t select'仒','eo'
 insert into @t select'乻','eol'
 insert into @t select'旕','eos'
 insert into @t select'儿','er'
 insert into @t select'发','fa'
 insert into @t select'帆','fan'
 insert into @t select'匚','fang'
 insert into @t select'飞','fei'
 insert into @t select'吩','fen'
 insert into @t select'丰','feng'
 insert into @t select'瓰','fenwa'
 insert into @t select'覅','fiao'
 insert into @t select'仏','fo'
 insert into @t select'垺','fou'
 insert into @t select'夫','fu'
 insert into @t select'猤','fui'
 insert into @t select'旮','ga'
 insert into @t select'侅','gai'
 insert into @t select'甘','gan'
 insert into @t select'冈','gang'
 insert into @t select'皋','gao'
 insert into @t select'戈','ge'
 insert into @t select'给','gei'
 insert into @t select'根','gen'
 insert into @t select'更','geng'
 insert into @t select'啹','geu'
 insert into @t select'喼','gib'
 insert into @t select'嗰','go'
 insert into @t select'工','gong'
 insert into @t select'兝','gongfen'
 insert into @t select'兣','gongli'
 insert into @t select'勾','gou'
 insert into @t select'估','gu'
 insert into @t select'瓜','gua'
 insert into @t select'乖','guai'
 insert into @t select'关','guan'
 insert into @t select'光','guang'
 insert into @t select'归','gui'
 insert into @t select'丨','gun'
 insert into @t select'呙','guo'
 insert into @t select'妎','ha'
 insert into @t select'咍','hai'
 insert into @t select'乤','hal'
 insert into @t select'兯','han'
 insert into @t select'魧','hang'
 insert into @t select'茠','hao'
 insert into @t select'兞','haoke'
 insert into @t select'诃','he'
 insert into @t select'黒','hei'
 insert into @t select'拫','hen'
 insert into @t select'亨','heng'
 insert into @t select'囍','heui'
 insert into @t select'乊','ho'
 insert into @t select'乥','hol'
 insert into @t select'叿','hong'
 insert into @t select'齁','hou'
 insert into @t select'乎','hu'
 insert into @t select'花','hua'
 insert into @t select'徊','huai'
 insert into @t select'欢','huan'
 insert into @t select'巟','huang'
 insert into @t select'灰','hui'
 insert into @t select'昏','hun'
 insert into @t select'吙','huo'
 insert into @t select'嚿','geo'
 insert into @t select'夻','hwa'
 insert into @t select'丌','ji'
 insert into @t select'加','jia'
 insert into @t select'嗧','jialun'
 insert into @t select'戋','jian'
 insert into @t select'江','jiang'
 insert into @t select'艽','jiao'
 insert into @t select'阶','jie'
 insert into @t select'巾','jin'
 insert into @t select'坕','jing'
 insert into @t select'冂','jiong'
 insert into @t select'丩','jiu'
 insert into @t select'欍','jou'
 insert into @t select'凥','ju'
 insert into @t select'姢','juan'
 insert into @t select'噘','jue'
 insert into @t select'军','jun'
 insert into @t select'咔','ka'
 insert into @t select'开','kai'
 insert into @t select'乫','kal'
 insert into @t select'刊','kan'
 insert into @t select'冚','hem'
 insert into @t select'砊','kang'
 insert into @t select'尻','kao'
 insert into @t select'坷','ke'
 insert into @t select'肎','ken'
 insert into @t select'劥','keng'
 insert into @t select'巪','keo'
 insert into @t select'乬','keol'
 insert into @t select'唟','keos'
 insert into @t select'厼','keum'
 insert into @t select'怾','ki'
 insert into @t select'空','kong'
 insert into @t select'廤','kos'
 insert into @t select'抠','kou'
 insert into @t select'扝','ku'
 insert into @t select'夸','kua'
 insert into @t select'蒯','kuai'
 insert into @t select'宽','kuan'
 insert into @t select'匡','kuang'
 insert into @t select'亏','kui'
 insert into @t select'坤','kun'
 insert into @t select'拡','kuo'
 insert into @t select'穒','kweok'
 insert into @t select'垃','la'
 insert into @t select'来','lai'
 insert into @t select'兰','lan'
 insert into @t select'啷','lang'
 insert into @t select'捞','lao'
 insert into @t select'仂','le'
 insert into @t select'雷','lei'
 insert into @t select'塄','leng'
 insert into @t select'唎','li'
 insert into @t select'俩','lia'
 insert into @t select'嫾','lian'
 insert into @t select'簗','liang'
 insert into @t select'蹽','liao'
 insert into @t select'毟','lie'
 insert into @t select'厸','lin'
 insert into @t select'伶','ling'
 insert into @t select'溜','liu'
 insert into @t select'瓼','liwa'
 insert into @t select'囖','lo'
 insert into @t select'龙','long'
 insert into @t select'娄','lou'
 insert into @t select'噜','lu'
 insert into @t select'驴','lv'
 insert into @t select'寽','lue'
 insert into @t select'孪','luan'
 insert into @t select'掄','lun'
 insert into @t select'頱','luo'
 insert into @t select'呣','m'
 insert into @t select'妈','ma'
 insert into @t select'吗','ma'
 insert into @t select'遤','hweong'
 insert into @t select'埋','mai'
 insert into @t select'颟','man'
 insert into @t select'牤','mang'
 insert into @t select'匁','mangmi'
 insert into @t select'猫','mao'
 insert into @t select'唜','mas'
 insert into @t select'庅','me'
 insert into @t select'呅','mei'
 insert into @t select'椚','men'
 insert into @t select'掹','meng'
 insert into @t select'踎','meo'
 insert into @t select'瞇','mi'
 insert into @t select'宀','mian'
 insert into @t select'喵','miao'
 insert into @t select'乜','mie'
 insert into @t select'瓱','miliklanm'
 insert into @t select'民','min'
 insert into @t select'冧','lem'
 insert into @t select'名','ming'
 insert into @t select'谬','miu'
 insert into @t select'摸','mo'
 insert into @t select'乮','mol'
 insert into @t select'哞','mou'
 insert into @t select'母','mu'
 insert into @t select'旀','myeo'
 insert into @t select'丆','myeon'
 insert into @t select'椧','myeong'
 insert into @t select'拏','na'
 insert into @t select'腉','nai'
 insert into @t select'囡','nan'
 insert into @t select'囔','nang'
 insert into @t select'乪','keg'
 insert into @t select'孬','nao'
 insert into @t select'疒','ne'
 insert into @t select'娞','nei'
 insert into @t select'焾','nem'
 insert into @t select'嫩','nen'
 insert into @t select'莻','neus'
 insert into @t select'鈪','ngag'
 insert into @t select'銰','ngai'
 insert into @t select'啱','ngam'
 insert into @t select'妮','ni'
 insert into @t select'年','nian'
 insert into @t select'娘','niang'
 insert into @t select'茑','niao'
 insert into @t select'捏','nie'
 insert into @t select'脌','nin'
 insert into @t select'宁','ning'
 insert into @t select'牛','niu'
 insert into @t select'农','nong'
 insert into @t select'羺','nou'
 insert into @t select'奴','nu'
 insert into @t select'女','nv'
 insert into @t select'疟','nue'
 insert into @t select'瘧','nve'
 insert into @t select'奻','nuan'
 insert into @t select'黁','nun'
 insert into @t select'燶','nung'
 insert into @t select'挪','nuo'
 insert into @t select'筽','o'
 insert into @t select'夞','oes'
 insert into @t select'乯','ol'
 insert into @t select'鞰','on'
 insert into @t select'讴','ou'
 insert into @t select'妑','pa'
 insert into @t select'俳','pai'
 insert into @t select'磗','pak'
 insert into @t select'眅','pan'
 insert into @t select'乓','pang'
 insert into @t select'抛','pao'
 insert into @t select'呸','pei'
 insert into @t select'瓫','pen'
 insert into @t select'匉','peng'
 insert into @t select'浌','peol'
 insert into @t select'巼','phas'
 insert into @t select'闏','phdeng'
 insert into @t select'乶','phoi'
 insert into @t select'喸','phos'
 insert into @t select'丕','pi'
 insert into @t select'囨','pian'
 insert into @t select'缥','piao'
 insert into @t select'氕','pie'
 insert into @t select'丿','pianpang'
 insert into @t select'姘','pin'
 insert into @t select'乒','ping'
 insert into @t select'钋','po'
 insert into @t select'剖','pou'
 insert into @t select'哣','deo'
 insert into @t select'兺','ppun'
 insert into @t select'仆','pu'
 insert into @t select'七','qi'
 insert into @t select'掐','qia'
 insert into @t select'千','qian'
 insert into @t select'羌','qiang'
 insert into @t select'兛','qianke'
 insert into @t select'瓩','qianwa'
 insert into @t select'悄','qiao'
 insert into @t select'苆','qie'
 insert into @t select'亲','qin'
 insert into @t select'蠄','kem'
 insert into @t select'氢','qing'
 insert into @t select'銎','qiong'
 insert into @t select'丘','qiu'
 insert into @t select'曲','qu'
 insert into @t select'迲','keop'
 insert into @t select'峑','quan'
 insert into @t select'蒛','que'
 insert into @t select'夋','qun'
 insert into @t select'亽','ra'
 insert into @t select'囕','ram'
 insert into @t select'呥','ran'
 insert into @t select'穣','rang'
 insert into @t select'荛','rao'
 insert into @t select'惹','re'
 insert into @t select'人','ren'
 insert into @t select'扔','reng'
 insert into @t select'日','ri'
 insert into @t select'栄','rong'
 insert into @t select'禸','rou'
 insert into @t select'嶿','ru'
 insert into @t select'撋','ruan'
 insert into @t select'桵','rui'
 insert into @t select'闰','run'
 insert into @t select'叒','ruo'
 insert into @t select'仨','sa'
 insert into @t select'栍','saeng'
 insert into @t select'毢','sai'
 insert into @t select'虄','sal'
 insert into @t select'三','san'
 insert into @t select'桒','sang'
 insert into @t select'掻','sao'
 insert into @t select'色','se'
 insert into @t select'裇','sed'
 insert into @t select'聓','sei'
 insert into @t select'森','sen'
 insert into @t select'鬙','seng'
 insert into @t select'閪','seo'
 insert into @t select'縇','seon'
 insert into @t select'厦','sha'
 insert into @t select'筛','shai'
 insert into @t select'山','shan'
 insert into @t select'伤','shang'
 insert into @t select'弰','shao'
 insert into @t select'奢','she'
 insert into @t select'申','shen'
 insert into @t select'升','sheng'
 insert into @t select'尸','shi'
 insert into @t select'兙','shike'
 insert into @t select'瓧','shiwa'
 insert into @t select'収','shou'
 insert into @t select'书','shu'
 insert into @t select'刷','shua'
 insert into @t select'摔','shuai'
 insert into @t select'闩','shuan'
 insert into @t select'双','shuang'
 insert into @t select'谁','shei'
 insert into @t select'脽','shui'
 insert into @t select'吮','shun'
 insert into @t select'哾','shuo'
 insert into @t select'丝','si'
 insert into @t select'螦','so'
 insert into @t select'乺','sol'
 insert into @t select'忪','song'
 insert into @t select'凁','sou'
 insert into @t select'苏','su'
 insert into @t select'痠','suan'
 insert into @t select'夊','sui'
 insert into @t select'孙','sun'
 insert into @t select'娑','suo'
 insert into @t select'他','ta'
 insert into @t select'襨','tae'
 insert into @t select'囼','tai'
 insert into @t select'坍','tan'
 insert into @t select'铴','tang'
 insert into @t select'仐','tao'
 insert into @t select'畓','tap'
 insert into @t select'忒','te'
 insert into @t select'膯','teng'
 insert into @t select'唞','teo'
 insert into @t select'朰','teul'
 insert into @t select'剔','ti'
 insert into @t select'天','tian'
 insert into @t select'旫','tiao'
 insert into @t select'怗','tie'
 insert into @t select'厅','ting'
 insert into @t select'乭','tol'
 insert into @t select'囲','tong'
 insert into @t select'偷','tou'
 insert into @t select'凸','tu'
 insert into @t select'湍','tuan'
 insert into @t select'推','tui'
 insert into @t select'旽','tun'
 insert into @t select'乇','tuo'
 insert into @t select'屲','wa'
 insert into @t select'歪','wai'
 insert into @t select'乛','wan'
 insert into @t select'尣','wang'
 insert into @t select'危','wei'
 insert into @t select'塭','wen'
 insert into @t select'翁','weng'
 insert into @t select'挝','wo'
 insert into @t select'乌','wu'
 insert into @t select'夕','xi'
 insert into @t select'诶','ei'
 insert into @t select'疨','xia'
 insert into @t select'仙','xian'
 insert into @t select'乡','xiang'
 insert into @t select'灱','xiao'
 insert into @t select'楔','xie'
 insert into @t select'心','xin'
 insert into @t select'星','xing'
 insert into @t select'凶','xiong'
 insert into @t select'休','xiu'
 insert into @t select'旴','xu'
 insert into @t select'昍','xuan'
 insert into @t select'疶','xue'
 insert into @t select'坃','xun'
 insert into @t select'丫','ya'
 insert into @t select'咽','yan'
 insert into @t select'欕','eom'
 insert into @t select'央','yang'
 insert into @t select'吆','yao'
 insert into @t select'椰','ye'
 insert into @t select'膶','yen'
 insert into @t select'一','yi'
 --insert into @t select'乁','i'
 insert into @t select'乚','yin'
 insert into @t select'应','ying'
 insert into @t select'哟','yo'
 insert into @t select'佣','yong'
 insert into @t select'优','you'
 insert into @t select'迂','yu'
 insert into @t select'囦','yuan'
 insert into @t select'曰','yue'
 insert into @t select'蒀','yun'
 insert into @t select'帀','za'
 insert into @t select'災','zai'
 insert into @t select'兂','zan'
 insert into @t select'牂','zang'
 insert into @t select'遭','zao'
 insert into @t select'啫','ze'
 insert into @t select'贼','zei'
 insert into @t select'怎','zen'
 insert into @t select'曽','zeng'
 insert into @t select'吒','zha'
 insert into @t select'甴','gad'
 insert into @t select'夈','zhai'
 insert into @t select'毡','zhan'
 insert into @t select'张','zhang'
 insert into @t select'钊','zhao'
 insert into @t select'蜇','zhe'
 insert into @t select'贞','zhen'
 insert into @t select'凧','zheng'
 insert into @t select'之','zhi'
 insert into @t select'中','zhong'
 insert into @t select'州','zhou'
 insert into @t select'劯','zhu'
 insert into @t select'抓','zhua'
 insert into @t select'专','zhuan'
 insert into @t select'转','zhuai'
 insert into @t select'妆','zhuang'
 insert into @t select'骓','zhui'
 insert into @t select'宒','zhun'
 insert into @t select'卓','zhuo'
 insert into @t select'孜','zi'
 insert into @t select'唨','zo'
 insert into @t select'宗','zong'
 insert into @t select'棸','zou'
 insert into @t select'哫','zu'
 insert into @t select'劗','zuan'
 insert into @t select'厜','zui'
 insert into @t select'尊','zun'
 insert into @t select'昨','zuo'

 declare @strlen int
 select @strlen=len(@str),@re=''
 while @strlen>0
 begin    
      select top 1 @re=' '+
  (case --非汉字不转换,2007-6-15,:-)
   when unicode(substring(@str,@strlen,1)) between 19968 and 19968+20901 then
     substring(py,1,1)+substring(py,2,len(py))+' '+@re
   else
    substring(@str,@strlen,1)+@re
   end),@strlen=@strlen-1
      from @t a where chr<=substring(@str,@strlen,1)
      order by chr collate Chinese_PRC_CS_AS_KS_WS  desc
      if @@rowcount=0
        select @re=substring(@str,@strlen,1)+@re,@strlen=@strlen-1
   end
 --set @re = substring(@re,2,len(@re)-1)
 set @re = replace(@re,'  ',' ')
 return(@re)
end

------------------------------------------------------------------------------------

/*
返回字符出现次数

select dbo.StringMatchTimes('杭州大厦,杭州大厦,杭州大厦,杭州大厦,杭州大厦','杭州')
*/

 

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go


 ALTER FUNCTION [dbo].[StringMatchTimes]
(
 @Phrase VARCHAR(1000),
 @Word VARCHAR(15)
)
 RETURNS SMALLINT
 AS
BEGIN

/* 如果@Word 或者@Phrase 为空返回 0 */

IF @Word IS NULL OR @Phrase IS NULL RETURN 0

/* @BiggerWord 比@Word长一个字符 */

DECLARE @BiggerWord VARCHAR(21)

SELECT @BiggerWord = @Word + '^'

/* 在@Phrase用@BiggerWord替换@Word */

DECLARE @BiggerPhrase VARCHAR(2000)

SELECT @BiggerPhrase = REPLACE (@Phrase, @Word, @BiggerWord)
/* 相减结果就是出现的次数了 */

RETURN LEN(@BiggerPhrase) - LEN(@Phrase)

END

 

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值