SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 add a new column with its columnName and aliasName
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

pooya1072
Starting Member

13 Posts

Posted - 05/15/2013 :  07:47:40  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3706 Posts

Posted - 05/15/2013 :  08:18:28  Show Profile  Reply with Quote
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 - 05/15/2013 :  08:33:36  Show Profile  Reply with Quote
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 ?

Edited by - pooya1072 on 05/15/2013 08:38:42
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3706 Posts

Posted - 05/15/2013 :  10:16:39  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 05/16/2013 :  00:44:28  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000