4

How to change * [asterisk symbol] into list of column names? I can view the list of column names after placing mouse cursor over the *. Is it possible to click-crack on something to change the * into names without running the script an inserting the results into some dbo.temp table?

enter image description here

So the desired results would be:

with A as (select 
 MyColumn1=1 
,MyColumn2=2 
,MyColumn3=3)

select 
 MyColumn1
,MyColumn2
,MyColumn3
from A
Przemyslaw Remin
  • 6,276
  • 25
  • 113
  • 191

5 Answers5

3

In SQL Server Management Studio, you can do what you want.

On the left side of the screen, expand the database to get the table name. Then, drag the table name over to the query pane and it will list all the columns.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
2

There is an option into Sql Server Management in which you can execute a stored procedure with the keyboard, you can configure that option to execute a procedure that lists the columns of a table, this is the way you can do it:

enter image description here

Click over "Options"

enter image description here

As you can see there are many keyboard shortcuts to execute a stored procedure, eg when you highlight a name of a table with the shortcut alt+f1 you can see the metadata of the table, I wrote a stored procedure that shows the lists of the columns of a table separated with ",", this is the procedure:

    Create Procedure [dbo].[NS_rs_columnas]
        @Tabla Sysname,
        @Alias Char(3)=null
    AS
    Begin
        Declare @Colums Nvarchar(Max)='';

        Select 
        @Colums+=','+isnull(Ltrim(Rtrim(@Alias))+'.','')+'['+b.name+']' + CHAR(13)+CHAR(10)
        from sys.tables a
        Inner join sys.all_columns b 
        on (a.object_id=b.object_id)
        Where a.name=ltrim(rtrim(@Tabla));

        Select ' '+Substring(@Colums,2,len(@Colums)-2);
    End

So what you can do is configure a shortcut to execute that procedure.

this is the result when I press the shortcut ctrl+f1 over a table name:

enter image description here

As you can see the procedure has two parameters, the second parameter is to send an alias, this is an example:

enter image description here

MelgoV
  • 661
  • 8
  • 21
  • 1
    This is very useful answer. Few remarks. Changed two lines: `@Colums+=','+isnull(Ltrim(Rtrim(@Alias))+'.','')+'['+b.name+']' + CHAR(13)+CHAR(10)` and the last one `Select ' '+Substring(@Colums,2,len(@Colums)-2);` In this way we get results starting with comma, column names wrapped in brackets and every column name in a new line. The variable @campo in your code is not used anywhere, I got rid of it. Short cut works after closing and opening again SSMS. You have to get in and get out of the car to have it working (a rule of thumb for MS products). – Przemyslaw Remin Jan 05 '16 at 09:41
  • This is a great suggestion, also I think the stored procedure can be upgraded to select the list of column names of the temp tables, I will change that lines, if my answer solved your needing please mark it as right answer. – MelgoV Jan 05 '16 at 15:13
  • I have voted it up. The comments of Devart, Martin Smith, and onupdatecascade probably are better solution to my question - because they provide results without running the code. Though there is a drawback of installing additional soft which is not always possible. Thank you, MelgoV. – Przemyslaw Remin Jan 07 '16 at 10:53
1

Converting * in a cte to column names in Management studio

  • create a table using ... select * into MyTable from A
  • you can drag Object Explorer / Databases / MyDb / Tables / MyTable / Columns into your query
  • or you can use Script table as Select if you prefer column names with []
  • delete MyTable
Istvan Heckl
  • 864
  • 10
  • 22
0

If you are just trying to get the full list of columns for a table, (not CTE) you can do this:

In SSMS, open the Object Explorer and find the Table. Right click on it and choose "Select Top 1000 Rows".

It will give you a query like this:

SELECT TOP (1000) [ColumnName1]
    ,[ColumnName2]
    ,[Column Name 3]
    ...
FROM [databaseName].[schemaName].[TableName]

exhaustively listing all of the column names with square brackets around them.

TechSavvySam
  • 1,382
  • 16
  • 28
-1

It is called the wildcard and it is bad practice to use it in production code. It is also bad practice to do a "SELECT *" on anything without at least a "TOP n" expression.

For instance a view which includes a wildcard will have to be rebuilt in order to properly function again if you ever change the schema of the table it refers.

As Gordon says, you can use the UI or, as I prefer myself, intellisense the column names.

The UI will impose the use of the square bracket, which enhances compatibility, especially for objects named after reserved keywords or starting with a number, for instance.

NicVerAZ
  • 409
  • 1
  • 4
  • 10