简体   繁体   中英

Shortcut for adding table to column name SQL-server 2014

Stupidly simple question, but I just don't know what to google!

If I create a query like this:

Select id, data
from table1

Now I want to join with table2. I can immediately see that the id column is no longer unique and I have to change it to

table1.id

Is there any smart way (like a keyboard-shortcut) to do this, instead of manually adding table1 to every column? Either before I add the Join to secure that all columns will be unique, or after with suggestions based on the different possible tables.

No, there is no helper.

But do not you can alias the table name:

select x.Col1, y.Col2
from ALongTableName x
  inner join AReallyReallyLongTableName y on x.Id = y.OtherId

which can also make queries clearer, and is very much necessary when doing self joins.

First of all, you should start using aliases :

SQL aliases are used to give a database table, or a column in a table, a temporary name.

Basically aliases are created to make column names more readable.

This will narrow down your problem and make your code maintenance easier. If that's not enough, I guess you could start using auto-completion tools, such as these:

  1. SQL Complete
  2. SQL Prompt
  3. ApexSQL Complete

These have your desired functionality, however, they do not always work as expected (at least for me).

Oh! You can use alias table name. Like this:

SELECT A.ID, A.data
FROM TableA A
INNER JOIN TableB B
ON A.ID = B.ID

You just only use A. or B. if two table have same this column selected. If they different, you don't need: Like this:

SELECT A.ID, data -- if Table B not have column data
FROM TableA A
INNER JOIN TableB B
ON A.ID = B.ID

Or:

Select A.*, B.ID
FROM TableA A
INNER JOIN TableB B
ON A.ID = B.ID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM