A coworker showed me a cool “hidden” feature in SQL Query Analyzer, Templates.
Start Microsoft SQL Query Analyzer and connect to a database. Now open Object Browser (F8), and choose the bottom right tab Templates. You can choose from a extensive list of SQL templates that will automagically generate prescribed code for the task you wish to preform.
Example:
-- =============================================
-- Create table basic template
-- =============================================
IF EXISTS(SELECT name
FROM sysobjects
WHERE name = N'<table_name, sysname, test_table>'
AND type = 'U')
DROP TABLE <table_name, sysname, test_table>
GO
CREATE TABLE <table_name, sysname, test_table> (
<column_1, sysname, c1> <datatype_for_column_1, , int> NULL,
<column_2, sysname, c2> <datatype_for_column_2, , int> NOT NULL)
GO
Another neat feature, is to choose a user table from one of the database tables, and right click. Go to Script Object to New Window As ... This will generate code to Select, Insert, Update, Delete, etc.
Example:
SELECT [OrderID], [CustomerID], [EmployeeID], [OrderDate], [RequiredDate], [ShippedDate], [ShipVia],
[Freight], [ShipName], [ShipAddress], [ShipCity], [ShipRegion], [ShipPostalCode], [ShipCountry]
FROM [Northwind].[dbo].[Orders]