patternsqlMinor
Get object script in SQL Server via T-SQL?
Viewed 0 times
scriptsqlgetviaserverobject
Problem
I need to get the creation script of an object in a SQL Server database. I need the exact same script that was used to create it. Like when you use SSMS to alter a view you already defined, and then the script is there, just the way you've defined it earlier.
I need to do this in T-SQL. Where does SQL Server store the scripts related to the database objects? I can't find the related view in system catalog views.
What I'm trying to find is something like:
I need to do this in T-SQL. Where does SQL Server store the scripts related to the database objects? I can't find the related view in system catalog views.
What I'm trying to find is something like:
select creation_script -- I know this column doesn't exist
from sys.objects
where name = 'CustomersView'Solution
This is not feasible in T-SQL. Not all object have a definition stored and you would have to reverse engineer CREATE scripts for basic things like tables and indexes.
Rather than reinvent the wheel (in T-SQL), do the proper thing and reuse the component dedicated for this purpose the SMO
Rather than reinvent the wheel (in T-SQL), do the proper thing and reuse the component dedicated for this purpose the SMO
Scripter class. Is already available, works, is up to date with all the latest SQL Server objects, is free to redistribute along with SMO. Simply move your scripting logic out of T-SQL.Context
StackExchange Database Administrators Q#95262, answer score: 6
Revisions (0)
No revisions yet.