简体   繁体   中英

SQL Error: Invalid object name 'Table name'

I have 2 tables as below:

tblTransaction
tblMobileRegistration

I want to join these 2 tables and count the numbers of raws that specific columns value in that is not '1' as below:

SELECT COUNT(Transaction_MobileErrorCode) FROM T1 WHERE Transaction_MobileErrorCode <> ''

Its my whole query:

SELECT 
T1.*, 
T2.*,  
(SELECT COUNT(Transaction_MobileErrorCode) FROM T1 WHERE Transaction_MobileErrorCode <> '')  
FROM 
(SELECT MobileRegistration_DateTime, Transaction_MobileErrorCode FROM tblTransaction, tblMobileRegistration
 WHERE T1.ID1 = T2.ID1 AND 
      T1.ID2 = '111111' AND
      T1.ID3 = '222222' AND
      T1.ID4 = '333333' AND
      T1.ID5 = '444444') AS T1,

      tblMobileRegistration AS T2

but I got this error:

Invalid object name 'T1'.

so how can I fix it? thanks for any helping...

It is a bit hard to tell what results you actually want. But this following may do what you want:

SELECT MobileRegistration_DateTime, Transaction_MobileErrorCode,
       SUM(CASE WHENTransaction_MobileErrorCode <> '' THEN 1 ELSE 0 END) OVER ()
FROM tblTransaction t JOIN
     tblMobileRegistration mr
     ON t.ID1 = mr.ID1
WHERE T1.ID2 = '111111' AND
      T1.ID3 = '222222' AND
      T1.ID4 = '333333' AND
      T1.ID5 = '444444';

You should learn to use proper, explicit join syntax. You simply have too many table references for what you probably want to do.

This version uses a window function. You don't specify the database, but this is ANSI standard syntax supported by most databases.

This is your inner most query. Nothing is aliased as T1 (or T2). You are either mixing up your parenthesis, or missing aliases. If you're missing aliases, you really need to get a bit more creative than just using t1 and t2 everywhere. Makes debugging your query very difficult.

    SELECT MobileRegistration_DateTime, 
Transaction_MobileErrorCode 
FROM tblTransaction, 
tblMobileRegistration
         WHERE T1.ID1 = T2.ID1 AND 
              T1.ID2 = '111111' AND
              T1.ID3 = '222222' AND
              T1.ID4 = '333333' AND
              T1.ID5 = '444444'

As other folks have said, your query is very confusing. If you really are intending to have all those derived tables, break it down and build it from the inside out.

It was a runtime error.

At first it will checks all columns in select statement from tables but not the assigned table(T1 will created while executing and deleted after that execution) remove that select statement (from t1) then execute it will work.

(SELECT COUNT(Transaction_MobileErrorCode) FROM T1)

from this line you are getting error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM