In Query Analyzer, get the script by SQLDMO

Source: Internet
Author: User
Tags object key query return
Query Analysis | scripts
--With the following script, you can get the script of the creation of any object, the Internet seems to be circulated, but the things that belong to their own use or feel comfortable.
--When you add a shortcut key, you can then display the object's script by pressing the shortcut key in the Query Analyzer.
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. SQL Server ', @object out
IF @hr <> 0
BEGIN
EXEC sp_OAGetErrorInfo @object, @src out, @desc out
SELECT Hr=convert (varbinary (4), @hr), source= @src, description= @desc
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), source= @src, description= @desc
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), source= @src, description= @desc
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




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.