Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 add a new column with its columnName and aliasName

Author  Topic 

pooya1072
Starting Member

13 Posts

Posted - 2013-05-15 : 07:47:40
hi
i want to create a new column (through a vb.net app) for a table . the problem is that i want set an alias name for this Column . my goal is to use column name for programming and use column alias as a caption for datagridview columns. is there a T-SQL command for this reason . (CREATE A COLUMN WITH NAME AND ALIAS)
thanks

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-05-15 : 08:18:28
SQL Server does not allow a built-in aliases for columns in base tables. Each column has a name and that is it. You can create a view that selects from the base table and alias the column in the view. Then just make sure you select from the view. Or, when you write the select statement, you can alias the column. The .Net datagrid also allows you to alias a column name.
Go to Top of Page

pooya1072
Starting Member

13 Posts

Posted - 2013-05-15 : 08:33:36
quote:
Originally posted by James K

SQL Server does not allow a built-in aliases for columns in base tables. Each column has a name and that is it. You can create a view that selects from the base table and alias the column in the view. Then just make sure you select from the view. Or, when you write the select statement, you can alias the column. The .Net datagrid also allows you to alias a column name.


ok... thank you very much
how if i want to set a description for my column with T-SQL ? is that has the same problem?

how can i create a column and define a description for my sql column for example through vb.net ?
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-05-15 : 10:16:39
Here is an example - My column name is col1, but I want to get the column name as FirstName when I do the select. The aliasing is shown in red
CREATE TABLE #tmp(col1 CHAR(32) );
INSERT INTO #tmp VALUES ('Mary');

SELECT col1 AS FirstName FROM #tmp;

DROP TABLE #tmp;
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-16 : 00:44:28
quote:
Originally posted by pooya1072

quote:
Originally posted by James K

SQL Server does not allow a built-in aliases for columns in base tables. Each column has a name and that is it. You can create a view that selects from the base table and alias the column in the view. Then just make sure you select from the view. Or, when you write the select statement, you can alias the column. The .Net datagrid also allows you to alias a column name.


ok... thank you very much
how if i want to set a description for my column with T-SQL ? is that has the same problem?

how can i create a column and define a description for my sql column for example through vb.net ?



you can create description for a column by adding it as a extended property. You can use sp_addextendedproperty to add it.

http://msdn.microsoft.com/en-IN/library/ms180047.aspx

In case you need to modify an existing description, you can use sp_updateextendedproperty

http://msdn.microsoft.com/en-IN/library/ms186885.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -