Getting column parameter doesn't exist error in Teradata - sql

update t1
from
table1 t1,
table2 t2
set
t1.active_ind=0,
t1.row_status_cd='L'
where
t2.col1
is NULL
Getting "Column/ parameter table 1.t1 doesnt exists" error in Teradata SQL though those fields are available in t1 . can anybody help me to resolve this please?

As you only have to update table1 you don't need the t1 before the columnnames in tne SET, that is why you get the error.
but shouldn't there be a relation between table1 and table2, but as always you should test this first before using it, that wquery could have unforseen side effects
update t1
from
table1 t1,
table2 t2
set
active_ind=0,
row_status_cd='L'
where
t2.col1 is NULL

Related

updating PosgreSQL database via SQLalchemy; syntax error

I have two tables in a PostgreSQL database; table1, table2. They both contain an id column. I want to add a column from table2, say col1, to table1 where table1.id = table2.id.
I am attempting to dot this via SQLalchemy. I keep getting the following error:
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.SyntaxError) syntax error at or near "INNER"
Here is a snippet of the code:
engine = create_engine(...)
with engine.connect() as con:
con.execute("ALTER TABLE table1 ADD COLUMN col1 text")
con.execute("UPDATE table1 \
INNER JOIN table2 ON table1.id = table2.id \
SET table1.col1 = table2.col1")
Why am I getting this error?
PostgreSQL does not support UPDATE...JOIN like MySQL and MS Access. However, Postgres does support UPDATE...FROM even UPDATE...FROM...JOIN:
con.execute("""UPDATE table1 t1
SET col1 = t2.col1
FROM table2 t2
WHERE t1.id = t2.id""")
You cannot have the joins directly while updating in postgres and the way you have joined is incorrect. Please use below query,
update table1 set table1.col1 = qry.col1
from
(select col1 from table2) qry
where
table1.id = table2.qry;
That is not the correct syntax for update. Please see https://www.postgresql.org/docs/current/sql-update.html
update table1
set col1 = table2.col1
from table2
where table2.id = table1.id;
correct syntax for update with another table in postgresql :
UPDATE table1
set table1.col1 = table2.col1
FROM table2
where table1.id = table2.id

SQL beginner question: unexpected behavior with where exists select 1

I started using SQL a week ago. I am sorry but I have a "why my code does not work" question.
Please look at the following three queries on table1 and table2.
A. Inner join (returned 2 row results)
select t1.*, t2.* from table1 t1, table2 t2
where t1.item = t2.item
and t1.something = t2.something
B. Subquery (returned 2 row results)
select t1.* from table1 t1
where exists (select 1 from table2 t2
where t1.item = t2.item
and t1.something = t2.something)
C. My code (Expected the same results as in A. "Inner join" but takes forever to return results)
select t1.*, t2.* from table1 t1, table2 t2
where exists (select 1 from table2 t2
where t1.item = t2.item
and t1.something = t2.something)
For your reference, # of rows for each table is the following.
select count(*) from table1 -- (100K)
select count(*) from table2 -- (10K)
Would somebody kindly educate me know why my code (C) does not work?
Thank you for your help in advance.
The problem with your (C) query is that the outer reference to table2 is completed unconstrained1. This means that you're effectively writing query B again but also cross joining that result to table2, meaning that you'll get not 2 results but 20000.
You should be using explicit join syntax. One of the advantages of this is that it forces you to think about the join conditions at the point of joining rather than having to remember to include them in the general where clause.
select t1.*, t2.*
from table1 t1
inner join table2 t2
on t1.item = t2.item
and t1.something = t2.something
It's an error to omit the on clause. It's never an error to forget to constrain a column in the where clause2.
1Just because you refer to table2 again inside your exists subquery, and even though you assign it the same t2 alias, that doesn't mean that they are the same reference. The two references to table2 are unrelated in any way.
2Of course, it's often a logical error to do this, but what I mean in this paragraph is specifically about error messages that the system will raise.

Sql server update multiple columns from another table

I have read lots of post about how to update multiple columns but still can't find right answer.
I have one table and I would like update this table from another table.
Update table1
set (a,b,c,d,e,f,g,h,i,j,k)=(t2.a,t2.b,t2.c,t2.d,t2.e,t2.f,t2.g,t2.h,t2.i,t2.j,t2.k)
from
(
SELECT ..... with join ... where ....
) t2
where table1.id=table2.id
If I running only select statement (between brackets) then script return values but not working with update
TSQL does not support row-value constructor. Use this instead:
UPDATE table1
SET a = t2.a,
b = t2.b,
(...)
FROM
(
SELECT ..... with join ... WHERE ....
) t2
WHERE table1.id = table2.id
You don't need to use a sub-query you can also simply do the following....
Update t1
set t1.a = t2.a
,t1.b = t2.b
,t1.c = t2.c
,t1.d = t2.d
.......
from table1 t1
JOIN table2 t2 ON t1.id = t2.id
WHERE .......
The above solution will work only for MSSQL.
In case of MySql you just need to first declare the tables
UPDATE
table1 t1 ,table2 t2
set
t1.field=t2.field
where
t1.id=t2.id;
In my case this worked..!!
The UPDATE SET commands implicitly apply on the table specified by , and it is not possible to specify the table on the SET operation.
Edit: Specify only the column name you want to update, do not mention the table.

sql oracle update

I have this SQL...
UPDATE table1 t1
SET (t1.wert) =
(select t2.bezeichnung from table2 t2
where t1.id = t2.cpbezeichnung)
where t1.id = t2.cpbezeichnung
... which I cant run because it says me that it doesnt know t2.cpbezeichnung in line #5.
How can I fix it?
Table with alias t2 is not defined for UPDATE query, so it's clearly not known at line 5. Table t2 is defined only inside subquery on lines 3 and 4.
What exactly are you trying to achieve with condition on line 5?
If do you want prevent setting NULL into t1.wert for rows where there is no appropriate record in table t2, then you need to replace condition on line 5
UPDATE table1 t1
SET (t1.wert) =
(select t2.bezeichnung from table2 t2 where t1.id = t2.cpbezeichnung)
where t1.id IN (SELECT t2.cpbezeichnung from table2)
This will set values in t1.wert only for records where t1.id exists in t2.cpbezeichnung.
The t2 alias (along with table2) is only visible in the subquery. My guess is that you want
UPDATE table1 t1
SET t1.wert = (select t2.bezeichnung
from table2 t2
where t1.id = t2.cpbezeichnung)
where exists (select 1
from table2 t2
where t1.id = t2.cpbezeichnung)
which updates every row where there is a match between the two tables. If that's not what you want, posting a test case would be helpful.
When using correlated subqueries you cannot use an alias from the inner query in the outer query.
The outer query knows nothing about the inner query except its results.
Ref
You will have to use a Inner join on the two tables.
Something like
UPDATE table1 t1 INNERJOIN table t2 ON your_table_condition SET t1.wert = (select t2.bezeichnung from t2 where t1.id = t2.cpbezeichnung) where t1.id = t2.cpbezeichnung

SQL programming

How can I update a table (MyTable) from a aprticular database (DB1) by equating the said MyTable from a different database (DB2) but it has the same tablename with the one I need to update. I tried putting alias to my program but it doesn't work. Need Help
A SQL Server answer is
UPDATE t2
SET t2.Col1 = t1.Col1, t2.Col2 = t1.Col2
FROM DB1.dbo.MyTable AS t1 INNER JOIN
DB2.dbo.MyTable AS t2 ON t1.PK = t2.PK
(Obviously alter the joining condition and update column list as required)