SQL Error: Invalid object name 'Table name' - sql

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

Related

Oracle SQL: Trying to update multiple rows from joined table without 1-1 relationship

Former SQL Server dataminer here expanding my skills. Complete newbie to Oracle. I've run into multiple error messages trying to convert this SQL query to work in Oracle:
UPDATE table1
SET program = SUBSTR(table2.project,1,5)
FROM table1
LEFT OUTER JOIN table2
ON table1.id = table2.id
WHERE table1.program = 'THE_PROGRAM_NAME'
AND table2.program = 'THE_PROGRAM_NAME';
I've dealt with each error having gone through various StackOverflow questions, but none I've encountered have helped me resolve it fully. The basic problem is that Oracle doesn't want to deal with taking one line from the joined table to update multiple lines from the primary table, and I don't know how to address this.
All I've read so far seems to indicate that this is an insurmountable problem so I'm asking my own new question to have that confirmed or refuted, and either get a whole new approach to try or that so-far-elusive solution.
This is as far as I have gotten, and it's led me to "ORA-30926 unable to get a stable set of rows in the source tables":
MERGE INTO table1 ce
USING
(SELECT DISTINCT SUBSTR(table2.project,1,5) newvalue, table1.code, table1.id
FROM table1
LEFT OUTER JOIN table2
ON table1.id = table2.id
WHERE table1.program = 'THE_PROGRAM_NAME'
AND table2.program = 'THE_PROGRAM_NAME'
) combined
ON (ce.id = combined.id
AND ce.code = combined.code)
WHEN MATCHED THEN
UPDATE SET ce.program = combined.newvalue;
If you need more information to be able help please ask.
Any help greatly appreciated.
Regards,
Oracle does not support joins in update queries. A typical translation would use a correlated subquery:
UPDATE table1 t1
SET program = (
SELECT SUBSTR(t2.project,1,5)
FROM table2 t2
WHERE t1.id = t2.id AND t2.program = t1.program
)
WHERE program = 'THE_PROGRAM_NAME';
Note that this would update program to null if there is no match in table2. If you want to avoid that, then add a condition in the WHERE clause:
UPDATE table1 t1
SET program = (
SELECT SUBSTR(t2.project,1,5)
FROM table2 t2
WHERE t1.id = t2.id AND t2.program = t1.program
)
WHERE program = 'THE_PROGRAM_NAME' AND EXISTS (
SELECT 1
FROM table2 t2
WHERE t1.id = t2.id AND t2.program = t1.program
);

Left join with sub query

Not sure why its throwing an error at t2. I am trying to run a simple sql query.
Running on MS-SQL and the error message says 'incorrect syntax near t2'
UPDATE t1
SET t1.EmpSubCompetency = t2.EmpSubCompetency,
t1.Competency = t2.Competency,
t1.FileName = t2.FileName,
t1.Longitude = t2.Longitude,
t1.Latitude = t2.Latitude,
t1.SubAreaName = t2.Region,
t1.SectorTag=t2.SectorTagClassification
FROM dbo.STG_MyCompetencies t1
LEFT JOIN (select * from dbo.STG_EmployeeMaster where Act_Flg='Y') t2
Your problem is the missing ON clause. Further, you don't need a subquery for this logic:
FROM dbo.STG_MyCompetencies t1 LEFT JOIN
dbo.STG_EmployeeMaster t2
ON t1.??? = t2.??? AND
em.Act_Flg = 'Y'
Note that unmatched rows will have all the columns set to NULL.
The ??? is for whatever column should be used for the JOIN.

Oracle Updates with left outer join when we have case statement in select

I wanted to know how to write this query in Oracle SQL:
UPDATE address
SET phone1 = sp.phone,
is_avlbl = ( CASE
WHEN sp.name IS NULL THEN 1
ELSE 0
END )
FROM address ad
LEFT JOIN speaker sp
ON sp.addressid = ad.id
The above query format is from MS SQL Server but I want to achieve similar functionality with Oracle.
Already seen Update and left outer join statements, which is for T-SQL.
EDIT
I have tried the following solution:
update
table1 t1
set
(
t1.column1,
t1.column2,
t1.column3
) = (
select
t2.column1,
t2.column2,
( CASE
WHEN t2.column2 IS NULL THEN 1
ELSE 0
END )
from
table2 t2
where
t2.column1 = t1.column1
);
But the problem is that When there is no record in t2 corresponding to t1, then the above sql inserts null values into t1 where as i need some other value inserted into it when there is no such record. I apologize if this part of the requirement was not clear earlier.
Something like this maybe:
merge into address
using
(
SELECT ad.id,
sp.phone,
sp.name
FROM address ad
LEFT JOIN speaker sp ON sp.addressid = ad.id
) t on (address.id = t.id)
when matched then update
set phone1 = t.phone,
is_avlbl = case
when t.name is null then 1
else 0
end;
(This assumes that address.id is the primary key)
Not tested though, there might be typos that cause syntax errors.

SQL command to compare results is not working [duplicate]

I want to update a column in a table making a join on other table e.g.:
UPDATE table1 a
INNER JOIN table2 b ON a.commonfield = b.[common field]
SET a.CalculatedColumn= b.[Calculated Column]
WHERE
b.[common field]= a.commonfield
AND a.BatchNO = '110'
But it is complaining :
Msg 170, Level 15, State 1, Line 2
Line 2: Incorrect syntax near 'a'.
What is wrong here?
You don't quite have SQL Server's proprietary UPDATE FROM syntax down. Also not sure why you needed to join on the CommonField and also filter on it afterward. Try this:
UPDATE t1
SET t1.CalculatedColumn = t2.[Calculated Column]
FROM dbo.Table1 AS t1
INNER JOIN dbo.Table2 AS t2
ON t1.CommonField = t2.[Common Field]
WHERE t1.BatchNo = '110';
If you're doing something silly - like constantly trying to set the value of one column to the aggregate of another column (which violates the principle of avoiding storing redundant data), you can use a CTE (common table expression) - see here and here for more details:
;WITH t2 AS
(
SELECT [key], CalculatedColumn = SUM(some_column)
FROM dbo.table2
GROUP BY [key]
)
UPDATE t1
SET t1.CalculatedColumn = t2.CalculatedColumn
FROM dbo.table1 AS t1
INNER JOIN t2
ON t1.[key] = t2.[key];
The reason this is silly, is that you're going to have to re-run this entire update every single time any row in table2 changes. A SUM is something you can always calculate at runtime and, in doing so, never have to worry that the result is stale.
Try it like this:
UPDATE a
SET a.CalculatedColumn= b.[Calculated Column]
FROM table1 a INNER JOIN table2 b ON a.commonfield = b.[common field]
WHERE a.BatchNO = '110'
Answer given above by Aaron is perfect:
UPDATE a
SET a.CalculatedColumn = b.[Calculated Column]
FROM Table1 AS a
INNER JOIN Table2 AS b
ON a.CommonField = b.[Common Field]
WHERE a.BatchNo = '110';
Just want to add why this problem occurs in SQL Server when we try to use alias of a table while updating that table, below mention syntax will always give error:
update tableName t
set t.name = 'books new'
where t.id = 1
case can be any if you are updating a single table or updating while using join.
Although above query will work fine in PL/SQL but not in SQL Server.
Correct way to update a table while using table alias in SQL Server is:
update t
set t.name = 'books new'
from tableName t
where t.id = 1
Hope it will help everybody why error came here.
MERGE table1 T
USING table2 S
ON T.CommonField = S."Common Field"
AND T.BatchNo = '110'
WHEN MATCHED THEN
UPDATE
SET CalculatedColumn = S."Calculated Column";
UPDATE mytable
SET myfield = CASE other_field
WHEN 1 THEN 'value'
WHEN 2 THEN 'value'
WHEN 3 THEN 'value'
END
From mytable
Join otherTable on otherTable.id = mytable.id
Where othertable.somecolumn = '1234'
More alternatives here.
Seems like SQL Server 2012 can handle the old update syntax of Teradata too:
UPDATE a
SET a.CalculatedColumn= b.[Calculated Column]
FROM table1 a, table2 b
WHERE
b.[common field]= a.commonfield
AND a.BatchNO = '110'
If I remember correctly, 2008R2 was giving error when I tried similar query.
I find it useful to turn an UPDATE into a SELECT to get the rows I want to update as a test before updating. If I can select the exact rows I want, I can update just those rows I want to update.
DECLARE #expense_report_id AS INT
SET #expense_report_id = 1027
--UPDATE expense_report_detail_distribution
--SET service_bill_id = 9
SELECT *
FROM expense_report_detail_distribution erdd
INNER JOIN expense_report_detail erd
INNER JOIN expense_report er
ON er.expense_report_id = erd.expense_report_id
ON erdd.expense_report_detail_id = erd.expense_report_detail_id
WHERE er.expense_report_id = #expense_report_id
Another approach would be to use MERGE
;WITH cteTable1(CalculatedColumn, CommonField)
AS
(
select CalculatedColumn, CommonField from Table1 Where BatchNo = '110'
)
MERGE cteTable1 AS target
USING (select "Calculated Column", "Common Field" FROM dbo.Table2) AS source ("Calculated Column", "Common Field")
ON (target.CommonField = source."Common Field")
WHEN MATCHED THEN
UPDATE SET target.CalculatedColumn = source."Calculated Column";
-Merge is part of the SQL Standard
-Also I'm pretty sure inner join updates are non deterministic..
Similar question here where the answer talks about that
http://ask.sqlservercentral.com/questions/19089/updating-two-tables-using-single-query.html
I think, this is what you are looking for.
UPDATE
Table1
SET
Table1.columeName =T1.columeName * T2.columeName
FROM
Table1 T1
INNER JOIN Table2 T2
ON T1.columeName = T2.columeName;
I had the same issue.. and you don't need to add a physical column.. cuz now you will have to maintain it..
what you can do is add a generic column in the select query:
EX:
select tb1.col1, tb1.col2, tb1.col3 ,
(
select 'Match' from table2 as tbl2
where tbl1.col1 = tbl2.col1 and tab1.col2 = tbl2.col2
)
from myTable as tbl1
Aaron's approach above worked perfectly for me. My update statement was slightly different because I needed to join based on two fields concatenated in one table to match a field in another table.
--update clients table cell field from custom table containing mobile numbers
update clients
set cell = m.Phone
from clients as c
inner join [dbo].[COSStaffMobileNumbers] as m
on c.Last_Name + c.First_Name = m.Name
Those who are using MYSQL
UPDATE table1 INNER JOIN table2 ON table2.id = table1.id SET table1.status = 0 WHERE table1.column = 20
Try:
UPDATE table1
SET CalculatedColumn = ( SELECT [Calculated Column]
FROM table2
WHERE table1.commonfield = [common field])
WHERE BatchNO = '110'

How to simplify this query with sql joins?

my_table has 4 columns: id integer, value integer, value2 integer, name character varying
I want all the records that:
have the same value2 as a record which name is 'a_name'
have a field value inferior to the one of a record which name is 'a_name'
And I have satisfying results with the following query:
select t.id
from my_table as t
where t.value < ( select value from my_table where name = 'a_name')
and s.value2 = (select value2 from my_table where name = 'a_name');
But is it possible to simplify this query with sql joins ?
Joining on the same table is still too much intricate in my mind. And I try to understand with this example.
What I happened so far trying, is a result full of dupplicates:
select t2.id
from my_table as t
inner join my_table as t2 on t2.value2 = t.value2
where t2.value < ( select value from my_table where name = 'a_name');
I think this will solve your problem.
select t1.id
from my_table as t1
join my_table as t2
on t1.value2 = t2.value2
and t2.name = 'a_name'
and t1.value < t2.value
You should use self join instead of inner join see this
http://msdn.microsoft.com/en-us/library/ms177490%28v=sql.105%29.aspx
You can always get distinct results by calling "SELECT distinct t2.id ..."
However, that will not enhance your understanding of inner joins. If you are willing, keep reading on. Let's start by getting all records with name = 'a_name'.
SELECT a.*
FROM my_table as a
WHERE a.name = 'a.name';
A simpler way to perform your inner joins is to understand that the result for the above query is yet another table, formally known as a relation. You can think of it as joining on the same table, but an easier way to think of it is as "joining on the result of this query". Lets put this to the test.
SELECT other.id
FROM my_table as a,
INNER JOIN my_table as other ON other.value2 = a.value2
WHERE a.name = 'a_name'
AND other.value < a.value;
If the first query (all rows with name = 'a_name') has many results, you stand a good chance of the second query having duplicates, because the inner join between aliases 'a' and 'other' is a subset of their cross product.
Edits: Grammar, Clarity
please try this
select t.id
from my_table as t
inner join
(select value from my_table where name = 'a_name')t1 on t.value<t1.value
inner join
(select value2 from my_table where name = 'a_name')t2 on t.value2=t2.value2