13

I recently saw a tweet stating that you could prevent other developers from reading from a table using the SELECT * FROM TableName by building your table in the following way:

CREATE TABLE [TableName]
(
   [ID] INT IDENTITY NOT NULL,
   [Name] VARCHAR(50) NOT NULL,
   [DontUseStar] AS (1 / 0)
);

It's easy to see that using the SELECT * here would try to read the blank column name as 1 divided by 0 (thus causing a divide by zero error), but without a datatype assigned to the column.

Why does SQL allow you to create a column with no assigned data type, with a name it knows will be illegal?

4
  • 4
    This computed column has a data type and it is integer because underlying expression (integer divided by integer) is integer. May 30, 2019 at 16:26
  • The name isn't illegal; the value is. May 30, 2019 at 16:28
  • 1
    There's nothing "illegal" about the column. As detailed below, it's a perfectly acceptable computed column that will always throw an error at run time, which is when the computation takes place. Fascinating, though. May 30, 2019 at 16:38
  • RunTime is the key here, as @EricBrandt explained.
    – S3S
    May 30, 2019 at 16:47

3 Answers 3

10

It a perfectly valid syntax for a computed column. Computed columns can be used to calculate some value on select without actually storing them, although the value can be persisted.

The reason why 1/0 is allowed, is because the actual computed column value not evaluated until runtime. For example the computed column can be defined as columna/columnb, the same error will occur if any row in columnb has a 0 and is , but only if that row/column was selected.

if object_id('tempdb..#t') IS NOT NULL
    drop table #t;

CREATE TABLE #t 
(a int
,b int
,div as (a/b)
);

INSERT INTO #t
values 
(1,1)
,(2,0);

SELECT * FROM #t WHERE a = 1;
SELECT a from #t; -- NO ERRORS

SELECT * FROM #t WHERE a=2; --WILL RESULT IN AN ERROR
SELECT * FROM #t; --WILL RESULT IN AN ERROR

https://learn.microsoft.com/en-us/sql/relational-databases/tables/specify-computed-columns-in-a-table?view=sql-server-2017

7

What you have created is a computed column and it is quite powerful and useful!

The expression can be anything. For instance, you could define:

CREATE TABLE [TableName] (
   [ID] INT IDENTITY NOT NULL,
   [Name] VARCHAR(50) NOT NULL,
   NameLength as (LEN(Name))
);

This would create a column called NameLength and it would always have the length of Name when you refer to it -- no updates, no triggers, no views. It just works.

You don't need a type because SQL Server can figure that out.

The error that you have can even be beneficial -- if you really want to enforce that users never use select *.

-1

I think it's a greate way to create a table which allows no rows to be selected select * from . Why one may need such a table? Who knows, probably to wire a INSTEAD OF trigger to it or something. Any programming language will allow the code like x=1/0, it's not a compiler business to decide why one needs it.

4
  • allows no rows? Can you elaborate
    – S3S
    May 30, 2019 at 16:33
  • @scsimon correted , no rows can be selected
    – Serg
    May 30, 2019 at 16:35
  • 1
    But they can, as long as you list the columns explicitly... excluding the computed columns of course
    – S3S
    May 30, 2019 at 16:35
  • 1
    I'm not flagging it, but this is really a comment, not an answer. May 30, 2019 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.