Brett
Brett

Reputation: 43

SQL Server Hidden Column

I have a table that has three columns one of which is only used to get an increment number that is added to a string in another column. So I am trying to make it so that the column I don't want to see will never show up in an any matter. I know that I can just not select the column when doing a "Select" statement but I wanted to find out if there was another more permanent way to hide it.

Here is the output that I get:

INV   CUST_ID   FIRST_NAME
--------------------------
101   Cus101    Bob
102   Cus102    Jim

And here is how I want it to look:

CUST_ID FIRST_NAME
------------------
Cus101  Bob
Cus102  Jim

Here is the SQL statement:

CREATE TABLE tblcustomer 
(
    INV INT NOT NULL IDENTITY(101,1), 
    CUST_ID AS CONCAT('Cus', INV) PRIMARY KEY,
    FIRST_NAME VARCHAR(15) NOT NULL,
);

Also feel free to let me know if you see another way that I can achieve this without the "INV" column.

Upvotes: 0

Views: 2176

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270863

You can use a view:

create v_customer as
    select cust_id, first_name
     from tblcustomer;

Then use v_customer instead of tblcustomer.

I'm not 100% sure that I really encourage to do this. I think you are better off getting used to inv as the primary key. There is no need to convert it to an string. For instance, you should be using inv and not cust_id for foreign key relationships.

Upvotes: 3

Related Questions