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
 Why do I have to have a string after last bracket?

Author  Topic 

SQLGeno
Starting Member

13 Posts

Posted - 2011-01-26 : 23:44:52
Not a problem but someing I am curious about.

In the following SQL why must I put some guff (in the example shown I have "WahteverYouLikeHere" after the closing bracket in the Sub-Select? It seems to have no meaning that I can gather. Glad to be enlightned.....



SELECT *
INTO OffenderCaseHistory
FROM
(
SELECT OffenderCaseId,
StatusId,
CreatedDate
FROM OffenderCase

UNION ALL

SELECT OffenderCaseId,
StatusId,
ActionDate
FROM OffenderCaseAud
) WhateverYouLikeHere -- Why do I have to put something here?

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2011-01-26 : 23:55:05
I think its just a rule to represent a derived data set with an Alias/Name. For me the question is similar like to ... Is it necessary that a Name should be given to you, could you not be nameless :)
Go to Top of Page

SQLGeno
Starting Member

13 Posts

Posted - 2011-01-27 : 00:15:11
Yeah, but unless I am missing something it is not possible to reference this "derived" set. i.e. I can't write
SELECT *
FROM WhateverYouLikeHere
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2011-01-27 : 00:27:41
Yes you are missing and thats the name of the Derived Table which will be referenced by the From clause.

you can use inside bracket's query stand alone but while referring it through the From clause you will have to name the derived Data set.

By the way this is the way how SQL translator is designed. Change it as per your requirement if you want ... :) I don't have knowledge of Complier/translator development :D
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2011-01-27 : 01:09:59
also if you were joining the derived table to something else, then a name would truly be _necessary_ and not merely required:

select *
from (select 1 as a) x
join (select 1 as a) y on x.a=y.a

the alias on derived tables is simply required and that's that. :)


elsasoft.org
Go to Top of Page
   

- Advertisement -