数据库函数问题

zunmaxiake1 2012-11-19 11:16:27
CREATE FUNCTION USP_GetOrgAccess
(
@p_userID int,
@p_elementCat varchar(50),
@p_elementCode int,
@p_MFcode varchar(50)
)
RETURNS
@tempOrgTree TABLE
(
[ItemID] [int] NOT NULL primary key,
[ParentID] [int] NULL,
[ItemName] [nvarchar](50) NULL,
[TypeID] [int] NULL,
[Enable] [bit] NOT NULL,
[ITEMORDER] [nvarchar](32) NULL
)
AS
begin
declare @v_strSql varchar(8000)
declare @v_allorg int
declare @v_localorg int
declare @v_suborg int

--v_tempLimit := 0;
set @v_strSql = ''
set @v_allorg = 0
set @v_localorg = 0
set @v_suborg = 0

--定义游标
--DECLARE CURSOR myCusor IS
--获取指用户定指定元素的常规组织权限范围

Select sum(DEPTLIMIT & 1),
sum(DEPTLIMIT & 2),
sum(DEPTLIMIT & 4)
into @v_allorg,@v_localorg,@v_suborg --这里语法错误
FROM TB_RoleLinkElement a
inner join v_tb_userrole b
on a.rolecode = b.ROLECODE
Where a.MFCode = @p_MFcode
and a.ELEMENTCAT = @p_elementCat
and a.ELEMENTCODE = @p_elementCode
and b.USERID = @p_userID;

if @v_allorg >= 1 then --这里附近有语法错误
select * from tb_orgtree;
else --这里附近有语法错误


--用户所在组织(当动作为“启动”时,部门范围为用户所在部门,其它动作需特别指定)
--if mod(v_tempLimit,4)>=2 then --这里附近有语法错误

if @v_localorg >= 2 or @p_elementCode = 1 then
@v_strSql = @v_strSql || '
insert into @tempOrgTree select OrgID from tb_orgldlnk where UserID = ' ||
@p_userID || '
union
select OrgID from tb_orgmemlnk where UserID = ' ||
@p_userID || '
';
end if;

--用户所在组织的下级组织
--if mod(v_tempLimit,8)>=4 then
if @v_suborg >= 4 then --这里附近有语法错误

if length(@v_strSql) <> 0 then --这里附近有语法错误

@v_strSql = @v_strSql || ' union all';
end if;
@v_strSql = @v_strSql || '
insert into @tempOrgTree select ItemID from tb_orgtree
start with parentid in
(
select OrgID from tb_orgldlnk where UserID = ' ||
@p_userID || '
union
select OrgID from tb_orgmemlnk where UserID = ' ||
@p_userID || '
)
connect by prior ItemID = ParentID
';
end if;

--自定义组织权限范围
if length(@v_strSql) <> 0 then
@v_strSql = @v_strSql || ' union ';
end if;
@v_strSql = @v_strSql || '
insert into @tempOrgTree select a.orgid
from tb_elementlinkorg a
inner join
(
select * from tb_rolelinkelement
where rolecode in
(
select rolecode from tb_rolelinkgroup where rolegroupcode in (select rolegroupcode from tb_rolegrouplinkuser where userid = ' ||
@p_userID || ')
union
select rolecode from tb_rolelinkuser where userid = ' ||
@p_userID || '
)
and MFCode = ''' || @p_MFcode || '''
and ELEMENTCAT = ''' || @p_elementCat || '''
and ELEMENTCODE = ''' || @p_elementCode || '''
) b
on a.rolelinkelementid = b.LinkID
where mod((a.Deptlimit+4-BITAND(a.Deptlimit,4)),4)>=2
union
select ItemID from tb_orgtree
start with parentid in
(
select a.orgid
from tb_elementlinkorg a
inner join
(
select * from tb_rolelinkelement
where rolecode in
(
select rolecode from tb_rolelinkgroup where rolegroupcode in (select rolegroupcode from tb_rolegrouplinkuser where userid = ' ||
@p_userID || ')
union
select rolecode from tb_rolelinkuser where userid = ' ||
@p_userID || '
)
and MFCode = ''' || @p_MFcode || '''
and ELEMENTCAT = ''' || @p_elementCat || '''
and ELEMENTCODE = ''' || @p_elementCode || '''
) b
on a.rolelinkelementid = b.LinkID
where mod((a.Deptlimit+4-BITAND(a.Deptlimit,8)),8)>=4
)
connect by prior ItemID = ParentID
';

@v_strSql = 'insert into @tempOrgTree select * from tb_orgtree where ItemID in (' || @v_strSql || ')';
exec @v_strSql;
end if; --这里附近有语法错误

end --uSP_GetOrgAccess;

这个函数问题很多,不知道怎么改,望指点!!!
...全文
94 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunny6281 2012-11-19
  • 打赏
  • 举报
回复
CREATE FUNCTION USP_GetOrgAccess ( @p_userID int, @p_elementCat varchar(50), @p_elementCode int, @p_MFcode varchar(50) ) RETURNS @tempOrgTree TABLE ( [ItemID] [int] NOT NULL primary key, [ParentID] [int] NULL, [ItemName] [nvarchar](50) NULL, [TypeID] [int] NULL, [Enable] [bit] NOT NULL, [ITEMORDER] [nvarchar](32) NULL ) AS begin --declare @v_strSql varchar(8000) -- 拼sql要用nvarchar,最好设为max declare @v_strSql nvarchar(max) declare @v_allorg int declare @v_localorg int declare @v_suborg int --v_tempLimit := 0; --set @v_strSql = '' set @v_strSql = N'' set @v_allorg = 0 set @v_localorg = 0 set @v_suborg = 0 --定义游标 --DECLARE CURSOR myCusor IS --获取指用户定指定元素的常规组织权限范围 Select @v_allorg = sum(DEPTLIMIT & 1), @v_localorg = sum(DEPTLIMIT & 2), @v_suborg = sum(DEPTLIMIT & 4) --into @v_allorg,@v_localorg,@v_suborg --这里语法错误 FROM TB_RoleLinkElement a inner join v_tb_userrole b on a.rolecode = b.ROLECODE Where a.MFCode = @p_MFcode and a.ELEMENTCAT = @p_elementCat and a.ELEMENTCODE = @p_elementCode and b.USERID = @p_userID; --if @v_allorg >= 1 then --这里附近有语法错误 if @v_allorg >= 1 begin select * from tb_orgtree; end else --这里附近有语法错误 -- 这里不用管 前面改了 这里自然就好了 --用户所在组织(当动作为“启动”时,部门范围为用户所在部门,其它动作需特别指定) --if mod(v_tempLimit,4)>=2 then --这里附近有语法错误 --if @v_localorg >= 2 or @p_elementCode = 1 then if @v_localorg >= 2 or @p_elementCode = 1 begin -- 这里你到底想插到那个表啊?表名呢? --@v_strSql = @v_strSql || ' --insert into @tempOrgTree --select OrgID from tb_orgldlnk where UserID = ' || -- @p_userID || ' --union --select OrgID from tb_orgmemlnk where UserID = ' || -- @p_userID || ' '; --end if; end --用户所在组织的下级组织 --if mod(v_tempLimit,8)>=4 then --if @v_suborg >= 4 then --这里附近有语法错误 if @v_suborg >= 4 begin --if length(@v_strSql) <> 0 then --这里附近有语法错误 if length(@v_strSql) <> 0 begin --@v_strSql = @v_strSql || ' union all'; @v_strSql = @v_strSql + N' union all'; --end if; -- 后面的懒得改了,你确定你学过sql吗? @v_strSql = @v_strSql || ' insert into @tempOrgTree select ItemID from tb_orgtree start with parentid in ( select OrgID from tb_orgldlnk where UserID = ' || @p_userID || ' union select OrgID from tb_orgmemlnk where UserID = ' || @p_userID || ' ) connect by prior ItemID = ParentID '; end if; --自定义组织权限范围 if length(@v_strSql) <> 0 then @v_strSql = @v_strSql || ' union '; end if; @v_strSql = @v_strSql || ' insert into @tempOrgTree select a.orgid from tb_elementlinkorg a inner join ( select * from tb_rolelinkelement where rolecode in ( select rolecode from tb_rolelinkgroup where rolegroupcode in (select rolegroupcode from tb_rolegrouplinkuser where userid = ' || @p_userID || ') union select rolecode from tb_rolelinkuser where userid = ' || @p_userID || ' ) and MFCode = ''' || @p_MFcode || ''' and ELEMENTCAT = ''' || @p_elementCat || ''' and ELEMENTCODE = ''' || @p_elementCode || ''' ) b on a.rolelinkelementid = b.LinkID where mod((a.Deptlimit+4-BITAND(a.Deptlimit,4)),4)>=2 union select ItemID from tb_orgtree start with parentid in ( select a.orgid from tb_elementlinkorg a inner join ( select * from tb_rolelinkelement where rolecode in ( select rolecode from tb_rolelinkgroup where rolegroupcode in (select rolegroupcode from tb_rolegrouplinkuser where userid = ' || @p_userID || ') union select rolecode from tb_rolelinkuser where userid = ' || @p_userID || ' ) and MFCode = ''' || @p_MFcode || ''' and ELEMENTCAT = ''' || @p_elementCat || ''' and ELEMENTCODE = ''' || @p_elementCode || ''' ) b on a.rolelinkelementid = b.LinkID where mod((a.Deptlimit+4-BITAND(a.Deptlimit,8)),8)>=4 ) connect by prior ItemID = ParentID '; @v_strSql = 'insert into @tempOrgTree select * from tb_orgtree where ItemID in (' || @v_strSql || ')'; exec @v_strSql; end if; --这里附近有语法错误 end --uSP_GetOrgAccess;
快溜 2012-11-19
  • 打赏
  • 举报
回复
Select @v_allorg=sum(DEPTLIMIT & 1), @v_localorg=sum(DEPTLIMIT & 2), @v_suborg=sum(DEPTLIMIT & 4) FROM TB_RoleLinkElement a ................. 去掉then用begin end
zunmaxiake1 2012-11-19
  • 打赏
  • 举报
回复
我看了,好像没有问题啊!!!
bdmh 2012-11-19
  • 打赏
  • 举报
回复
都是基本的语法问题,你去对着sqlserver的帮助看
zunmaxiake1 2012-11-19
  • 打赏
  • 举报
回复
Start with connect by prior 递归
这个是oracle的递归sql语法,怎样换成sqlserver的递归
zunmaxiake1 2012-11-19
  • 打赏
  • 举报
回复
开始理解错了,这是个表值函数,要把最后处理的数据RETURNS @tempOrgTree TABLE 返回就可以了,这些数据不用给临时变量@v_strSql,直接insert就可以了。但是现在start with 还有connect附近报语法错误。
H_Gragon 2012-11-19
  • 打赏
  • 举报
回复
你可以挑些重点说一下,别一下一大片代码~!

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧