2014-09-12 69 views
0

我試圖運行下面的腳本,但我得到一個錯誤的SQL錯誤...不確定如何解決

消息306,級別16,狀態2,行58
文本,ntext和圖像數據類型不能被比較或排序,除非使用IS NULL或LIKE運算符。

有人能告訴我我哪裏出錯了。請儘量溫柔,因爲我對這一切都很陌生。 :)

在此先感謝!

SELECT 
    c.address_number as address_number, 
    c.contact_number as contact_number, 
    c.label_name as contact_name, 
    a.address as address, 
    a.postcode as postcode, 
    s.source as source, 
    s.source_desc as source_desc, 
    m.joined as member_join_date, 
    m.membership_card_expires as member_expiry_date, 
    o.payment_method as payment_method, 
    pf.payment_frequency_desc as payment_frequency_desc, 
    pf.frequency as payment_frequency, 
    c.std_code as std_code, 
    c.telephone as phone_number, 
    gift_aid = CASE 
       WHEN gad.declaration_number IS NOT NULL then 'Y' else null end, 
    cp.first_payment_date as first_payment_date, 
    cp.last_payment_date as last_payment_date, 
    -- NUMBER OF DONATIONS IN THE LAST 12 MONTHS GOES HERE -- 
    cp.value_of_payments as total_donation_amount 
FROM 
    contacts c 
INNER JOIN 
    contact_addresses ca ON c.contact_number = ca.contact_number 
         AND c.address_number = ca.address_number 
INNER JOIN 
    addresses a ON ca.address_number = a.address_number 
INNER JOIN 
    sources s ON c.source = s.source 
INNER JOIN 
    contact_suppressions cs ON c.contact_number = cs.contact_number 
INNER JOIN 
    orders o ON c.contact_number = o.contact_number 
INNER JOIN 
    members m ON c.contact_number = m.contact_number 
INNER JOIN 
    contact_categories cc ON c.contact_number = cc.contact_number 
INNER JOIN 
    payment_frequencies pf ON o.payment_frequency = pf.payment_frequency 
LEFT OUTER JOIN 
    gift_aid_declarations gad ON c.contact_number = gad.contact_number 
          AND (start_date IS NULL OR start_date < GETDATE()) 
INNER JOIN 
    contact_performances cp ON c.contact_number = cp.contact_number 
WHERE 
    -- EXCLUSIONS -- 
    c.status NOT IN ('DECD','AN','DU','GONE') AND 
    c.contact_number NOT IN (SELECT contact_number FROM contact_suppressions 
          WHERE mailing_suppression IN ('NOMA','APP','DATA')) AND 
    c.contact_number NOT IN (SELECT contact_number FROM contact_categories 
          WHERE activity IN ('MAJDON', 'PATRON', 'PCD11', 'PCT', 
               'PCAD12', 'PCAD13', 'PCFI13', 
               'PCHR12', 'PCIA12', 'PCNH12', 
               'PCSL13', 'PCSK13', 'PCTC13')) AND 
    ca.historical = 'N' AND 
    -- MEMEBRSHIP NOT CANCELLED -- 
    m.cancellation_reason IS NULL 
ORDER BY 
    address_number, contact_number, contact_name, address, postcode, source, source_desc, member_join_date, 
member_expiry_date, payment_method, payment_frequency_desc, payment_frequency, std_code, phone_number, gift_aid,  first_payment_date, last_payment_date, total_donation_amount 
+1

請確定哪些列是「text」,「ntext」或「image」。 – 2014-09-12 14:53:02

+0

將在未來版本的SQL Server中刪除'ntext','text'和'image'數據類型。避免在新的開發工作中使用這些數據類型,並計劃修改當前正在使用它們的應用程序。改爲使用'nvarchar(max)','varchar(max)'和'varbinary(max)'。 [詳細請看這裏](http://msdn.microsoft.com/en-us/library/ms187993.aspx) – 2014-09-12 16:02:26

回答

0

您需要弄清楚您正在使用哪一列是text,ntext或image數據類型列。根據錯誤消息,您正在使用此數據類型的列進行「排序或」比較「。在此查詢中,該數據位於WHERE子句中,您的連接的ON子句,ORDER BY子句,ORDER BY子句,或者一個CASE語句中的可能。

完全基於colunn的名字,我想這是sourcegift_aid,但無法確定在沒有仔細檢查的模式。

+1

只需要提一下,在SQL 2012中,你真的*不應該使用文本,ntext或圖像數據類型。我猜測你已經繼承了多年前創建的數據庫,使得這個問題無法控制。 – 2014-09-12 14:59:46

0

的錯誤指示線58是「Cancellation_reason IS NULL」

也許你可以應用一個LEFT()來得到一個字符串,然後讓它測試爲空,就像

LEFT(m.cancellation_reason, 1) IS NULL 

所以在這裏,我只關心取消原因的第一個字符。如果它爲空,它不會返回爲空嗎?,

相關問題