得到對象指令碼

來源:互聯網
上載者:User

/*
 在查詢分析器中調用sqldmo產生指令碼--預存程序

鄒建 2003.07(引用請保留此資訊)--*/

/*--調用執行個體
 declare @str varchar(8000)
 exec sp_getscript 'zj','','','xzkh_sa','地區資料',@str output
 print @str
*/
if exists(select 1 from sysobjects where id=object_id('sp_getscript') and objectproperty(id,'IsProcedure')=1)
 drop procedure sp_getscript
go
create procedure sp_getscript
 @servername varchar(50)      --伺服器名
 ,@userid varchar(50)       --使用者名稱,如果為nt驗證方式,則為空白
 ,@password varchar(50)      --密碼
 ,@databasename varchar(50)    --資料庫名稱
 ,@objectname varchar(250)     --對象名
 ,@re varchar(8000) output     --返回指令碼
as
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,'connect',null,@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

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
 select 錯誤號碼=@re, 錯誤源=@src, 錯誤描述=@desc
 return

go

 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.