2014-05-12 58 views
0

Im嘗試指定標題爲「EmailAddress」的子查詢列的名稱。下面的代碼是說'沒有列名'可以有人告訴我我的方式錯誤?更改嵌套子查詢中的列名稱

SELECT  'RFW' + CAST(ALOPS.dbo.PLSupplierAccount.PLSupplierAccountID AS VARCHAR(10)) AS PKnumber, 
(SELECT Max(alops.dbo.PLSupplierContactValue.ContactValue)AS EmailAddress 
    FROM alops.dbo.PLSupplierAccount as PLSup, ALOPS.dbo.PLSupplierContact, 
     ALOPS.dbo.PLSupplierContactValue ,  ALOPS.dbo.SYSContactType 
    WHERE 
    PLSup.PLSupplierAccountID = alops.dbo.PLSupplierAccount.PLSupplierAccountID 
    AND PLSup.PLSupplierAccountID = alops.dbo.PLSupplierContact.PLSupplierAccountID 
    AND alops.dbo.PLSupplierContactValue.PLSupplierContactID = 
    alops.dbo.PLSupplierContact.PLSupplierContactID 
    AND alops.dbo.SYSContactType.SYSContactTypeID = alops.dbo.PLSupplierContactValue.SYSContactTypeID 
    AND alops.dbo.SYSContactType.Name='E-mail Address') , 
    ALOPS.dbo.PLSupplierAccount.PLSupplierAccountID AS AccountID, 
    ALOPS.dbo.PLSupplierAccount.SupplierAccountNumber AS AccountRef,  
    ALOPS.dbo.PLSupplierAccount.SupplierAccountName AS AccountName, 'RFW' AS Company, 
    ALOPS.dbo.PLSupplierLocation.AddressLine1, ALOPS.dbo.PLSupplierLocation.AddressLine2, 
    ALOPS.dbo.PLSupplierLocation.AddressLine3, 
    ALOPS.dbo.PLSupplierLocation.AddressLine4, ALOPS.dbo.PLSupplierLocation.Postcode, 
    (ALOPS.dbo.PLSupplierAccount.MainTelephoneAreaCode + 
    ALOPS.dbo.PLSupplierAccount.MainTelephoneSubscriberNumber) as Telephone 
      FROM  
       ALOPS.dbo.PLSupplierAccount INNER JOIN 
       ALOPS.dbo.PLSupplierLocation ON ALOPS.dbo.PLSupplierAccount.PLSupplierAccountID =    
       ALOPS.dbo.PLSupplierLocation.PLSupplierAccountID 

回答

0

別名是在錯誤的地方嘗試

(SELECT Max(alops.dbo.PLSupplierContactValue.ContactValue) 
FROM alops.dbo.PLSupplierAccount as PLSup, ALOPS.dbo.PLSupplierContact, ALOPS.dbo.PLSupplierContactValue , ALOPS.dbo.SYSContactType) AS EmailAddress 
+0

完美,但我知道這是簡單的東西!再次感謝! –

0

你需要給別名到內選擇以及定義列名。檢查以下

SELECT  'RFW' + CAST(ALOPS.dbo.PLSupplierAccount.PLSupplierAccountID AS VARCHAR(10)) AS PKnumber, 
(SELECT Max(alops.dbo.PLSupplierContactValue.ContactValue) 
    FROM alops.dbo.PLSupplierAccount as PLSup, ALOPS.dbo.PLSupplierContact, 
     ALOPS.dbo.PLSupplierContactValue ,  ALOPS.dbo.SYSContactType 
    WHERE 
    PLSup.PLSupplierAccountID = alops.dbo.PLSupplierAccount.PLSupplierAccountID 
    AND PLSup.PLSupplierAccountID = alops.dbo.PLSupplierContact.PLSupplierAccountID 
    AND alops.dbo.PLSupplierContactValue.PLSupplierContactID = 
    alops.dbo.PLSupplierContact.PLSupplierContactID 
    AND alops.dbo.SYSContactType.SYSContactTypeID = alops.dbo.PLSupplierContactValue.SYSContactTypeID 
    AND alops.dbo.SYSContactType.Name='E-mail Address') AS EmailAddress, 
    ALOPS.dbo.PLSupplierAccount.PLSupplierAccountID AS AccountID, 
    ALOPS.dbo.PLSupplierAccount.SupplierAccountNumber AS AccountRef,  
    ALOPS.dbo.PLSupplierAccount.SupplierAccountName AS AccountName, 'RFW' AS Company, 
    ALOPS.dbo.PLSupplierLocation.AddressLine1, ALOPS.dbo.PLSupplierLocation.AddressLine2, 
    ALOPS.dbo.PLSupplierLocation.AddressLine3, 
    ALOPS.dbo.PLSupplierLocation.AddressLine4, ALOPS.dbo.PLSupplierLocation.Postcode, 
    (ALOPS.dbo.PLSupplierAccount.MainTelephoneAreaCode + 
    ALOPS.dbo.PLSupplierAccount.MainTelephoneSubscriberNumber) as Telephone 
      FROM  
       ALOPS.dbo.PLSupplierAccount INNER JOIN 
       ALOPS.dbo.PLSupplierLocation ON ALOPS.dbo.PLSupplierAccount.PLSupplierAccountID =    
       ALOPS.dbo.PLSupplierLocation.PLSupplierAccountID 

希望這有助於