我有一个存储过程 存储过程中有insert into exec 这种语句,在数据库中执行的话,完全没有问题,但是用ado.net调用的话,总报列名无效。实在不明白是什么原因。
存储过程的作用是获取用户针对某表的记录权限 ado.net 中说此句代码 exec p_get_roles_with_user @userid 有错 说roleid无效
create proc [dbo].[p_get_rows_with_user_table]
@userid varchar(50),
@table varchar(50)
as 
begin 
 set nocount on;
 set xact_abort on;
 
 /*
 * exec p_get_rows_with_user_table '016327','common_menu' 
 */
 declare @tableid int;
 select @tableid=ct.id from content_table ct where ct.name=@table
create table #role
 (
 id int,
 name varchar(50),
 [version] binary(8)
 )
--角色
 insert into #role(id,name,[version])
 exec p_get_roles_with_user @userid
 
 
 --角色组
 select rrg.* into #rolegroup
 from rbca_rolegroup rrg
 where rrg.id in (select distinct rolegroupid from rbca_role_rolegroup where roleid in (select distinct id from #role))
--角色直接拥有的内容权限
 select contentid into #content
 from rbca_role_content where tableid=@tableid and roleid in (select id from #role)
 
 --角色组直接拥有的内容权限
 insert into #content (contentid)
 select contentid from rbca_rolegroup_content where tableid=@tableid and rolegroupid in (select id from #rolegroup)
--用户直接拥有的内容权限
 insert into #content (contentid)
 select contentid from rbca_user_content where tableid=@tableid and userid = @userid
--用户组直接拥有的内容权限
 insert into #content (contentid)
 select contentid from rbca_usergroup_content where tableid=@tableid and usergroupid in 
 (select usergroupid from rbca_user_usergroup where userid = @userid )
declare @filtersql nvarchar(max)
 set @filtersql = ''
 exec p_get_filtersql_with_user_table @userid,@table,@filtersql output;
 
 if rtrim(ltrim(@filtersql)) <> ''
 begin
 insert into #content exec sp_executesql @filtersql;
 end
 
 declare @sql nvarchar(max)
 set @sql = 'select * from ' + @table +' t where cast(t.id as varchar(50)) in (select contentid from #content)'
 exec sp_executesql @sql;
 
end
create proc [dbo].[p_get_roles_with_user]
@userid varchar(50)
as
begin
 /*
 * exec p_get_roles_with_user '016327'
 * 根据用户的角色,包括用户的角色、用户所在用户组的角色、前两者继承的角色得到用户最终拥有的角色
 */
 set nocount on;
 set xact_abort on;
 
 --获取用户的角色
 --用户的角色
 select r.* into #role
 from (
 select rur.roleid
 from rbca_user_role rur
 where rur.userid = @userid
 union 
 --用户所在用户组的角色
 select rugr.roleid
 from rbca_user_usergroup ruu
 join rbca_usergroup_role rugr
 on ruu.usergroupid = rugr.usergroupid
 where ruu.userid = @userid
 ) r
 
 --获取用户的角色继承的角色
 ;with ihrole as
 (
 select rri.inheritedroleid roleid
 from rbca_roleinherit rri
 join #role r
 on rri.roleid = r.roleid 
 union all
 select rri.inheritedroleid roleid
 from rbca_roleinherit rri
 join ihrole ihr
 on rri.roleid = ihr.roleid
 )
 select r.*
 from rbca_role r
 where r.id in (select distinct roleid
 from #role
 union
 select distinct roleid
 from ihrole)
end
使用
 监测下,将语句拿出来在 SQL分析器中执行下 ,试试
分析了,只看到这句exec p_get_rows_with_user_table '016327','common_menu',这正常啊,没有什么不正常的,不正常的是ado.net 指示此存储过程中insert into #role(id,name,[version])
 exec p_get_roles_with_user @userid 此句有错,列名roleid无效,但是这跟roleid没一点关系
  --角色
 --insert into #role(id,name,[version])
 --exec p_get_roles_with_user @userid
 
 select * from rbca_role;
 return ;
把那里改成这样就OK了,前辈们救救啊
  --角色
 --insert into #role(id,name,[version])
 --exec p_get_roles_with_user @userid
 
 select * from rbca_role;
 return ;
把那里改成这样就OK了,前辈们救救啊
所以还是感觉 ado.net是不是有bug啊
错误好像是说rbca_role_rolegroup这个表中没有roleid这个字段……
但是有啊,而且奇怪的是,在查询分析器偶尔也报roleid无效,但是只一次,然后随便执行都可以。然后没办法了,过了好久,偶尔又出现这个错误。用ado.net调用,每次都是这个错,是不是ado.net不支持存储过程中有insert into exec 这种语句呢
没有道理啊,好奇怪啊
但是rbca_role_rolegroup这个表有这个字段,如果没有的话,在查询分析器应该也报错啊,但是查询分析器真的随便执行都没有问题。有啊,而且奇怪的是,在查询分析器偶尔也报roleid无效,但是只一次,然后随便执行都可以。然后没办法了,过了好久,偶尔又出现这个错误。用ado.net调用,每次都是这个错,是不是ado.net不支持存储过程中有insert into exec 这种语句呢
没有道理啊,好奇怪啊
@曹赛军: 没这么用过这种语法……我以前碰到**无效的都是字段没创建!
insert操作完全可以的,前天刚写个例子还在用:
http://www.cnblogs.com/baiboy/archive/2013/02/26/2933432.html
角色中你改成:
insert into #role values(id,name,[version])
--exec p_get_roles_with_user @userid
 select * from rbca_role;
 return ;