locked
add a new column with its columnName and aliasName RRS feed

  • Question

  • 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
    Wednesday, May 15, 2013 11:50 AM

Answers

  • Hello,

    You can't create an alias in the table definition itself, only in the query (or in a view) for the table, like

    SELECT RealColumnName AS AliasColumnName
    FROM yorTable


    Olaf Helper

    Blog Xing

    Wednesday, May 15, 2013 11:56 AM
  • You don't create a column alias directly in T-SQL.  You specify column aliases in SELECT statements, including those encapsulated in a view or stored procedure:

    SELECT ColumnName AS ColumnAlias
    FROM dbo.MyTable;

    Also, .NET objects typically provide a way to name columns for display purposes other than that returned by a query.


    Dan Guzman, SQL Server MVP, http://www.dbdelta.com

    Wednesday, May 15, 2013 11:57 AM
  • Two options:

    1. You can very well set the Datagrid column name in the application code itself.

    2. You can alise the column while fetching the data.


    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    Wednesday, May 15, 2013 12:42 PM
  • how can i create a column and  define a description for my  sql column for example through vb.net ?

    With a T-SQL Statement, which creates first table + column, and then calling sp_addextendedproperty to add the comment

    DECLARE @v sql_variant 
    SET @v = N'Description for my Column'
    EXECUTE sp_addextendedproperty N'MS_Description', @v, N'SCHEMA', N'dbo', N'TABLE', N'MyTableName', N'COLUMN', N'MyColumName'
    


    Olaf Helper

    Blog Xing

    Wednesday, May 15, 2013 12:48 PM

All replies

  • Hello,

    You can't create an alias in the table definition itself, only in the query (or in a view) for the table, like

    SELECT RealColumnName AS AliasColumnName
    FROM yorTable


    Olaf Helper

    Blog Xing

    Wednesday, May 15, 2013 11:56 AM
  • You don't create a column alias directly in T-SQL.  You specify column aliases in SELECT statements, including those encapsulated in a view or stored procedure:

    SELECT ColumnName AS ColumnAlias
    FROM dbo.MyTable;

    Also, .NET objects typically provide a way to name columns for display purposes other than that returned by a query.


    Dan Guzman, SQL Server MVP, http://www.dbdelta.com

    Wednesday, May 15, 2013 11:57 AM
  • Hi Try as follows,

    -- CREATE A TEST TABLE
    CREATE TABLE TEST_TBL(COL1 INT, COL2 VARCHAR(20))
    --CREATE A VIEW WITH ALIAS NAME
    CREATE VIEW TEST_VIEW
    AS
    SELECT COL1,COL2 AS COLUMN2 FROM TEST_TBL
    -- USE VIEW WHERE EVER U WANT
    SELECT * FROM TEST_VIEW

    Thanks



    bala krishna

    Wednesday, May 15, 2013 12:07 PM
  • thanks ... 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 ?

    Wednesday, May 15, 2013 12:35 PM
  • Two options:

    1. You can very well set the Datagrid column name in the application code itself.

    2. You can alise the column while fetching the data.


    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    Wednesday, May 15, 2013 12:42 PM
  • how can i create a column and  define a description for my  sql column for example through vb.net ?

    With a T-SQL Statement, which creates first table + column, and then calling sp_addextendedproperty to add the comment

    DECLARE @v sql_variant 
    SET @v = N'Description for my Column'
    EXECUTE sp_addextendedproperty N'MS_Description', @v, N'SCHEMA', N'dbo', N'TABLE', N'MyTableName', N'COLUMN', N'MyColumName'
    


    Olaf Helper

    Blog Xing

    Wednesday, May 15, 2013 12:48 PM
  • thanks ... your helps is very useful .
    Wednesday, May 15, 2013 12:51 PM