日期:2014-05-19  浏览次数:20447 次

怎样删除所有用户存储过程?
顺便求删除view、function、index等的方法。

------解决方案--------------------
复制一个列子:
USE pubs

--创建角色 r_test
EXEC sp_addrole 'r_test '

--授予 r_test 对 jobs 表的所有权限
GRANT ALL ON jobs TO r_test
--授予角色 r_test 对 titles 表的 SELECT 权限
GRANT SELECT ON titles TO r_test

--添加登录 l_test,设置密码为pwd,默认数据库为pubs
EXEC sp_addlogin 'l_test ', 'pwd ', 'pubs '

--为登录 l_test 在数据库 pubs 中添加安全账户 u_test
EXEC sp_grantdbaccess 'l_test ', 'u_test '

--添加 u_test 为角色 r_test 的成员
EXEC sp_addrolemember 'r_test ', 'u_test '

--拒绝安全账户 u_test 对 titles 表的 SELECT 权限
DENY SELECT ON titles TO u_test

/*--完成上述步骤后,用 l_test 登录,可以对jobs表进行所有操作,但无法对titles表查询,虽然角色 r_test 有titles表的select权限,但已经在安全账户中明确拒绝了对titles的select权限,所以l_test无titles表的select权限--*/

--从数据库 pubs 中删除安全账户
EXEC sp_revokedbaccess 'u_test '

--删除登录 l_test
EXEC sp_droplogin 'l_test '

--删除角色 r_test
EXEC sp_droprole 'r_test '

------解决方案--------------------
1、不用SQL語句的:直接在界面上完成
2、用SQL語句的:
可以用一個循環從sysobjects中查出相應的存儲過程、視圖等的名字後再drop掉即可
------解决方案--------------------
declare @str varchar(8000)
select @str = ' '
select @str=@str+ 'drop porc '+name+ ' ' from sysobjects where xtype = 'P '
print @str -- 显示
exec(@str) --删除
------解决方案--------------------

create proc usp_deny (@userid varchar(20))
as
declare @sql varchar(1000)
declare @name varchar(100)
declare @type varchar(10)
if @userid is null
return
declare cur_objects cursor for
select name,type from sysobjects where xtype in ( 'P ', 'V ', 'FN ', 'TF ')
open cur_objects
fetch next from cur_objects into @name,@type
while @@fetch_status=0
begin
if @type = 'V ' --视图
begin
select @sql = 'DROP VIEW '+@name
exec(@sql)
end
if @type = 'P ' --存储过程
begin
select @sql = 'DROP PROC '+@name
exec(@sql)
end
if @type in( 'FN ', 'TF ') --函数
begin
select @sql = 'DROP FUNCTION '+@name
exec(@sql)
end
fetch next from cur_objects into @name,@type
end
close cur_objects
deallocate cur_objects
------解决方案--------------------

=========================
declare @str varchar(8000)
select @str = ' '
select @str=@str+ 'drop porc '+name+ ' ' from sysobjects where xtype = 'P '
print @str -- 显示
exec(@str) --删除

------解决方案--------------------
declare @str_proc varchar(8000)
declare @str_view varchar(8000)
declare @str_function varchar(8000)
select @str_proc = ' '
select @str_view = ' '
select @str_function = ' '

select @str_proc=@str_proc+ 'drop porc '+name+ ' ' from sysobjects where xtype = 'P '
select @str_view=@str_view+ 'drop view '+name+ ' ' from sysobjects where xtype = 'V '
select @str_function=@str_function+ 'drop function '+name+ ' ' from sysobjects where xtype = 'FN '
print @str_proc -- 显示
print @str_view -- 显示
print @str_function -- 显示