-5
    SELECT * INTO [H_ACTIVE_STUDENTS]  FROM
/* The section below works independently fine, but generates error when used as subquery */
    (SELECT VIEW_H_ACTIVE_STUDENTS.STUD_ID, 
       VIEW_H_ACTIVE_STUDENTS.STUD_LAST_NAME, 
       VIEW_H_ACTIVE_STUDENTS.STUD_FIRST_NAME, 
       VIEW_H_ACTIVE_STUDENTS.STUD_MIDDLE_INITIAL, 
       CAST(VIEW_H_ACTIVE_STUDENTS.STUD_BIRTH AS DATE) AS STUD_BIRTH, 
       VIEW_H_ACTIVE_STUDENTS.STUD_SEX, 
       VIEW_H_ACTIVE_STUDENTS.STUD_MAIL_STREET_ADDRESS,
       VIEW_H_ACTIVE_STUDENTS.STUD_MAIL_CITY, 
       VIEW_H_ACTIVE_STUDENTS.STUD_MAIL_ST, 
       VIEW_H_ACTIVE_STUDENTS.STUD_MAIL_ZIP_CODE,
       VIEW_H_ACTIVE_STUDENTS.STUD_AREA_CD, 
       VIEW_H_ACTIVE_STUDENTS.STUD_PHONE_NO, 
       VIEW_H_ACTIVE_STUDENTS.STUD_PIN, 
       VIEW_H_ACTIVE_STUDENTS.SEMC_YR_SEM, 
       VIEW_H_ACTIVE_STUDENTS.SEMC_ACTIVE_UNITS, 
       VIEW_H_ACTIVE_STUDENTS.SEMC_FINAID_ACTIVE_UNITS, 
       VIEW_H_ACTIVE_STUDENTS.SEMC_ACTIVE_HOURS, 
       VIEW_H_ACTIVE_STUDENTS.SEMC_FINAID_ACTIVE_HOURS, 
       H_LAST_NAME_UPD  = CAST(REPLACE(REPLACE(REPLACE(REPLACE      (VIEW_H_ACTIVE_STUDENTS.STUD_LAST_NAME,' ',''),',',''),'-',''),'''','') AS CHAR(30)),
       H_FIRST_NAME_UPD = CAST(REPLACE(REPLACE(REPLACE(REPLACE(VIEW_H_ACTIVE_STUDENTS.STUD_FIRST_NAME,' ',''),',',''),'-',''),'''','') AS CHAR(15)),
       H_ADRESS_UPD = CAST(REPLACE(VIEW_H_ACTIVE_STUDENTS.STUD_MAIL_STREET_ADDRESS,',','') AS CHAR(30)), /* here is where error pointed*/
       H_STUD_ID = CAST(LEFT(REPLACE(REPLACE(REPLACE(REPLACE(VIEW_H_ACTIVE_STUDENTS.STUD_LAST_NAME,' ',''),',',''),'-',''),'''',''), 3) + LEFT(REPLACE(REPLACE(REPLACE(REPLACE(VIEW_H_ACTIVE_STUDENTS.STUD_FIRST_NAME,' ',''),',',''),'-',''),'''',''),1) + RIGHT(VIEW_H_ACTIVE_STUDENTS.STUD_ID,4) AS CHAR(8))

    FROM   [VIEW_H_ACTIVE_STUDENTS] )
4

1 回答 1

4

SQL Server 需要所有派生表和子查询的别名,因此您需要在最后一个括号后添加别名:

SELECT * 
INTO   [h_active_students] 
FROM   (SELECT view_h_active_students.stud_id, 
               view_h_active_students.stud_last_name, 
               view_h_active_students.stud_first_name, 
               view_h_active_students.stud_middle_initial, 
               Cast(view_h_active_students.stud_birth AS DATE) AS STUD_BIRTH, 
               view_h_active_students.stud_sex, 
               view_h_active_students.stud_mail_street_address, 
               view_h_active_students.stud_mail_city, 
               view_h_active_students.stud_mail_st, 
               view_h_active_students.stud_mail_zip_code, 
               view_h_active_students.stud_area_cd, 
               view_h_active_students.stud_phone_no, 
               view_h_active_students.stud_pin, 
               view_h_active_students.semc_yr_sem, 
               view_h_active_students.semc_active_units, 
               view_h_active_students.semc_finaid_active_units, 
               view_h_active_students.semc_active_hours, 
               view_h_active_students.semc_finaid_active_hours, 
               H_LAST_NAME_UPD = Cast( 
Replace( 
Replace(Replace(Replace 
(view_h_active_students.stud_last_name, ' ', ''), ',', ''), '-', ''), '''', '') 
AS CHAR(30)), 
H_FIRST_NAME_UPD = Cast(Replace( 
Replace( 
Replace(Replace( 
view_h_active_students.stud_first_name, ' ', ''), ',', ''), '-', ''), '''', '') 
                        AS CHAR(15)), 
H_ADRESS_UPD = Cast( 
Replace(view_h_active_students.stud_mail_street_address, ',', '') AS 
CHAR( 
               30)), 
H_STUD_ID = 
Cast( 
LEFT(Replace(Replace(Replace(Replace(view_h_active_students.stud_last_name, ' ', ''), ',', ''), '-', ''), '''', ''), 3) 
+ 
LEFT(Replace(Replace(Replace(Replace(view_h_active_students.stud_first_name, ' ', ''), ',', ''), '-', ''), '''', ''), 1) 
+ RIGHT(view_h_active_students.stud_id, 4) AS CHAR(8)) 
 FROM   [view_h_active_students]) data -- add this
于 2013-09-03T20:59:09.887 回答