SQL Server parallels to Oracle DBMS_METADATA.GET_DDL?

橙三吉。 提交于 2020-01-03 17:12:19

问题


I'm looking for command line or scripted solutions to pull the DDL out of SQL Server 2005+ for all database objects: tables, stored procs, views, indices/indexes, constraints, etc. GUI tools are not of interest.

Preference is for built-in tools, since that would be the most comparable to Oracle's DBMS_METADATA stuff. Also, preference for a solution that is as simple as Oracle's for getting the DDL out - eg, a one liner:


    SELECT DBMS_METADATA.GET_DDL('TABLE', 'MY_TABLE') FROM DUAL

Note: Getting things out for procedures in SQL Server 2005 seems easy, but I can't find any references to something similar for other objects (like tables).


    SELECT definition 
    FROM Sys.sql_modules 
    WHERE object_id = OBJECT_ID('MyProc')

Thanks in advance!


回答1:


There is no support in the Transact-SQL language. The client libraries (SMO) can do it using a Scripter object, see example at http://msdn.microsoft.com/en-us/library/ms162153.aspx. You can use SMO from PowerShell as a scripted solution.

The SQL Management Studio also has an option (right click on a database, go to Tasks, select Generate Scripts), it uses an SMO Scripter under the covers.




回答2:


I wrote SMOscript, a command-line tool which uses SMO to generate DDL files of all database objects.



来源:https://stackoverflow.com/questions/1511956/sql-server-parallels-to-oracle-dbms-metadata-get-ddl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!