[SQL]权限管理之存储过程

存储过程

CREATE PROCEDURE dbo.home_column
(@User_no nvarchar(20))
AS
declare @item1 nvarchar(4000)
declare @item2 nvarchar(4000)
declare @item nvarchar(4000)
set @item1='[new Ext.tree.TreePanel({id:''tree'
set @item=''',border:false,xtype:''treepanel'',split:true,'
set @item=@item+'width: 180,minSize: 175,maxSize: 500,'
set @item=@item+'collapsible: true,cmargins:''0 0 0 0'',rootVisible:false,'
set @item=@item+'autoScroll:true,animCollapse:false,useArrows: true,'
set @item=@item+'animate: false,collapseMode:''mini'',border :false,leaf:true,'
set @item=@item+'margins:''0 0 0 0'',loader :loader,'
set @item=@item+'root: new Ext.tree.AsyncTreeNode({text : '''
set @item2=''',draggable:false,expanded: true}),'
set @item2=@item2+'rootVisible: true,autoScroll : true})]'/**/
/**/
select 'panel'+convert(nvarchar,Menu_Tree.id) as id,Menu_Tree.text as title,'false' as border,'form' as layout, 'center' as algin,@item1+CONVERT(nvarchar,Menu_Tree.id)+''',region:''Div'+CONVERT(nvarchar,Menu_Tree.id)+@item+CONVERT(nvarchar,Menu_Tree.text)+''',id:'''+CONVERT(nvarchar,Menu_Tree.id)+@item2 as items from Menu_Tree
INNER JOIN
Per_tree ON Menu_Tree.id = Per_tree.id
WHERE (Per_tree.User_no = @User_no) AND (Menu_Tree.node = N'0')
order by Menu_Tree.orderby
--where node='0' order by orderby
RETURN

GO

CREATE PROCEDURE dbo.Menu_Edit
(
@idz nvarchar(10),
@textz nvarchar(50),
@nodez nvarchar(50),
@hrefz nvarchar(500),
@leafz nvarchar(10),
@otherz nvarchar(10),
@orderbyz nvarchar(10),
@clas nvarchar(10)
)

AS
declare @newid nvarchar(5)
set @newid=(SELECT TOP 1 id
FROM Menu_Tree
WHERE (SUBSTRING(id, 1, 1) <> 'P')
ORDER BY id DESC)
set @newid=convert(nvarchar,isnull(convert(int,@newid)+1,''))
if(@otherz='')
begin
set @otherz='Y'
end
IF(@leafz='')
begin
set @leafz='true'
end
if @clas='new' or @clas='upd'
begin
if @idz=''
begin
insert into Menu_Tree (id,node,root,text,open_href,leaf,other,orderby)
values(@newid,'0','Y',@textz,@hrefz,@leafz,@otherz,@orderbyz)
end
else
begin
update Menu_Tree set text=@textz,open_href=@hrefz,leaf=@leafz,other=@otherz,orderby=@orderbyz
where id=@idz
end
end

if @clas='del'
begin
delete from Menu_Tree where id=@idz
delete from Per_userwhere tree_no=@idz
end
RETURN

GO


--名称:分页存储过程
--使用示例 EXEC sp_PageIndex '*',' FROM StuSources ',2,10
--注意
--目前还没有对输入的参数进行严格的验证
--默认为输入都是合法有效的

CREATE PROC Paging
@sqlSelect varchar(8000) --SELECT 后面 FROM 前面 的 字段 不用包含SELECT
,@sqlFrom varchar(8000) --FROM 后面 的 字段 包含FROM
,@countPerPage int -- 每页数据行数
,@toPage int --要转到的页码

AS

BEGIN

-- 根据每页数据行数 和 要转到的页码 得到 数据起止点
Declare @start int
Declare @end int

set @end = @countPerPage * @toPage
set @start = @countPerPage * (@toPage - 1) + 1
create table #table(no nvarchar(10))--創建臨時表
insert #table exec(' select count(*) '+@sqlFrom)--將資料總條數插入臨時表
declare @no nvarchar(10)--定義變量
set @no=(select top 1 no from #table)--從臨時表中取值給@NO變量
--set @sqlFrom=@sqlFrom+' order by td_date desc'--查詢語句按日期排序

-- 临时表名称 可随机命名
Declare @tmpTable varchar(10)
SET @tmpTable ='#tmp'

Declare @sqlStr varchar(800)
-- 创建数据源到临时表
SELECT @sqlStr = 'SELECT Identity(int,1,1) AS RowIndex , '
SELECT @sqlStr = @sqlStr + rtrim(@sqlSelect) + ' INTO '+ @tmpTable
SELECT @sqlStr = @sqlStr + rtrim(@sqlFrom)
-- 查询临时表 得到所需要的数据
--SELECT @sqlStr = @sqlStr + ' '+'SELECT '+ @no +' as allda,'+ rtrim(@sqlSelect) +' FROM ' + @tmpTable
--SELECT @sqlStr = @sqlStr + ' WHERE RowIndex BETWEEN ' + Convert(char,@start) + ' AND ' + Convert(char,@end)
-- 删除临时表
SELECT @sqlStr = @sqlStr + ' '+'DROP TABLE '+@tmpTable
select @sqlStr
EXEC (@sqlStr)


END

GO


CREATE PROCEDURE dbo.per_tree
(
@node VARCHAR(50),--TREE'ID值以NODE傳回
@clas VARCHAR(20),---TREE類別
@per VARCHAR(10)----用戶權限
)

AS

IF @clas='homepage'--首頁用TREE
BEGIN
SELECT no,id, node,node as nodez, root, text, open_href, leaf, per,other,orderby FROM Per_tree WHERE (other = 'Y') andnode=@node and User_wkno=@per order by orderby
END
IF @clas='Menu_Edit'--首頁用TREE
BEGIN
SELECT no,id, node,node as nodez, root, text, open_href, leaf, per,other,orderby FROM Menu_Tree WHEREnode=@node order by orderby
END

IF @clas='per'
BEGIN
SELECT text,id,leaf,open_href from Per_tree where node=@node andUser_wkno=@per order by orderby
END

IF @clas='all'--更改權限用TREE
BEGIN
DECLARE @wkno nvarchar(10)
set @wkno=dbo.split(dbo.split(@node,'*',2),'-',1)
set @per=(select top 1 User_per from Base_User where User_wkno=@wkno)
set @node=dbo.split(@node,'*',1)
SELECT Per_user.per_no, Per_user.tree_no, Menu_Tree.id, Menu_Tree.node,Menu_Tree.text
into #user_tree_table
FROM Per_user INNER JOIN
Menu_Tree ON Per_user.tree_no = Menu_Tree.id
WHERE (Per_user.per_no = @per)
/*SELECT *,@node FROM #user_tree_table*/
SELECT convert(nvarchar,rtrim(Menu_Tree.id))+'*'+@wkno+'-' as id, Menu_Tree.node, Menu_Tree.text, Menu_Tree.leaf, Menu_Tree.open_href,
Menu_Tree.per, Menu_Tree.other,replace(#user_tree_table.per_no,@per,'true') AS checked
FROM Menu_Tree LEFT OUTER JOIN
#user_tree_table ON Menu_Tree.id = #user_tree_table.id whereMenu_Tree.node=@node and other='Y' order by orderby


END


IF @clas='group'
begin
set @per=dbo.split(dbo.split(@node,'*',2),'-',1)
set @node=dbo.split(@node,'*',1)
SELECT Per_user.per_no, Per_user.tree_no, Menu_Tree.id, Menu_Tree.node,Menu_Tree.text
INTO #group_tree_table
FROM dbo.Per_user INNER JOIN
dbo.Menu_Tree ON dbo.Per_user.tree_no = dbo.Menu_Tree.id
WHERE (dbo.Per_user.per_no = @per)
SELECT convert(nvarchar,rtrim(Menu_Tree.id))+'*'+@per+'-' as id, Menu_Tree.node, Menu_Tree.text, Menu_Tree.leaf, Menu_Tree.open_href,
Menu_Tree.per, Menu_Tree.other,replace(#group_tree_table.per_no,@per,'true') AS checked
FROM Menu_Tree LEFT OUTER JOIN
#group_tree_table ON Menu_Tree.id = #group_tree_table.id whereMenu_Tree.node=@node and other='Y' order by orderby
end

if(@clas='root')
begin
SELECT no,id, node,node as nodez, root, text, open_href, leaf, per,other,orderby FROM Menu_Tree WHEREnode=@node order by orderby
end

RETURN
GO


CREATE PROCEDURE dbo.select_data
(
@cb VARCHAR(20),--类别
@zh VARCHAR(20),--账号
@data VARCHAR(30),--
@qe VARCHAR(50),--查询条件
@today VARCHAR(10),--日期
@m_date VARCHAR(20),
@m_add VARCHAR(20),
@lg_gh VARCHAR(7),--登陆工号
@dep VARCHAR(20),
@sq_gh VARCHAR(7)
)
AS

DECLARE @n_time VARCHAR(4)
SET @today=ISNULL(@today,'')

IF @cb='user'--行政人員資料GRID查詢
BEGIN
IF @data<>''
BEGIN
SELECT * FROM Base_User where ( User_no like '%'+ @data+'%' or User_wkno like '%'+ @data+'%' or User_name like '%'+ @data+'%' or User_per like'%'+ @data+'%' or User_sect like '%'+ @data+'%' )
END
ELSE
BEGIN
SELECT * FROM Base_User
END
END
IF @cb='sect'
BEGIN
SELECT * FROM Class_Sect
END
IF @cb='per'
BEGIN
SELECT * FROM Class_Per
END
IF @cb='veri'
BEGIN
SELECT * FROM Class_Veri
END
IF @cb='name_veri'
BEGIN
declare @name_veri nvarchar(10)
set @name_veri=(select count(*) from Base_User where User_no=@qe)
if(@name_veri='1')
begin
select 'true' as ch
end
else
begin
select 'false' as ch
end
END
IF @cb='wknoname'
BEGIN
SELECT rtrim(ltrim(User_wkno)) +'-'+ rtrim(ltrim(User_name)) as noname From Base_User where User_veri='Y'
END
IF @cb='group'
BEGIN
SELECT rtrim(ltrim(per_no))+'-'+rtrim(ltrim(per_class)) as group_name FROM Class_Per
END

IF @cb='page_per'
BEGIN
declare @page nvarchar(2)
set @page=isnull((SELECT count(*) FROM Per_tree WHERE (User_wkno = ''+@lg_gh+'') AND (convert(nvarchar ,no) = ''+@qe+'')),'')
IF(@page<>'' and @page<>'0')
begin
select 'true' as ch
end
else
begin
select 'false' as ch
end
END

IF @cb='news'
BEGIN
SELECT TOP 5 * FROM News order by no desc
END
if @@error<>0
return 1
GO


CREATE PROCEDURE dbo.Update_Grid
(
@clas varchar(50),
@idz varchar(10),
@glw varchar(50),
@gdt varchar(50)

)
AS

declare @sql varchar(8000)
declare @today varchar(50)

set @today=CONVERT(VARCHAR(10),GETDATE(),120)--當天日期 格式:(2008-01-01)
declare @ntime varchar(50)
set @ntime=CONVERT(VARCHAR(4),REPLACE(CONVERT(VARCHAR,GETDATE(),108),':',''))--當前時間 格式(0901)
DECLARE @lw VARCHAR(2)

SET @lw=@glw
IF @clas='user'--人員資料修改GRID編輯更新
BEGIN
SET @sql='update Base_User set '+@glw+'='''+@gdt+''' whereid='''+@idz+''''
END

IF @clas='menu'
BEGIN
IF @gdt='0'
begin
DECLARE @pno nvarchar(10)
set @pno=dbo.Function1()
update Menu_Tree set node=@gdt,root='Y',id=@pno whereid=@idz
update Menu_Tree set node=@pno where node=@idz
end
else
begin
update Menu_Tree set node=@gdt ,root='0' where id=@idz
END
END

EXEC(@sql)
RETURN

GO

你可能感兴趣的:(存储过程)