In SQL Server, you can use the sys.columns
system catalog view to return a list of non-computed columns from a table.
By “non-computed”, I simply mean columns that are not computed columns.
Example
Here’s an example to demonstrate.
SELECT name AS [Column], TYPE_NAME(user_type_id) AS [Data Type], max_length, is_computed FROM sys.columns WHERE OBJECT_NAME(object_id) = 'Products' AND is_computed = 0;
Result:
+-------------+-------------+--------------+---------------+ | Column | Data Type | max_length | is_computed | |-------------+-------------+--------------+---------------| | ProductID | int | 4 | 0 | | ProductName | varchar | 255 | 0 | | Quantity | smallint | 2 | 0 | | Price | money | 8 | 0 | +-------------+-------------+--------------+---------------+
The sys.columns
view returns a lot of columns, so I’ve narrowed them down here to just a handful.
In this case, the table name is Products
. If I didn’t filter by that, I’d get a very large list of columns from all tables (including system tables), views, table-valued functions, etc.
I included the is_computed
column here just so that you could see that these columns have 0
in that column.
I happen to know that this table has a computed column called TotalValue
. Here’s the query again, but this time returning all columns (including computed columns).
SELECT name AS [Column], TYPE_NAME(user_type_id) AS [Data Type], max_length, is_computed FROM sys.columns WHERE OBJECT_NAME(object_id) = 'Products';
Result:
+-------------+-------------+--------------+---------------+ | Column | Data Type | max_length | is_computed | |-------------+-------------+--------------+---------------| | ProductID | int | 4 | 0 | | ProductName | varchar | 255 | 0 | | Quantity | smallint | 2 | 0 | | Price | money | 8 | 0 | | TotalValue | money | 8 | 1 | +-------------+-------------+--------------+---------------+