问题
Can anyone please share the steps to hide the particular column from the table in SQL Server 2012 as I don't want to delete that column
By hide I mean that whenever I use the select query against that particular table it should never show me that column.
Is it possible? I need to make the column as hidden irrespective of any user login and whatever query i use
3rd party edit
Based on the comment But the problem is whenever i open the table in sql i dont want to see that particular column
i assume that the question is:
- How can i configure ssms so that opening a table definition inside sql management studio to only show the columns the connected user has select right to?
The screenshot below shows all columns of the table Employee despite the fact that the login StackoverIntern
has no select rights to the columns SSN, Salary
回答1:
Late post but I think its worth to share
Earlier to SQLSERVER-2016 there was no any option to hide few columns from table when selecting *
, however SQLSERVER-2016
come up with HIDDEN
keyword by which you can now set columns hidden from Select *
which you don't want to show and want only for some background process of your business logic.
The HIDDEN property is optional and will hide these columns from a standard SELECT statement for backward compatibility with our application and queries. You cannot apply the HIDDEN property to an existing column
.
you can alter existing table as well lets take an example of existing table
ALTER TABLE [dbo].[Account] ALTER COLUMN [StartDate] ADD HIDDEN;
ALTER TABLE [dbo].[Account] ALTER COLUMN [EndDate] ADD HIDDEN;
You can check this concept used more often in Temporal table
you can find more on this in below Temporal Table
回答2:
You can use column level permissions so that targeted users cannot select on that column. However, this will not "hide" the column in the case of doing a SELECT *
or SELECT SpecialColumn
. Instead, it will fail the query, resulting in an error.
An alternative to allow easier queries, you can make a View that does not include this column:
create view MyTableEx As
SELECT Every, Other, Column
FROM MyTable
Then only grant SELECT permissions to the View, rather than the table, for certain users. However, this is still problematic for an application, which now has to know whether it should select from the Table or the View.
When it comes down to it, column level permissions are kind of an unnatural thing to do in a database.
回答3:
If you do not want the column to show, then you should not include it in you select statement. It is also more efficient to not use an asterisk (*)
in your select statement.
See this post for more info in the performance issue: Performance issue in using SELECT *?
来源:https://stackoverflow.com/questions/37263621/hide-a-column-from-table-in-ms-sql-server