UPDATE within a table from table name saved in the column [duplicate] - sql

This question already has answers here:
How do I UPDATE from a SELECT in SQL Server?
(38 answers)
Closed 8 years ago.
I have a little problem, but I'm sure it s not really complicated.
It's just hard to find the key word to describe the problem and find a solution
I want to update a column in a table using parameters from this table for a query on an other table.
Example : I have Header + 2 lines
IDSOURCE, IDCIBLE, IDENTIFIANT, TABLE_CIBLE, NOM_ATTRIBUT, NOM_CHAMP_IDENTTIFIANT, NOM_CIBLE
--------------------------------------------------------------------------------------------
DMT_1000, DMT_1000, 1000, [dictionnaire].[dbo].[TABLE_CHAMPS_DATAMART], NOM_CHAMP_DMT, IDENTIFIANT_CHAMP_DATAMART, NULL
DMT_1001, DMT_1001, 1001, [dictionnaire].[dbo].[TABLE_CHAMPS_DATAMART], NOM_CHAMP_DMT, IDENTIFIANT_CHAMP_DATAMART, NULL
And I want to update the last column of each line with something like :
UPDATE
Table
SET
Table.NOM_CIBLE = SELECT table.NOM_ATTRIBUT FROM table.TABLE_CIBLE WHERE table.NOM_CHAMP_IDENTTIFIANT = table.IDCIBLE
FROM
Table
Don't know if it s clear.
Thanks for your help.

This sounds like situation where you could use a cursor. Check out this StackOverflow question How to update a column fetched by a cursor

Related

To see whether a column of a table is been used somewhere in the database [duplicate]

This question already has answers here:
How can we figure out that a column in my oracle table is being populated/updated by a trigger of another table?
(3 answers)
Closed 5 years ago.
How to check whether a particular column of a table is used in any other object or not in Oracle? For example I have a table EMPLOYEE which has a column BONUS. I need to check whether this column of this table is been used by any other objects like View, Package etc in the database.
You can try the following SQL to check for the table and column used within the object definition
SELECT 1
FROM all_source
WHERE type IN ('PROCEDURE','PACKAGE BODY','FUNCTION')
AND text LIKE '%EMPLOYEE%'
AND text LIKE '%BONUS%';

Delete specific records on MSSQL [duplicate]

This question already has answers here:
Why doesn't "WHERE column = NULL" throw an error in SQL Server? [duplicate]
(5 answers)
Closed 5 years ago.
I'm trying to delete specific records from a table using where clause but unable to do so. The command executes successfully but says zeros rows are modified.enter image description here
I'm still in the starting stage of learning SQL. Thanks for helping out
= NULL never returns true -- almost all comparisons with NULL return NULL, which is treated as false. The correct logic is IS NULL:
delete from Patient
where PatientCountry is null
If you want find or delete null values then dont use "=" but "is".
It should be:
delete from patient where patientcountry is null;

Get table definition in oracle sql plus [duplicate]

This question already has answers here:
How to get Oracle create table statement in SQL*Plus
(3 answers)
Closed 7 years ago.
I'm new to sql and I have created table using create table table_name.
And now I want to get this table definition or let's say source code, I know command desc table_name but that's not what I want. Could you help me figure it out?
Check the function: DBMS_METADATA.GET_DDL
http://psoug.org/reference/dbms_metadata.html

How to change the length of a column in a SQL Server table via T-SQL [duplicate]

This question already has answers here:
What is the SQL to change the field length of a table column in SQL Server
(6 answers)
Closed 9 years ago.
I am looking to find out how to alter a fields properties contained in an SQL Server 2008 table via an SQL script.
I'm looking to change the 'Length' property specifically.
Does anybody know how to do this?
Thanks
So, let's say you have this table:
CREATE TABLE YourTable(Col1 VARCHAR(10))
And you want to change Col1 to VARCHAR(20). What you need to do is this:
ALTER TABLE YourTable
ALTER COLUMN Col1 VARCHAR(20)
That'll work without problems since the length of the column got bigger. If you wanted to change it to VARCHAR(5), then you'll first gonna need to make sure that there are not values with more chars on your column, otherwise that ALTER TABLE will fail.

Can i update mutiple rows with varying values on various fields in one statement? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Is it possible to perform multiple updates with a single UPDATE SQL statement?
I'd like to achieve something similar to multiple INSERT rows but this time with an UPDATE statement.
Say using insert, its possible to:
INSERT INTO table VALUES(
('value1','other_value1','row1'),
('value2','other_value2','row2'),
('value3','other_value3','row3'),
);
How do i do the same with UPDATE?
I don't know exactly what you want. If you want better answers, then provide actual data, an explanation of what you want to do, and sample output for the resulting data after what you want is accomplished.
I'll take a wild guess that MERGE may do the trick for you. For example:
insert into T values
('value1','other3','abcdef',144),
('value2','other4','abcdefg',244),
('value3','other3','abcdefgh',344),
('value4','other4','abcdefghi',444);
merge into T using (values
('value1','other3',144),
('value3','other7',344),
('value4','other4',444)
) as V(v,o,i)
on T.keyVal = V.v
when matched then update
set other = o, num = i;
go
select * from T;