sqlserver 2000 自定义函数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sqlserver 2000 自定义函数相关的知识,希望对你有一定的参考价值。
CREATE function dbo.GetPersonTable(@personID int, @needSelf int)
returns table as
return(
if @needSelf = 0
SELECT a.* from dbo.v_Person as a
inner join dbo.m_SysUser_Popedom as b on a.i_deptid=b.deptid and b.userid = @personID
else
SELECT a.* from dbo.v_Person as a inner join dbo.m_SysUser_Popedom as b on a.i_deptid=b.deptid and b.userid = @personID
union
select c.* from dbo.v_Person as c where c.personid = @personid
)
报if附近有语法错误 附:dbo.v_Person是视图
CREATE function dbo.GetPersonTable(@personID int, @needSelf int)
returns @table table(fielda int,fieldb varchar(10))--定义返回表结构,要与你后面查询中字段一致
begin
if @needSelf = 0
insert into @table SELECT a.* from dbo.v_Person as a
inner join dbo.m_SysUser_Popedom as b on a.i_deptid=b.deptid and b.userid = @personID
else
insert into @table
SELECT a.* from dbo.v_Person as a inner join dbo.m_SysUser_Popedom as b on a.i_deptid=b.deptid and b.userid = @personID
union
select c.* from dbo.v_Person as c where c.personid = @personid
return
end
go
SqlServer 中查询子节对应的上级自定义函数
CREATE FUNCTION [dbo].[FN_TopGetOrgByUserName] ( @UserName NVARCHAR(128) ) RETURNS @showOrg TABLE(id NVARCHAR(36)) AS BEGIN DECLARE @baseOrg TABLE(id NVARCHAR(36), fullpath NVARCHAR(200), shortName NVARCHAR(200), parentid NVARCHAR(36), [level] INT,index_baseOrg INT) INSERT INTO @baseOrg SELECT id,fullpath,shortName,parentid,[level],ROW_NUMBER()OVER(ORDER BY id) FROM SysOrganization WHERE id IN ( SELECT OrgID FROM SysPosition ,SysUserPosition,SysUser WHERE SysPosition.ID=SysPosition AND SysUser.ID=SysUser_ID AND [email protected] AND basepositionid=‘‘ ) AND IsEnable=1 AND (IsDel=0 OR IsDel IS NULL) DECLARE @i INT DECLARE @j INT SET @i=1 SELECT @j=COUNT(*)FROM @baseOrg WHERE [level]=1 IF @j=1 BEGIN INSERT INTO @showOrg SELECT id FROM SysOrganization WHERE IsEnable=1 AND( IsDel=0 OR isdel IS NULL ) END ELSE BEGIN SELECT @j=COUNT(*)FROM @baseOrg WHILE @i<[email protected] BEGIN DECLARE @currentID NVARCHAR(36) SELECT @currentID=ID FROM @baseOrg WHERE [email protected] DECLARE @levelChild INT DECLARE @t_level TABLE(id VARCHAR(MAX) , [level] INT) SET @levelChild = 1 INSERT @t_level SELECT @currentID, @levelChild WHILE @@ROWCOUNT > 0 BEGIN SET @levelChild = @levelChild + 1 INSERT INTO @t_level SELECT a.ID , @levelChild FROM SysOrganization a , @t_Level b WHERE a.ParentID = b.id AND b.[level] = @levelChild - 1 END INSERT INTO @showOrg SELECT id FROM @t_level WHERE id NOT IN (SELECT id FROM @showOrg) SET @[email protected]+1 END END RETURN END GO
Split 表函数将一个字符串按指定分隔符进行分割,返回一个表。 charindex:在一段字符中搜索字符或者字符串
create function split( @string varchar(255),--待分割字符串 @separator varchar(255)--分割符 )returns @array table(item varchar(255)) as begin declare @begin int,@end int,@item varchar(255) set @begin = 1 set @end=charindex(@separator,@string,@begin) while(@end<>0) begin set @item = substring(@string,@begin,@[email protected]) insert into @array(item) values(@item) set @begin = @end+1 set @end=charindex(@separator,@string,@begin) end set @item = substring(@string,@begin,len(@string)[email protected]) if (len(@item)>0) insert into @array(item) values(substring(@string,@begin,len(@string)[email protected])) return end
以上是关于sqlserver 2000 自定义函数的主要内容,如果未能解决你的问题,请参考以下文章