以下是小编为大家收集的用函数简化你的字符串连接语句数据库教程,本文共9篇,希望能够帮助到大家。

篇1:用函数简化你的字符串连接语句数据库教程
'/07/27 朱彦志(goodidea)myad@yeah.net
'你是否非常讨厌 用 & 一大串的连接变量?
'你是否不得不经常使用“”与“”“” ?
'就好像这样:
'strFilter = strFilter & “ and [” & strFldname & “] like ”“” & strValue & “”“”
'
'strSql =“insert into tblpubStyle. (Class,Prop,Value,Type,[Set],memo,flag) values ('” & _
' ctl.ControlType & “','” & _
' prp.Name & “','” & _
' prp.Value & “','” & _
' prp.Type & “', ” & _
' 2 & “,” & _
' “' ', ” & _
' false “)”
'
'strCnn = “Provider=sqloledb;” & _
' “Data Source=” & strServerName & “;” & _
' “Initial Catalog=” & strDbname & “;” & _
' “User Id=” & strUid & “;” & _
' “Password=” & strPwd & “;”
'
'用它,gCombinationString,这个十分简单的函数,让冗长的代码更简洁,增强语句可读性
'以下是它的使用典型形式:
'strFilter = gCombinationString( “$1 and [$2] like ”“$3”“”,strFilter,strFldname,strValue)
'strCnn= gCombinationString(“Provider=sqloledb;Data Source=$1;Initial Catalog=$2;User Id=$3;Password=$4;”,_
' strServerName ,strDbname ,strUid, strPwd)
'参数个数不限,参数类型不限
' strSql = gCombinationString(“insert into tblpubStyle. (Class,Prop,Value,Type,[Set],memo,flag) ” & _
' “ values ('$1','$2','$3','$4',$5,$6)”, _
' ctl.ControlType, prp.Name, prp.Value, prp.Type, 2, False)
'
Const strCharPre = “$”
Dim varItem As Variant
Dim i As Integer
i = 0
For Each varItem In Para
i = i + 1
strText = VBA.Replace(strText, strCharPre & i, varItem)
strText = VBA.Replace(strText, strCharPre & “”, strCharPre)
Next
gCombinationString = strText
End Function
篇2:用OMF来简化数据库管理数据库教程
Oracle9i带来了许多令人兴奋的新特性,操作系统级别上的Oracle的自动管理文件――即Oracle Managed Files(OMF)――就是其中之一,在Oracle9i出现之前,DBA必须在卸载表空间时手工移除数据文件(datafile)并在创建新的表空间之前仔细检查文件的位置。
为了理解OMF是如何工作的,让我们首先定义一个叫着DB_CTEATE_FILE_DEST的新参数。
alter system set db_create_file_dest='/u01/oradata/PROD';
一旦设置好了DB_CTEATE_FILE_DEST参数,我们就可以创建表空间而无须指定文件名。
createtablespace
omf1
datafile size 100m
locally managed;
文件创建之后,Oracle就可以分配和命名这些文件。Oracle OMF使用如下的命名转换惯例。默认的文件大小为100M,并按如下格式命名:
where
%u is a unique 8 digit code
%g is the log file group number
%t is the tablespace name
Controlfiles ora_%u.ctl
Redo Log Files ora_%g_%u.log
Datafiles ora_%t_%u.dbf
Temporary Datafilesora_%t_%u.tmp
Oracle9i OMF工具还有一个新的警告日志消息(alert log message),它用来告诉我们OMF数据文件何时发生了改动,
下面是关于警告日志的一个例子。
Fri Mar 23 01:44:46
CREATE TABLESPACE omf1 DATAFILE SIZE 10M AUTOEXTEND OFF
Fri Mar 23 01:44:47 2001
Created Oracle managed file /databases/ed25/OMF1/ora_omf1_xcp6rg84.dbf
Completed: CREATE TABLESPACE omf1 DATAFILE SIZE 10M AUTOEXTE
Fri Mar 23 01:45:45 2001
drop tablespace omf1
Fri Mar 23 01:45:45 2001
Deleted Oracle managed file /databases/ed25/OMF1/ora_omf_xcp6rg84.dbf
Completed: drop tablespace omf1
篇3:SQL 中自己创建函数,分割字符串数据库教程
创建|函数|字符串
----------------------------------------------------------------
/**
* 版权: 石太祥 [ E.Alpha ] 所有 ;
*
* email: ealpha(AT)msn(DOT)com ;
* msn: ealpha(AT)msn(DOT)com ;
* QQ : 9690501
*
* 所有请注明本信息!
*/
----------------------------------------------------------------
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[getEPnum]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[getEPnum]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[getstrcount]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[getstrcount]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[getstrofindex]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[getstrofindex]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
--- 这个函数直接调用了另外的两个函数,可以先阅读下面提到的两个函数
CREATE function getEPnum (@str varchar(8000))
returns varchar(8000)
as
begin
declare @str_return varchar(8000)
declare @i int
declare @temp_i int
declare @onlineornot int
declare @findepnumok int
-- 用来取得一个epnum,
-- 规则:首先从chatid中取,如果有在线得,则取得最前面得在线得返回
-- 如果全部不在线,则返回 ‘00000000’
select @findepnumok = 0
select @temp_i = 0
IF len(@str)<=0
begin
SELECT @str_return = '00000000'
end
else
begin
select @i = dbo.getstrcount(@str,',')
WHILE @temp_i< @i
BEGIN
select @onlineornot = online from wwchat_user where epnum=dbo.getstrofindex(@str,',',@temp_i)
IF (@onlineornot=1)
begin
select @str_return =dbo.getstrofindex(@str,',',@temp_i)
select @findepnumok = 1 --找到epnum后置为1
BREAK
end
ELSE
begin
select @temp_i = @temp_i + 1
select @findepnumok = 0 --找不到epnum后置为1
end
END
if @findepnumok = 0
begin
SELECT @str_return = '00000000'
end
end
return @str_return
end
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
-- getstrcount 输入一个没有分割的字符串,以及分割符
--返回数组的个数
CREATE function getstrcount (@str varchar(8000),@splitstr varchar(100))
--returns varchar(8000)
returns int
as
begin
declare @int_return int
declare @start int
declare @next int
declare @location int
select @next = 0
select @location = 1
if len(@str)
select @int_return =0
if charindex(@splitstr,@str) = 0
select @int_return =0
while (@location0)
begin
select @start = @location + 1
select @location = charindex(@splitstr,@str,@start)
select @next = @next + 1
select @int_return = @next
end
return @int_return
end
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
-- getstrofindex 输入一个未分割的字符串,舒服分割符号,舒服要取得的字符位置
-- 返回 制定位置的字符串
CREATE function getstrofindex (@str varchar(8000),@splitstr varchar(4),@index int=0)
returns varchar(8000)
as
begin
declare @str_return varchar(8000)
declare @start int
declare @next int
declare @location int
select @start =1
select @next = 1 --如果习惯从0开始则select @next =0
select @location = charindex(@splitstr,@str,@start)
while (@location 0 and @index > @next )
begin
select @start = @location +1
select @location = charindex(@splitstr,@str,@start)
select @next =@next +1
end
if @location =0 select @location =len(@str)+1 --如果是因为没有逗号退出,则认为逗号在字符串后
select @str_return = substring(@str,@start,@location -@start) --@start肯定是逗号之后的位置或者就是初始值1
if (@index @next ) select @str_return = '' --如果二者不相等,则是因为逗号太少,或者@index小于@next的初始值1,
SQL 中自己创建函数,分割字符串数据库教程
,
return @str_return
end
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
篇4:模拟字符串处理函数 stuff 处理 Ntext 字段数据库教程
函数|字符串
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_stuff]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)drop procedure [dbo].[p_stuff]GO
/*--Ntext字段处理
模拟字符串处理函数 stuff 完成表中 ntext 字段的 stuff 处理 注意,表中需要有列名为:id 的主键(或标识字段),数据类型为int 如果没有这个主键字段,或者是其他类型,则对应的需要修改存储过程
--邹建 .07--*/
/*--调用示例
--测试数据 create table tb(id int identity(1,1),content ntext) insert tb select 'a;sd' union all select 'a;sdfkjas2qasdfdfsg45yhjhdfg45645a' --调用存储过程,将第8~9的字符替换成'中国' exec p_stuff 'tb','content',8,2,'中国','' select * from tb drop table tb--*/
create proc p_stuff@tbname sysname, --要处理的表名@fdname sysname, --text/ntext字段名@start int=null, --开始位置,NULL表示追加数据@length int=null, --替换的长度@str nvarchar(4000),--要插入的字符串@where nvarchar(1000)=''--要处理的记录的条件asif @str is null returndeclare @s nvarchar(4000)set @s='declare @id int,@ptr varbinary(16),@start1 int
declare tb cursor local for select id,start=datalength(['+@fdname+'])/2from ['+@tbname+']'+case isnull(@where,'') when '' then '' else ' where '+@where end+'
open tb fetch tb into @id,@start1while @@fetch_status=0begin select @ptr=textptr(content) from ['+@tbname+'] where id=@id
if @start is null or @start1<@start updatetext ['+@tbname+'].['+@fdname+'] @ptr null null @str else begin set @start1=@start-1 updatetext ['+@tbname+'].['+@fdname+'] @ptr @start1 @length @str end fetch tb into @id,@start1endclose tbdeallocate tb'exec sp_executesql @s ,N'@start int,@length int,@str nvarchar(4000)' ,@start,@length,@strgo
篇5:用SQL进行函数查询数据库教程
Oracle 9i提供了很多函数可以用来辅助数据查询,
用SQL进行函数查询数据库教程
。接下来我们介绍常用的函数功能及使用方法。4.5.1 【ceil】函数
在【命令编辑区】输入“select mgr, mgr/100,ceil(mgr/100) from scott.emp;”,然后单击【执行】按钮,出现如图4.29所示的结果。
【参见光盘文件】:第4章4.5451.sql。
【ceil】函数用法:ceil(n),取大于扔谑值n的最小整数。
4.5.2 【floor】函数
在【命令编辑区】输入“select mgr, mgr/100,floor(mgr/100) from scott.emp;”,然后单击【执行】按钮,出现如图4.30所示的结果。
【参见光盘文件】:第4章4.5452.sql。
【floor】函数用法:floor(n),取小于等于数值n的最大整数。
4.5.3 【mod】函数
在【命令编辑区】输入“select mgr, mod(mgr,1000), mod(mgr,100), mod(mgr,10) from scott.emp;”,然后单击【执行】按钮,出现如图4.31所示的结果。
【参见光盘文件】:第4章4.5453.sql。
【mod】函数用法:mod(m,n),取m整除n后的余数。
4.5.4 【power】函数
在【命令编辑区】输入“select mgr, power(mgr,2),power(mgr,3) from scott.emp;”,然后单击【执行】按钮,出现如图4.32所示的结果。
【参见光盘文件】:第4章4.5454.sql。
【power】函数用法:power(m,n),取m的n次方。
4.5.5 【round】函数
在【命令编辑区】输入“select mgr, round(mgr/100,2),round(mgr/1000,2) from scott.emp;”,然后单击【执行】按钮,出现如图4.33所示的结果。
【参见光盘文件】:第4章4.5455.sql。
【round】函数用法:round(m,n),四舍五入,保留n位。
4.5.6 【sign】函数
在【命令编辑区】输入“select mgr, mgr-7800,sign(mgr-7800) from scott.emp;”,然后单击【执行】按钮,出现如图4.34所示的结果,
【参见光盘文件】:第4章4.5456.sql。
【sign】函数用法:sign(n)。n>0,取1;n=0,取0;n<0,取-1。
4.5.7 【avg】函数
在【命令编辑区】输入“select avg(mgr)平均薪水 from scott.emp;”,然后单击【执行】按钮,出现如图4.35所示的结果。
【参见光盘文件】:第4章4.5457.sql。
【avg】函数用法:avg(字段名),求平均值。要求字段为数值型。
4.5.8 【count】函数
(1)在【命令编辑区】输入“select count(*) 记录总数 from scott.emp;”,然后单击【执行】按钮,出现如图4.36所示的结果。
【参见光盘文件】:第4章4.5458-1.sql。
(2)在【命令编辑区】输入“select count(distinct job ) 工作类别总数 from scott.emp;”,然后单击【执行】按钮,出现如图4.37所示的结果。
【参见光盘文件】:第4章4.5458-2.sql。
【count】函数用法:count(字段名)或count(*),统计总数。
4.5.9 【min】函数
在【命令编辑区】输入“select min(sal) 最少薪水 from scott.emp;”,然后单击【执行】按钮,出现如图4.38所示的结果。
【参见光盘文件】:第4章4.5459.sql。
【min】函数用法:min(字段名),计算数值型字段最小数。
4.5.10 【max】函数
在【命令编辑区】输入“select max(sal) 最高薪水 from scott.emp;”,然后单击【执行】按钮,出现如图4.39所示的结果。
【参见光盘文件】:第4章4.54510.sql。
【max】函数用法:max(字段名),计算数值型字段最大数。
4.5.11 【sum】函数
在【命令编辑区】输入“select sum(sal) 薪水总和 from scott.emp;”,然后单击【执行】按钮,出现如图4.40所示的结果。
【参见光盘文件】:第4章4.54511.sql。
【sum】函数用法:sum(字段名),计算数值型字段总和。
通过上面4类查询实例的学习,读者可以举一反三,灵活运用。用SQL进行数据的查询就介绍到这里,下面学习如何录入数据。
篇6:用函数格式化EXPLAIN PLAN数据库教程
EXPLAIN PLAN命令的输出结果是解决SQL效率低下问题的最重要的诊断工具之一,此命令加载一个工作表格(默认名称为PLAN_TABLE),表格中带有Oracle优化器为SQL语句计算的执行计划步骤。例如:
EXPLAIN PLAN FOR
SELECT empno, ename
FROM emp
WHERE ename LIKE 'S%';
由于PLAN_TABLE中的行形成了一个等级,对其进行查询需要应用SELECT语句中的两个复杂子语句――START WITH与CONNECT BY。第一次在Oracle 9i中出现并在10g中得到扩充的DBMS_XPLAN使格式化及显示执行计划更为容易。
DBMS_XPLAN.DISPLAY是一个表格标值函数,它显示PLAN_TABLE的内容。与常规的纯量函数SUBSTR或总计函数SUM不同的是,表格函数返回一个完整的行集合(rowset)。它们用在SELECT语句的FROM子语句中,前面必须加上TABLE这个词,
如:
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY);
列表A显示的是上述EXPLAIN PLAN语句的结果,并由DBMS_XPLAN.DISPLAY格式化。
值得注意的是,DBMS_XPLAN还能从存储在系统全局区(System Global Area, SGA)内的指针显示“实时”执行计划。列表B显示的是由用户SCOTT执行的同一SQL语句。通过查看V$SESSION视图,你可以找到执行SCOTT任务的最后一个SQL ID。这反过来又可输入到DBMS_XPLAN.DISPLAY_CURSOR中,获得指针所使用的执行计划。
最后,DBMS_XPLAN.DISPLAY_AWR函数可用来查寻Oracle 10g的自动负荷信息库(Automatic Workload Repository, AWR)获得的历史SQL语句,并显示它的执行计划。它显示一个你可以访问的含七天历史记录的滚动窗口。
所有这三个函数都是协调SQL语句的强大工具。
Bob Watkins(OCP、MCDBA、MCSE、MCT)是一个有25年经验的计算机专业人士,从事过技术培训师、顾问与数据库管理员等职 。
篇7:右外连接数据库教程
select * from dt_function_node n,dt_function_point p where n.NODE_FUNCTION = p.oid (+) and n.node_code='123';
在这里面,可以选出n.NODE_FUNCTION=NULL的字段
如果没有(+),那么n.NODE_FUNCTION子段必须不为空,
右外连接数据库教程
,
篇8:分拆统计字符串数据库教程
统计|字符串
在数据库表tbl1中有一个字段Keywords,它是nvarchar类型,长度为1000,该字段的内容是所要分析的论文的关键字id keywords -----------------------------------------------------------1 kw1;kw2;kw32 kw2;kw33 kw3;kw1;kw4
问题1,对于在keywords字段中出现的所有关键字集合(上例中关键字集合为{kw1,kw2,kw3,kw4})中的任意一个关键字,要统计它出现的次数(也就是包含该关键字的纪录的条数),然后写到另一张表中。最后的效果就是keywords count-------------------------kw1 2kw2 2kw3 3kw4 1
问题2。在此基础上,要进行组合查询。也就是说在整个关键字集合中任意抽出两个关键字,统计它们在数据库表纪录中同时出现的次数。对于上题,最后效果要是:keywords count----------------------------------kw1;kw2 1kw1;kw3 2kw1;kw4 1kw2;kw3 2kw2;kw4 0kw3;kw4 1
--------------------------------------------------------------------------------------
--统计示例
--为统计处理专门做的序数表select top 1000 id=identity(int,1,1) into 序数表 from syscolumns a,syscolumns balter table 序数表 add constraint pk_id_序数表 primary key(id)go
--示例数据create table tbl1(id int,keywords nvarchar(1000))insert tbl1 select 1,'kw1;kw2;kw3'union all select 2,'kw2;kw3'union all select 3,'kw3;kw1;kw4'go
--第一种统计(计数)select keyword=substring(a.keywords,b.id,charindex(';',a.keywords+';',b.id)-b.id) ,[count]=count(distinct a.id)from tbl1 a,序数表 bwhere b.id<=len(a.keywords) and substring(';'+a.keywords,b.id,1)=';'group by substring(a.keywords,b.id,charindex(';',a.keywords+';',b.id)-b.id)go
--第二种统计(组合统计)select keyword=substring(a.keywords,b.id,charindex(';',a.keywords+';',b.id)-b.id) ,[count]=count(distinct a.id),a.idinto #tfrom tbl1 a,序数表 bwhere b.id<=len(a.keywords) and substring(';'+a.keywords,b.id,1)=';'group by substring(a.keywords,b.id,charindex(';',a.keywords+';',b.id)-b.id),a.id
select keyword=a.keyword+';'+b.keyword,[count]=sum(case a.id when b.id then 1 else 0 end)from #t a,#t bwhere a.keyword drop table #tgo --删除测试环境drop table tbl1,序数表 /*--测试结果 --统计1keyword count ---------- --------kw1 2kw2 2kw3 3kw4 1 (所影响的行数为 4 行) --统计2keyword count ----------------------- -----------kw1;kw2 1kw1;kw3 2kw1;kw4 1kw2;kw3 2kw2;kw4 0kw3;kw4 1 (所影响的行数为 6 行)--*/
篇9:SQL里类似SPLIT的分割字符串函数数据库教程
函数|字符串
T-SQL对字符串的处理能力比较弱,比如我要循环遍历象1,2,3,4,5这样的字符串,如果用数组的话,遍历很简单,但是T-SQL不支持数组,所以处理下来比较麻烦,下边的函数,实现了象数组一样去处理字符串。
一,用临时表作为数组
create function f_split(@c varchar(2000),@split varchar(2))
returns @t table(col varchar(20))
as
begin
while(charindex(@split,@c)0)
begin
insert @t(col) values (substring(@c,1,charindex(@split,@c)-1))
set @c = stuff(@c,1,charindex(@split,@c),'')
end
insert @t(col) values (@c)
return
end
go
select * from dbo.f_split('dfkd,dfdkdf,dfdkf,dffjk',',')
drop function f_split
col
--------------------
dfkd
dfdkdf
dfdkf
dffjk
(所影响的行数为 4 行)
二、按指定符号分割字符串,返回分割后的元素个数,方法很简单,就是看字符串中存在多少个分隔符号,然后再加一,就是要求的结果。
CREATE function Get_StrArrayLength
(
@str varchar(1024), --要分割的字符串
@split varchar(10) --分隔符号
)
returns int
as
begin
declare @location int
declare @start int
declare @length int
set @str=ltrim(rtrim(@str))
set @location=charindex(@split,@str)
set @length=1
while @location0
begin
set @start=@location+1
set @location=charindex(@split,@str,@start)
set @length=@length+1
end
return @length
end
调用示例:select dbo.Get_StrArrayLength('78,1,2,3',',')
返回值:4
三、按指定符号分割字符串,返回分割后指定索引的第几个元素,象数组一样方便
CREATE function Get_StrArrayStrOfIndex
(
@str varchar(1024), --要分割的字符串
@split varchar(10), --分隔符号
@index int --取第几个元素
)
returns varchar(1024)
as
begin
declare @location int
declare @start int
declare @next int
declare @seed int
set @str=ltrim(rtrim(@str))
set @start=1
set @next=1
set @seed=len(@split)
set @location=charindex(@split,@str)
while @location0 and @index>@next
begin
set @start=@location+@seed
set @location=charindex(@split,@str,@start)
set @next=@next+1
end
if @location =0 select @location =len(@str)+1
--这儿存在两种情况:1、字符串不存在分隔符号 2、字符串中存在分隔符号,跳出while循环后,@location为0,那默认为字符串后边有一个分隔符号,
return substring(@str,@start,@location-@start)
end
调用示例:select dbo.Get_StrArrayStrOfIndex('8,9,4',',',2)
返回值:9
三、结合上边两个函数,象数组一样遍历字符串中的元素
declare @str varchar(50)
set @str='1,2,3,4,5'
declare @next int
set @next=1
while @next<=dbo.Get_StrArrayLength(@str,',')
begin
print dbo.Get_StrArrayStrOfIndex(@str,',',@next)
set @next=@next+1
end
调用结果:
1
2
3
4
5
文档为doc格式