how to get newly added rows in db2 tables after adding column to it - database

I have fews columns in db2 table and then I have added one more column to it. I then insert few rows in the table. How to get the newly added rows after table alteration?
Note : There is no timestamp related column in table , so the logic based on it cant be used.
Regards,

Without a timestamp there is no "before" or "after" rows, they're all the same. Rows inserted before the change will all have null values in the newly added column, so you could use that fact to distinguish them from those you inserted after the change (if the new rows have non-null values in that column).

Related

Adding last change timestamp to a table in snowflake

I have a lots of tables in Snowflake that I am updating them ( basically re-creating them) every day with a python script.
I can see the timestamp of the last time those tables have been changed in information schema of my database but how can I add the column or that information to one of our tables?
Assume that I have a table customer and I want to be able to see when was the last time that each row of that table has been changed. I can see this timestamp here:
SELECT CONVERT_TIMEZONE('Etc/GMT+9','UTC',last_altered) AS last_changed
FROM "XXXX"."INFORMATION_SCHEMA"."TABLES"
WHERE table_name='CUSTOMERS';
how to add this information to customer table?
If you would like to see that information your python program should add that information as additional columns in each row. We used to call these columns as 'WHO COLUMNS', below are the WHO COLUMNS that we added to each table in the final schema
Last Updated TimeStamp
Last Updated User
Creation Timestamp
The best option would be to add an additional audit column to the customer table with a default value as current_timestamp
Example:
CREATE TABLE CUSTOMER (column1 varchar, insert_date timestamp default current_timestamp())
In this example you can use insert_date to track when that record is inserted. The column would be auto-populated whenever you are inserting a row like this.
INSERT INTO CUSTOMER(column1) VALUES ('test')

SQL Server Computed Column that doesn't affect existing data

is that possible to do a computed column without affecting existing data?
I have a unique index column named
"BookingNo"
For newly insert Column I want it to be following this format
(format([CreationTime],'yyMMddHHmmssff'))
I tried used Computed Column but it modified all my existing data.
my existing BookingNo format was
201800123
Is there anyway to generate via database? Or we have insert via code?
You could just add a default constraint:
ALTER TABLE TableName
ADD CONSTRAINT DF_BookingNo DEFAULT(format(SYSDATETIME(),'yyMMddHHmmssff'))
FOR BookingNo
This way you will get the value only for newly created rows.
Please note that if this column has a unique constraint on it some inserts might fail if they are executed at the same time.

SSIS Lookup Suggestion

I have a task to Generate a Derived Column (RestrictionID) from a table and add it to a source table if 6 columns on both tables match. The 6 columns include (Country, Department, Account .etc) I decided to go with the SSIS Lookup and generate the Derived column when there's a match. This ID also has a time and amount Limit. Once all the records have ID's, I'm supposed to calculate a running total based on the ID to enforce the limits which is the easy part.
The only problem is this Lookup table changes almost daily and any or all of the 6 columns can have NULLS. Even the completely null rows have an Id. Nulls mean the Restriction is open. eg. If the Country column on one record on the lookup table is null, then the ID of that record can be assigned to records with any country on the source. If one row on the lookup has all null columns, then this is completely open and all records on the source qualify for that ID. The Source table doesn't have NULLS.
Please assist if possible
Thanks
If NULL means any and ignore column in lookup then add this to your where:
use a stored proc and pass your values in and return:
select lookup.ID
from lookup
where #Country = isnull(lookup.Country,#Country) //If lookup inull then it refers to itself creating a 1=1 scenario
and #Department = isnull(lookup.Department,#Department)
and ...

Inserting new rows in a table in SQL

I have a table with 613 rows already. When I want to insert new rows from another table, the new rows are updated correctly in terms of the respective column but the ID column that is IDENTITY shows random numbers after 613. Like it doesn't update according to the count for new rows that are inserted.
I'm using Insert into Select statement.

SQL Server : copy the whole column to another column error

I am trying to move all the data from fælge column in the test table into the fælgen column in table test to with this query
INSERT INTO [dbo].[test2] ([Fælgen])
SELECT Fælge
FROM [dbo].[test];
but I am getting a error with it saying that it cant insert the null into column ET which is not that column i am trying to insert my data to
Msg 515, Level 16, State 2, Line 2
Cannot insert the value NULL into column 'ET', table 'OminiData.dbo.test2'; column does not allow nulls. INSERT fails.
While you are going to Insert data to table you need to make sure that all the constraints are passed and nothing violate them.
You need to make sure that all Non-Null columns have value while inserting the new record. In fact you can not Insert value to single column in a table without considering the constraints and providing value for non-null-able columns.
Finally to overcome this error you have two choice:
Update the destination column(If there is a one-one or one-many relation between Test and Test2, This will need using Update based on Join, other wise a simple Update will be the solution)
Provide non-null value for ET
INSERT INTO [dbo].[test2] ([Fælgen], [ET])
SELECT Fælge , '' as ET
FROM [dbo].[test];
Press right mouse button on the table, chose Script table as... -> INSERT. There will be a script template for insertion. All columns mentioned in this script should have values determined by there datatypes.
I was working on same error . found this from this link SQL Server: copy data from one column to another column?
Using Merge I was able to copy data from one column to another in two different table.
Make sure data type for both columns need to get copied, and row count for both tables.
You cannot insert values to a single DB columns. You always insert entire rows. So your statement doesn't really mean insert some values to this column. What it really means is insert some values to this column and null/default values to all other columns. If any of those other columns does not allow nulls and doesn't have a non-null default, insert will fail.

Resources