Select * EXCEPT
Strike the chord with dynamic SQL for SELECT * EXCEPT
.
Seize the column list, jettison undesired columns, and fetch the selection.
Deep Dive into Dynamic SQL
Dynamic SQL is your rescue ranger when creating SQL queries programmatically becomes a need, making it possible for you to exclude columns dynamically from a SELECT statement. But beware, with great power, comes great responsibility.
The Magic of System Tables and Schemas
To exorcise specific columns, you can conjure up system tables or chess with the INFORMATION_SCHEMA. System tables like sysobjects
and syscolumns
are your secret envoys providing metadata:
Watch your step, adventurer!
Always tread lightly when using dynamic SQL! It can unleash dreadful security vulnerabilities such as SQL injection if you don’t weigh in precautions such as parameterization. Also, dynamic SQL has a bit of a dark side. It’s often elusive to maintain and debug than static SQL.
The Oath of Flexibility and Limitations
While dynamic SQL endows you with the tools for customizing queries, it also nudges you to remember that SQL isn't always as dynamic as you are. However, some databases like Google BigQuery, DuckDB, and others would pleasantly surprise you with their SELECT * EXCEPT
aptitude. So if you can spare the dynamism, embrace the built-in features first.
Code Transformations and Nifty Tricks
Crystalizing a function or stored procedure
Why not transform your dynamic SQL into a function or stored procedure? This would streamline the process of executing such queries for end users:
Debugging Scenarios
Debugging with dynamically excluded columns can be a catch, but you can create a view without binary large objects (BLOBs):
Applying Non-SQL Server Strategies
Adventurous RDBMS users have their tools:
- Oracle users can lean on polymorphic table functions.
- DB2 hides specific columns with
IMPLICITLY HIDDEN
.
Just remember, these tricks come with their own spellbooks (read - documentation).
Emphasizing Modern SQL Facets
Advanced RDBMS Features
New SQL systems such as Snowflake and Databricks SQL support the * EXCEPT
syntax, while DuckDB provides EXCLUDE
and REPLACE
options for flexible column selection.
Oracle and DB2 Magic
Use Oracle 18c's polymorphic table functions for dynamic column skipping. In DB2, set the HIDDEN attribute to a column to make it invisible to SELECT *
queries.
Command Line & Debugging Do's and Don'ts
Command Line Magic
Command line environment users, rejoice! You can use SQL command line tools without relying on GUIs.
Drawbacks and Precautions
Remember, SELECT *
can bring unexpected results and debug nightmares if used indiscriminately. So wield dynamic selection methods judiciously.
Was this article helpful?