34,837
社区成员




create table Authority (ID int identity(1,1), RoleName varchar(16), Protocol char(5), Link char(5))
insert into Authority values ('Admin','True','True')
insert into Authority values ('User','False','True')
go
create procedure proc_GetCategoryAuthority
@Category varchar(32) -- ='Protocol'
,@RoleName varchar(32) -- ='Admin'
as begin
declare @sql varchar(128)
set @sql = 'select CategoryRight='+@Category+' from Authority where RoleName = '''+@RoleName+''''
exec (@sql)
end
go
exec proc_GetCategoryAuthority 'Protocol','Admin'
go
drop table Authority
drop procedure proc_GetCategoryAuthority
go
不好意思。刚才没试。掉了个符号
alter proc GetCategoryAuthority
@Category varchar(50),
@RoleName varchar(50)
as
declare @sql nvarchar(4000)
declare @bool varchar(50)
set @sql='select @bl='+@Category +' from Authority where RoleName='''+@RoleName+''''
exec sp_executesql @sql,N'@bl varchar(50) output',@bool output
select @bool
alter proc GetCategoryAuthority (@Category varchar(100),@RoleName varchar(100))
as
begin
declare @sql varchar(1000)
select @sql=''
select @sql='select RoleName,'+@Category+' from Authority where charindex('',''+RoleName+'','','','+@RoleName+','')>0'
exec(@sql)
end
exec GetCategoryAuthority 'link,Protocol','admin,user'--可以填入多个板块与角色 中间用 ,号隔开
RoleName link Protocol
---------- ---------- ----------
admin true true
user true false
(2 行受影响)
create table Authority(id int identity(1,1),RoleName varchar(10),Protocol varchar(10),link varchar(10))
insert into Authority values('admin','true','true')
insert into Authority values('user','false','true')
create proc GetCategoryAuthority (@Category varchar(100),@RoleName varchar(100))
as
begin
declare @sql varchar(1000)
select @sql=''
select @sql='select '+@Category+' from Authority where charindex('',''+RoleName+'','','','+@RoleName+','')>0'
exec(@sql)
end
exec GetCategoryAuthority 'link,Protocol','admin,user'
link Protocol
---------- ----------
true true
true false
我的呢?
create proc GetCategoryAuthority
@Category varchar(50),
@RoleName varchar(50)
as
declare @sql nvarchar(8000)
declare @bool varchar(50)
set @sql='select @bl='+@Category +' from Authority where RoleName=''+@RoleName+'''
exec sp_executesql @sql,N'@bl varchar(50) output',@bool output
select @bool
create procedure proc_GetCategoryAuthority
@Category varchar(32) -- ='Protocol'
,@RoleName varchar(32) -- ='Admin'
as begin
declare @sql varchar(128)
set @sql = 'select CategoryRight='+@Category+' from Authority where RoleName = '''+@RoleName+''''
exec (@sql)
end
上面创建存储过程时敲错了。应该是proc。不好意思哈。
--调用存储过程:
exec GetCategoryAuthority @Category='Protocol',@RoleName='Admin'
create procedure proc_GetCategoryAuthority
@Category varchar(32) -- ='Protocol'
,@RoleName varchar(32) -- ='Admin'
as begin
declare @sql varchar(128)
set @sql = 'select '+@Category+' from Authority where RoleName = '''+@RoleName+''''
exec (@sql)
end
create ptoc GetCategoryAuthority
@Category varchar(50),
@RoleName varchar(50)
as
declare @sql nvarchar(8000)
declare @bool varchar(50)
set @sql='select @bl='+@Category +' from Authority where RoleName=''+@RoleName+'''
exec sp_executesql @sql,N'@bl varchar(50) output',@bool output
select @bool