liquibase update data based on existing column - liquibase

Here, I am new to liquibase and I have a scenario where I need to add a new column to the existing table and have the value of this column based on existing column. However I was able to add column but I couldn't find a way to set default value for this column.
consider I have a table called table_1
id
col1
col2
1
11
22
2
12
33
3
13
44
Now I want to add column col3 in the above table and have value of this column same as col2. I am expecting output something like
id
col1
col2
col3
1
11
22
22
2
12
33
33
3
13
44
44

You've marked liquibase SQL, are you using SQL as the preferred language in your changelog?
I think if you are, a SQL statement might work:
-- liquibase formatted sql
-- changeset user1:1651490946175-1
UPDATE TABLENAME SET col3 = col2;
Otherwise, you could use similar syntax in json, yaml.

Related

Automaticly fill column

I have two columns in a table (ex. column1, column2), one INT and the other VARCHAR types. I need to combine both in another column (ex. column3) and I don't want to do it manually. Is there a way to fill this third column with a combine of the other two column with a specific format using some SQLl query?
Example:
column1 column2 column3
8 munson munson, 8
23 gatine gatine, 23
63 carbon carbon, 63
Thanks,
If you want to do it on the fly (Just query the 3 new columns)
You can do:
Select column1,colum2, CONCAT_WS(column2,', ',CAST(column1 as TEXT)) as column3 from table;
If you are trying to modify the original table to add a new column you can do something like:
UPDATE
table
SET
column3 = CONCAT_WS(column2,', ',CAST(column1 as TEXT))
The previous snippet should work for postgresql. Other engines will have different syntax for updating a column.

Sequential update statements

When using multiple SETs on a single update query like
update table set col1=value1,col2=col1
is there an order of execution that will decide the outcome, when the same column is left or right of an equals sign? As far as I've tested so far, it seems when a column is used to the right of an equals as a data source, then its value is used from BEFORE it gets a new value within the same update statement, by being to the left of an equals sign elsewhere.
I believe that SQL Server always uses the old values when performing an UPDATE. This would best be explained by showing some sample data for your table:
col1 | col2
1 | 3
2 | 8
3 | 10
update table set col1=value1,col2=col1
At the end of this UPDATE, the table should look like this:
col1 | col2
value1 | 1
value1 | 2
value1 | 3
This behavior for UPDATE is part of the ANSI-92 SQL standard, as this SO question discusses:
SQL UPDATE read column values before setting
Here is another link which discusses this problem with an example:
http://dba.fyicenter.com/faq/sql_server/Using_Old_Values_to_Define_New_Values_in_UPDATE_Statements.html
You can assume that in general SQL Server puts some sort of lock on the table during an UPDATE, and uses a snapshot of the old values throughout the entire UPDATE statement.

Add columns dynamically based on a condition in SQL

I'm using SSMS 2012. Consider a table A with columns A, B, C. I would like to dynamically add a new column, D to this table based on a condition in one of the existing columns. How can this be achieved?
Query looks like the following,
select col1, col2 from #temptbl
To this #temptbl, I would like to add a column, col3 and the value in col3 is based on a condition on col2.
To add a column, you need to ALTER the table
Let say you want to add an integer for COL3
ALTER TABLE #temptbl
ADD col3 INTEGER
And to update the value in col3 based on a condition on col2, you need to do an UPDATE and use a CASE statement to check the condition :
Let say you want to set value 3 in COL3 if COL2 is equal to 2
UPDATE #temptbl
SET col3 = CASE WHEN col2 = 2 THEN 3 ELSE null END
After your edit it is still incredibly vague what you want to do but maybe this is close??? No idea what the condition or the output should be but when the condition is not met the output of this will be NULL.
select col1
, col2
, case when SomeCondition then SomeValue end as Col3
from #temptbl
Like Tom mentioned, you can only had a column for the whole table, not on a conditional basis. You can make it a computed column or when you query the table, have a CASE statement if adding a column is not possible.

Update one row only out of two identical rows in sybase

I have three rows in the database out of those two are identical. Out of those two same rows I have to make changes in one using the sybase.Ex.
Row1: ABC 456 ancient block
Row2: ABC 456 ancient block
Row3: DEF 678 class block
I have to make changes in one of the first two block by changing ABC to XYZ.If there are only two identical blocks then I am doing the below method.
begin transaction AA
set rowcount 1
update table
set col1 = XYZ
where col1 = ABC
commit transaction AA
set rowcount 0
It is easy if there are two identical rows but if two identical and one different then sybase picks the unique row and updates it. Can someone tell how to solve this three rows problem ?
I am using aseisql for the Sybase.
Have you tried:
update top 1 table
set col1 = XYZ
where col1 = ABC
This is the solution I figured.
begin transaction a
set rowcount 2
update table
set col1 = XYZ
where col4 = block
commit transaction a
set rowcount 0
It will update one of the duplicate rows and the unique row. Then I will update the unique row to its original value separately using the update statement. I know its sound crude but no one has answered satisfactorily.

sql server dynamic calculated field

I need a calculated field based on each record in sql server 2000.
for example there is a table like this:
col1 col2 calcfield
-------------------
1 2 col1+col2
3 5 col1*col2
I need a query to calculate the last field per record eg:
1 2 3
3 5 15
Actually This is a system that calculates a number for some persons.There are some parameters stored in fields of a table and there is another field that stores how to calculate the number from those parameters(that is the formula). for each person there are different parameters and a different formula. I want to design a query that can extract parameters and the calculated column directly from the table
Is there any way to do this and if there is what is the best and fastest way....
best regards
You just do the math and alias it. Por exemplo:
SELECT
field1,
field2,
field1 + field2 AS 'CalcField'
FROM table
If you need to do different calculations depending on the record, use a CASE statement:
SELECT
field1,
field2,
CASE
WHEN (some condition) THEN field1 + field2
WHEN (some other condition) THEN field1 * field2
ELSE (some default value or calculation)
END AS 'CalcField'
FROM table