微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SQLServer获得所有用户存储过程、根据存储过程名称获得内容、获得数据库对象创建脚本

select * from sysobjects where xtype='P' and category=2 --这个是系统存储过程

select * from sysobjects where xtype='P' and status>=0 --是用户

exec sp_helptext 'procname‘--获得存储过程创建脚本



获得数据库对象创建脚本

转自邹建

/*
在查询分析器中调用sqldmo生成脚本--函数

邹建 2003.07-----------------*/

/*--调用实例
print dbo.fgetscript('zj','','xzkh_sa','syscolumns')

/*--得到所有对象的脚本
declare @name varchar(250)
declare #aa cursor for
select name from sysobjects where xtype not in('S','PK','D','X','L')
open #aa
fetch next from #aa into @name
while @@fetch_status=0
begin
print dbo.fgetscript('zj',@name)
fetch next from #aa into @name
end
close #aa
deallocate #aa
--*/
*/
if exists(select 1 from sysobjects where id=object_id('fgetscript') and objectproperty(id,'IsInlineFunction')=0)
drop function fgetscript
go

create function fgetscript(
@servername varchar(50) --服务器名,@userid varchar(50)='sa' --用户名,如果为nt验证方式,则为空,@password varchar(50)='' --密码,@databasename varchar(50) --数据库名称,@objectname varchar(250) --对象名

) returns varchar(8000)
as
begin
declare @re varchar(8000) --返回脚本
declare @srvid int,@dbsid int --定义服务器、数据库集id
declare @dbid int,@tbid int --数据库、表id
declare @err int,@src varchar(255),@desc varchar(255) --错误处理变量

--创建sqldmo对象
exec @err=sp_oacreate 'sqldmo.sqlserver',@srvid output
if @err<>0 goto lberr

--连接服务器
if isnull(@userid,'')='' --如果是 Nt验证方式
begin
exec @err=sp_oasetproperty @srvid,'loginsecure',1
if @err<>0 goto lberr

exec @err=sp_oamethod @srvid,'connect',null,@servername
end
else
exec @err=sp_oamethod @srvid,@servername,@userid,@password

if @err<>0 goto lberr

--获取数据库集
exec @err=sp_oagetproperty @srvid,'databases',@dbsid output
if @err<>0 goto lberr

--获取要取得脚本的数据库id
exec @err=sp_oamethod @dbsid,'item',@dbid output,@databasename
if @err<>0 goto lberr

--获取要取得脚本的对象id
exec @err=sp_oamethod @dbid,'getobjectbyname',@tbid output,@objectname
if @err<>0 goto lberr

--取得脚本
exec @err=sp_oamethod @tbid,'script',@re output
if @err<>0 goto lberr

--print @re
return(@re)

lberr:
exec sp_oageterrorinfo NULL,@src out,@desc out 
declare @errb varbinary(4)
set @errb=cast(@err as varbinary(4))
exec master..xp_varbintohexstr @errb,@re out
set @re='错误号: '+@re
+char(13)+'错误源: '+@src
+char(13)+'错误描述: '+@desc
return(@re)
end
go


执行sql函数要使用select

如:select function

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐