I want to generate scripts for database objects, for example.
- tables
- view
- stored procedures
- Functions
FROM
not installed with a new installation:
- Windows XP
- Windows Vista li>
- Windows 7
and they are not distributed, they are not an option (it will work on the client’s machine).
( EDIT : It looks like SMO is actually distributable to date.)
Is there any source code that converts SELECTs from system tables to related scripts?
I will start with pseudocode that scripts stored procedures, views, triggers, or user-defined functions:
String GetStoredProcedureScript(String storedProcedureName) { return GetHelpText(storedProcedureName); } String GetViewScript(String viewName) { return GetHelpText(viewName); } String GetTriggerScript(String triggerName) { return GetHelpText(storedProcedureName); } String GetUserDefinedFunctionScript(String userDefinedFunctionName) { return GetHelpText(userDefinedFunctionName); }
All that can use only one helper function:
String GetHelpText(String objectName) { String szQuery = 'EXECUTE sp_helptext '+QuotedStr(objectName); String sql := ''; using (Recordset rs = connection.Execute(szQuery)) { while not rs.EOF do { sql = sql+rs['text']; rs.Next; } } return sql; }
Edit: Thanks to servicesharvest316 for pointing out sp_helptext . That is why I have a class that abstracts these things.
source share