T-SQL object selection: CTAS vs stored procedure vs view vs function
Verdict: Use CTAS to create and load a new table in one statement. Use a stored procedure for parameterised, multi-step loads with inserts, updates and branching, invoked by name from a pipeline. A view stores a query; it holds no rows.
| Criterion | CTAS | Stored procedure | View | Scalar function |
|---|---|---|---|---|
| What it does | Creates and populates a new table in one statement | Encapsulates parameterised multi-step logic | Stores a reusable SELECT definition | Returns a single computed value |
| Persists data | Yes, physical table | Via its DML | No, virtual | No |
| DML and branching | One-time load only | Yes, insert, update, conditional | No | No |
| Invoked from a pipeline | As a statement | By name with EXEC | Referenced in queries | Used in expressions |
| Choose when | Build or stage a summarised table from existing data | Scheduled multi-step incremental load | Consistent query definition without storing rows | Compute one value in a query |
Rules
- Use CREATE TABLE AS SELECT (CTAS) to create and populate a new table from existing data in one statement.
- Use a stored procedure for encapsulated, parameterised, multi-step logic with inserts, updates and branching, invoked by name from a pipeline.
- Save as table in the SQL query editor persists a result set via CTAS; Save as view creates a stored query definition instead.
- Generate a DimDate range with a T-SQL script or CTAS, adding derived columns like fiscal year and day-of-week name.
Traps
- ALTER TABLE modifies an existing table; it does not create a new one populated with data.
- A view holds a query definition, not materialised rows; Save as view does not create a physical table.
- A scalar function returns one value and a view allows a single SELECT; neither supports multi-step DML.