/ * Call SQLDMO to generate scripts in the query analyzer - stored procedure
Zou Jian 2003.07 (Please keep this information) - * /
/ * - Call instance declare @str varchar (8000) Exec sp_getscript 'zj', '', '', 'xzkh_sa', 'Area Information', @ Str Output Print @ Str * / if EXISTS (SELECT 1 from sysobjects where id = object_id ( 'sp_getscript') and objectproperty (id, 'IsProcedure') = 1) drop procedure sp_getscriptgocreate procedure sp_getscript @servername varchar (50) - the server name, @ userid varchar (50) - user name, if nt If the verification method is empty, @ password varchar (50) - password, @ DatabaseName Varchar (50) - Database Name, @ Objectname Varchar (250) - Object Name, @ RE VARCHAR (8000) Output - Return the script Asdeclare @srvid int, @ dbsid int - Define server, database set IDDeclare @dbid int, @ Tbid int - database, table IDDeclare @err int, @ src varchar (255), @Desc varchar (255) - error handling variable
- Create SQLDMO objects EXEC @ Err = sp_oacreate 'sqldmo.sqlserver', @ srvid outputif @err <> 0 goto lberr
- Connecting server if isnull (@Userid, '') = '' - if it is NT verification mode begin exec @ err = sp_oasetproperty @ srvid, 'loginsecure', - 1 if @err <> 0 goto lberr
Exec @ Err = SP_OAMETHOD @ Srvid, 'Connect', Null, @ ServerNameEndelse Exec @ Err = SP_OAMETHOD @ SRVID, 'Connect', Null, @ ServerName, @ UserId, @ Password
IF @err <> 0 goto lberr
- Get Database Sets Exec @ Err = SP_OAGETPROPERTY @ SRVID, 'DatabaseS', @ dbsid Outputif @err <> 0 goto lberr
- Get the database IDEC @ err = sp_oamethod @ err = sp_oamethod @ err = sp_oamethod @ DBSID, 'Item', @ DBID OUTPUT, @ DatabaseNameif @err <> 0 goto lberr
- Get to get the script objects idexec @ err = sp_oamethod @ dbid, 'getobjectbyname', @ tbid output, @ objectnameif @err <> 0 goto lberr-- to get the script exec @ err = sp_oamethod @ tbid, 'script', @ Re Outputif @err <> 0 goto lberr
--Print @rereturn
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 error number = @ RE, error source = @ src, error description = @ Desc Return
Go