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?
So the desired results would be:
with A as (select
MyColumn1=1
,MyColumn2=2
,MyColumn3=3)
select
MyColumn1
,MyColumn2
,MyColumn3
from A
6条答案
按热度按时间iqjalb3h1#
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.
9w11ddsr2#
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:
Click over "Options"
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:
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:
As you can see the procedure has two parameters, the second parameter is to send an alias, this is an example:
68de4m5k3#
Converting * in a cte to column names in Management studio
... select * into MyTable from A
Script table as Select
if you prefer column names with []delete MyTable
ej83mcc04#
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:
exhaustively listing all of the column names with square brackets around them.
yvgpqqbh5#
typically the question you ask on here doesn't get answered. I do actually have an answer. Firstly, everyone elses answers here are lazy. We know we can drag over the columns from the table but when you're creating queries upon other queries and you only want the fields from the previous nested select, you don't want every field from the table. Here's what I do... Run the query with the asterisk. Select the 1st record, right click and select copy with headers. Paste into Excel, take the top row (headings) and paste with Transpose (so vertical paste) into A2. In the column to the right add =CONCATENATE(B2,", ",A2) - if you do this correctly and copy this value down, you will have a growing text of comma separated field names. Copy the bottom row and paste into your SQL. The whole thing can be done in 15 seconds and if gives you every field from the recordset.
7kjnsjlb6#
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.