If you are like I used to be for YEARS, anytime you want to copy a table, index, etc… you probably right click on that object in SQL Server Management Studio and click “Script <Table|Index|View|etc…> as” > “CREATE To”:
This is a pretty easy way to quickly script database objects, however it’s incomplete.
For starters, I’m forced to generate the scripts for tables one at a time. Not fun.
Additionally, if I want to script associated objects for that table, like indexes, I have to go to each index and then right-click and select “Script Index as”. Ugh.
There Is A Better Way
Instead of using “Script Table as”, you can right click on your database and choose “Tasks” > “Generate Scripts…”:
This option brings up a GUI that will allow you to script multiple objects at the same time:
Additionally if you click the “Advanced” button in the final page of the dialog, you will receive many more options for how your objects will get scripted, including the ability to script out the indexes!
Using the Generate Scripts Task it’s easy to generate table, index, stored procedure, etc… change scripts all in one step – nice!
NOTE: If your SQL User receives an error when trying to generate the scripts, make sure they have the following access:
GRANT VIEW DEFINITION TO[MyUser]
GRANT SELECT ON sys.sql_expression_dependencies TO[MyUser]