In the query analyzer, get the script by Sqldmo

xiaoxiao2021-03-06  39

- Use the following scripts, you can get the script of any object, and it seems to be spread on the Internet, but it belongs to your own thing is still comfortable. - After adding the shortcut, then in the query analyzer according to the corresponding fast The key can display the script of the object .Set quoted_identifier on goset ANSI_NULLS ON Go

Alter procedure sp_script @Objectname varchar (50) asdeclare @DatabaseNamename varchar (50)

Set @DatabaseNamename = DB_NAME (DB_ID ())

Declare @str varchar (4000)

Declare @Object INTDECLARE @ObjectDatabase INTDECLARE @hr INTDECLARE @SRC VARCHAR (255), @Desc varchar (255) Declare @DatabaseCount Int, @current int, @Database INT

Declare @table int, @tables int

Exec @hr = sp_oacreate 'sqldmo.sqlser', @object outif @hr <> 0begin exec sp_oageterrorinfo @Object, @src out, @Desc out select hr = convert (varbinary (4), @ HR), Source = @ src, Description = @ Desc Returnend

Exec @hr = sp_oamethod @Object, 'connect', null, @@ servername, 'sa', ''

IF @hr <> 0begin exec sp_oageterrorinfo @Object Returnend

Exec @hr = sp_oagetproperty @Object, 'Databases', @objectDatabase Output

IF @hr <> 0begin exec sp_oageterrorinfo @ObjectDatabase, @src out, @Desc out select hr = convert (varbinary (4), @ HR), Source = @ src, description = @ Desc Returnend

EXEC @hr = sp_OAGetProperty @objectdatabase, 'count', @DataBaseCount outputIF @hr <> 0BEGIN EXEC sp_OAGetErrorInfo @objectdatabase, @src OUT, @desc OUT SELECT hr = convert (varbinary (4), @ hr), Source = @ src , Description = @ desc returnend

Set @current = 1LOOP1:

Exec @hr = sp_oamethod @ObjectDatabase, 'item', @Database output, @DatabaseNamenameif @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 returnid

Print @str

Goset quoted_identifier off goset ANSI_NULLS ON Go

转载请注明原文地址:https://www.9cbs.com/read-74858.html

New Post(0)