首页 > 开发 > 综合 > 正文

在查询分析器中,通过SQLDMO来得到脚本

2024-07-21 02:06:21
字体:
来源:转载
供稿:网友

--用以下脚本,可以得到任意对象的创建时的脚本,网上好像也流传有,但属于自己的东西用着还是感觉舒坦.
--增加快捷键后,然后在查询分析器中按相应的快捷键就能显示对象的脚本.
set quoted_identifier on
go
set ansi_nulls on
go

 

 

alter    procedure sp_script
        @objectname varchar(50)
as
declare @databasename varchar(50)

set @databasename = db_name(db_id())

declare        @str varchar(4000)


declare @object int
declare @objectdatabase int
declare @hr int
declare @src varchar(255), @desc varchar(255)
declare @databasecount int, @current int, @database int

declare @table int, @tables int

exec @hr = sp_oacreate 'sqldmo.sqlserver', @object out
if @hr <> 0
begin
     exec sp_oageterrorinfo @object, @src out, @desc out
   select hr=convert(varbinary(4),@hr), [email protected], description=[email protected]
    return
end

exec @hr = sp_oamethod @object, 'connect', null, @@servername,
    'sa', ''

if @hr <> 0
begin
   exec sp_oageterrorinfo @object
    return
end

exec @hr = sp_oagetproperty @object, 'databases', @objectdatabase output

if @hr <> 0
begin
   exec sp_oageterrorinfo @objectdatabase, @src out, @desc out
   select hr=convert(varbinary(4),@hr), [email protected], [email protected]
    return
end

 

exec @hr = sp_oagetproperty @objectdatabase, 'count', @databasecount output
if @hr <> 0
begin
   exec sp_oageterrorinfo @objectdatabase, @src out, @desc out
   select hr=convert(varbinary(4),@hr), [email protected], [email protected]
    return
end


set @current = 1
loop1:

  exec @hr = sp_oamethod @objectdatabase, 'item', @database output, @databasename

  if @hr <> 0
  begin
    exec sp_oageterrorinfo @objectdatabase
     return
  end


exec @hr = sp_oamethod @database, 'getobjectbyname', @table output, @objectname

  if @hr <> 0
  begin
    exec sp_oageterrorinfo @database
     return
  end

exec @hr = sp_oamethod @table, 'script', @str output, 5
  if @hr <> 0
  begin
    exec sp_oageterrorinfo @table
     return
  end


print @str

 

 


go
set quoted_identifier off
go
set ansi_nulls on
go

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表