Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Syntax of schema(?)

Author  Topic 

PatrickLi
Starting Member

2 Posts

Posted - 2012-02-17 : 12:44:21
Hi there,

I asked a friend for some assistance with a SQL statement for self joins. It works fine but I was trying to understand what he did.

He gave me this:

select dt.book_name,
dt.date_value datetime,
ph.number_value phone,
gl.number_value glass
from export dt
join export ph on
ph.book_template_name = dt.book_template_name
and ph.book_name = dt.book_name
and ph.page_name = dt.page_name
and ph.row_number = dt.row_number
join export gl on
gl.book_template_name = dt.book_template_name
and gl.book_name = dt.book_name
and gl.page_name = dt.page_name
and gl.row_number = dt.row_number
where dt.book_template_name = 'Alice'
and dt.page_name = '12'
and dt.field_name = 'font';


I'm assuming "dt.date_value datetime" means "dt.date_value as datetime" but I don't understand the "dt." portion. Is it a schema? I don't have that as a schema but the script seems to work. He later uses in a separate script:


select ...
from export dt
join export ph on ...


which seems to reference the portion from the top. Can anyone explain what the significance of [value].[field_name] syntax is and how it is possible to use [value] in a later script? Much thanks in advance.

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-02-17 : 12:47:46
It's table alias. The full syntax would be:
gl.number_value glass
from export AS dt
join export AS ph on
You can omit the AS and SQL would understand that you are trying to use an alias.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-17 : 13:01:24
please note that datetime is a reserved word in sql so you might be better off enclosing it in [] ie [datetime] so that compiler doesnt complain

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

PatrickLi
Starting Member

2 Posts

Posted - 2012-02-17 : 13:01:36
Ah! Thats what it is. I made the connection up top but it escaped me in the latter half. Thanks again!
Go to Top of Page
   

- Advertisement -