How to name a column the same name of a tablename

  • Hi newbie here, first let me say this I am not asking you to do my homework..So please don't make that statement. I have spent near 11 hours trying to figure this one thing out and can't. Basically I need to name make the name of a column the same as the name of a table from the result of a sql query.. here is the assignment question below..I can't figure out how to get the name of the table to be inputed as the column name..

    Write a script that uses dynamic SQL to return a single column that represents the number of rows in the first table in the current database. The script should automatically choose the table that appears first alphabetically, and it should exclude tables named dtproperties and sysdiagrams. [highlight=#ffff11]Name the column CountOfTable, where Table is the chosen table name.[/highlight]

    Hint: Use the sys.tables catalog view.

    I can figure out the rest. and actually have alredy done it, but i cannot figure out how to do the part that is highlighted above. I looked at lots of things on google to figure it out but no luck..Can some just give me some directlon or an example.. Thank you.

  • You can use system procedure:

    sp_rename

    to rename a column. The general format is like this:

    EXEC sp_rename 'table_name.existing_column_name', 'new_column_name', 'COLUMN'

    SQL DBA,SQL Server MVP(07, 08, 09) "Money can't buy you happiness." Maybe so, but it can make your unhappiness a LOT more comfortable!

  • Can you post what you have so far please? It's not actually hard to do the highlighted portion, but easier to add to your existing code than write from scratch.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • ScottPletcher (10/23/2014)


    You can use system procedure:

    sp_rename

    to rename a column.

    He's trying to alias a column in a query with a dynamic name, not rename a column in a table.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Sorry [that did seem rather obscure for a starting class on SQL].

    In the SELECT list, after the original column name or expression, you can add "AS new_column_name", and the query will list the column under the new name in the results.

    For example:

    SELECT object_id, name AS table_name

    FROM sys.tables

    WHERE name = 'table_name'

    SQL DBA,SQL Server MVP(07, 08, 09) "Money can't buy you happiness." Maybe so, but it can make your unhappiness a LOT more comfortable!

  • ScottPletcher (10/23/2014)


    Sorry [that did seem rather obscure for a starting class on SQL].

    In the SELECT list, after the original column name or expression, you can add "AS new_column_name", and the query will list the column under the new name in the results.

    For example:

    SELECT object_id, name AS table_name

    FROM sys.tables

    WHERE name = 'table_name'

    Sorry this doesn't work. Se the column name should be equal to the name of the talbe form the result.I already know that the table name is ContactUpdates cause I did a select query to see what was the first table listed in sys.tables. So when the query is run it should name the name column to ContactUpdates.

    so the table name resulting from the following query should become the column name.

    SELECT name

    FROM sys.tables

    WHERE name = (select Top 1 name from sys.tables)

    ;

    The resulting table name from this query is ContactUpdates.. now i need to have the script rename the column name to 'count of ContactUpdates' as instructed in the assignment..

  • dariuslearn (10/23/2014)


    ScottPletcher (10/23/2014)


    Sorry [that did seem rather obscure for a starting class on SQL].

    In the SELECT list, after the original column name or expression, you can add "AS new_column_name", and the query will list the column under the new name in the results.

    For example:

    SELECT object_id, name AS table_name

    FROM sys.tables

    WHERE name = 'table_name'

    Sorry this doesn't work. Se the column name should be equal to the name of the talbe form the result.I already know that the table name is ContactUpdates cause I did a select query to see what was the first table listed in sys.tables. So when the query is run it should name the name column to ContactUpdates.

    so the table name resulting from the following query should become the column name.

    SELECT name

    FROM sys.tables

    WHERE name = (select Top 1 name from sys.tables)

    ;

    The resulting table name from this query is ContactUpdates.. now i need to have the script rename the column name to 'count of ContactUpdates' as instructed in the assignment..

    Indeed you do need to rename your column that way; my code was not to write it for you, just to give you a working example. I will note that you'll need quotes around the name if it has spaces in it:

    SELECT object_id, name AS "table name"

    And you need to add "ORDER BY name" to your subquery above.

    SQL DBA,SQL Server MVP(07, 08, 09) "Money can't buy you happiness." Maybe so, but it can make your unhappiness a LOT more comfortable!

  • dariuslearn (10/23/2014)


    so the table name resulting from the following query should become the column name.

    SELECT name

    FROM sys.tables

    WHERE name = (select Top 1 name from sys.tables)

    ;

    You don't need to read the table twice. This will give almost the same result (mine will ensure that the order is correct)

    SELECT TOP 1 name

    FROM sys.tables

    ORDER BY name;

    As Gail said, please share what you have done to show you what you're missing (which is fairly simple).

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Just a thought, dynamic SQL, selecting the table names into a variable and loop through the resulting set of table names excecuting the count query for each.

    conceptually ...

    select the table names from the catelogue view into a variable, looping until there are no more table names.

    For each returned table name execute a dynamic SQL statement to get the required result set.

    Create a dynamic SQL statement using the varable name, along the lines of

    set @ColumnAlias = 'Countof' + @TableName

    set @SQL = 'Select count(*) as ' + @ColumnAlias + ' from ' + @tablename

    You could insert the results of the dynamic SQL into a table and then select the data from the table.

    I know it is not an elegant solution and I would most likely never do this in production but from answering the assignement it will get you there.

  • Luis Cazares (10/23/2014)


    dariuslearn (10/23/2014)


    so the table name resulting from the following query should become the column name.

    SELECT name

    FROM sys.tables

    WHERE name = (select Top 1 name from sys.tables)

    ;

    You don't need to read the table twice. This will give almost the same result (mine will ensure that the order is correct)

    SELECT TOP 1 name

    FROM sys.tables

    ORDER BY name;

    As Gail said, please share what you have done to show you what you're missing (which is fairly simple).

    USE AP

    Declare @MyRowCount int

    SELECT Top 1 name

    FROM sys.tables

    WHERE name not like 'dtproperties' and name not like 'sysdiagrams'

    order by name asc

    SET @MyRowCount = @@ROWCOUNT

    ;

    I also have to figure out how to get a count of the rows in that table and display it under the new column name

  • dogramone (10/23/2014)


    Just a thought, dynamic SQL, selecting the table names into a variable and loop through the resulting set of table names excecuting the count query for each.

    conceptually ...

    select the table names from the catelogue view into a variable, looping until there are no more table names.

    For each returned table name execute a dynamic SQL statement to get the required result set.

    Create a dynamic SQL statement using the varable name, along the lines of

    set @ColumnAlias = 'Countof' + @TableName

    set @SQL = 'Select count(*) as ' + @ColumnAlias + ' from ' + @tablename

    You could insert the results of the dynamic SQL into a table and then select the data from the table.

    I know it is not an elegant solution and I would most likely never do this in production but from answering the assignement it will get you there.

    Why do you want to loop? SQL Server can read and generate the statements in a single scan. In addition to that, the OP just needs one table.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • dogramone (10/23/2014)


    Just a thought, dynamic SQL, selecting the table names into a variable and loop through the resulting set of table names excecuting the count query for each.

    conceptually ...

    select the table names from the catelogue view into a variable, looping until there are no more table names.

    For each returned table name execute a dynamic SQL statement to get the required result set.

    Create a dynamic SQL statement using the varable name, along the lines of

    set @ColumnAlias = 'Countof' + @TableName

    set @SQL = 'Select count(*) as ' + @ColumnAlias + ' from ' + @tablename

    You could insert the results of the dynamic SQL into a table and then select the data from the table.

    I know it is not an elegant solution and I would most likely never do this in production but from answering the assignement it will get you there.

    Why would I need to loop through all tables? when I can just use top 1 to get the very first table..

  • Your variable @MyRowCount will always return 1 or 0 because you're limiting the results.

    You have a query that will return the name of the table. Using that value, build a string with the statement to count all rows in the table. Follow previous advises to understand how to alias a column. Use QUOTENAME() function to prevent problems with names. Once you're sure that you have the correct string, assign it to a variable. Using the variable, you can search about how to execute dynamic sql.

    EDIT: If you need additional help, feel free to continue asking. You can also find information on BooksOnLine(BOL) which is SQL Server help and you can access to it by pressing F1 on SSMS.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • sorry I am having trouble responding to post. This is the code I have so far.

    USE AP

    Declare @MyRowCount int

    SELECT Top 1 name

    FROM sys.tables

    WHERE name not like 'dtproperties' and name not like 'sysdiagrams'

    order by name asc

    SET @MyRowCount = @@ROWCOUNT

    ;

  • This is what i have so far.

    USE AP

    Declare @MyRowCount int

    SELECT Top 1 name

    FROM sys.tables

    WHERE name not like 'dtproperties' and name not like 'sysdiagrams'

    order by name asc

    SET @MyRowCount = @@ROWCOUNT

    ;

Viewing 15 posts - 1 through 15 (of 29 total)

You must be logged in to reply to this topic. Login to reply