Unknown character in SQL statement

I think this is a question like SQL newbies, but here it goes.

I have SQL Query (SQL Server 2005), which I compiled based on a user-defined function:

SELECT 
    CASEID,
    GetNoteText(CASEID)
FROM 
( 
    SELECT 
        CASEID 
    FROM 
        ATTACHMENTS 
    GROUP BY 
        CASEID 
) i
GO 

UDF works fine (it concatenates data from several rows in a linked table, if that matters at all), but I'm confused about the "i" after the FROM clause. The query works fine with i, but without it. What is the meaning of "i"?

EDIT: As Joel noted below, this is not a keyword

+3
source share
7 answers

FROM, . , "i" "a". , - "i" , , , .

, . , - .

+16

(), , .

, , :

SELECT 
    c.CASEID, c.CASE_NAME,
    a.COUNT AS ATTACHMENTSCOUNT, o.COUNT as OTHERCOUNT,
    dbo.GetNoteText(c.CASEID)
FROM CASES c
LEFT OUTER JOIN
( 
    SELECT 
        CASEID, COUNT(*) AS COUNT
    FROM 
        ATTACHMENTS 
    GROUP BY 
        CASEID 
) a
ON a.CASEID = c.CASEID
LEFT OUTER JOIN
(
    SELECT 
        CASEID, COUNT(*) AS COUNT
    FROM 
        OTHER
    GROUP BY 
        CASEID 
) o
ON o.CASEID = c.CASEID
+6

"i" . ( - MSSQLServer) "AS i".

+3

, .

i.CASEID .

, , .

, . "" .

+1

, , , , .

, , , , SQL-, , . id / c.name.

+1

" " FROM.

SQL Server Books Online , table_alias ; "table_alias" Transact-SQL, . "AS" , , ...

  output_table [AS] table_alias [(column_alias [, ... n])]

FROM (Transact-SQL):
http://msdn.microsoft.com/en-us/library/ms177634(SQL.90).aspx

Transact-SQL syntax conventions:
http://msdn.microsoft.com/en-us/library/ms177563(SQL.90).aspx

0
source

A lesson to learn is thinking about the person who inherits your code. As others said, if the code was written as follows:

SELECT DT1.CASEID, GetNoteText(DT1.CASEID) 
FROM (
   SELECT CASEID 
   FROM ATTACHMENTS
   GROUP BY CASEID
) AS DT1 (CASEID);

then there would be a high probability that the reader would figure it out and might even pick up "DT1", referring to the "view".

0
source

Source: https://habr.com/ru/post/1697769/


All Articles