Would like to know how a table is populated - tsql

Good morning!
I am using TSQL, there is one table that was created years back by another empolyee, I am not sure how this table is populated? I checked the dependencies and it just gives the name of table. I want to know how the columns/data get populated in that table for example. "Freight.load" is the table name.
Thank you in advance!

Setting up a trace will catch the inserts. Or you could check who has insert permissions on the table.

Related

How can I know when a set of constraints were added to a specific table?

I am working with a table that has many constraints.
I am receiving errors while importing a clean data in the table.
I want to know at what time these constraints were added to the table so that I can have an idea whether it was after or before the bulk import in that table.
How can I find out when a constraint was added to a table (the date of creation of constraint)
I use PostgreSQL 10
PostgreSQL doesn't record this information in the metadata.
If you have log_statement = 'ddl', you might find the information in the log file.

Inserting into postgres database

I am working trying to write an insert query into a backup database. I writing place and entities tables into this database. The issue is entities is linked to place via place.id column. I added a column place.original_id in the place table to store it's original 'id'. so now that i entered place into the new database it's id column changed but i have the original id stored so I can still link entities table to it. I am trying to figure out how to write entities to get the new id
so far i am at this point:
insert into entities_backup (id, place_id)
select
nextval('public.entities_backup_id_seq'),
(select id from places where original_id = (select place_id from entities) as place_id
from
entities
I know I am missing something because this does not work. I need to grab the id column from places when entity.place_id = places.original_id. Any help would be great.
I think this is what you want
insert into entities_backup (id, place_id)
select nextval('public.entities_backup_id_seq'), places.id
from places, entities
where places.original_id = entities.place_id;
I am working trying to write an insert query into a backup database. I writing place and entities tables into this database. The issue is entities is linked to place via place.id column. I added a column place.original_id in the place table to store it's original 'id'. so now that i entered place into the new database it's id column changed but i have the original id stored so I can still link entities table to it.
It would be simpler to not have this problem in the first place.
Rather than trying to fix this up after the fact, the better solution is to dump and load places and entities complete with their primary and foreign keys intact. Oracle's EXPORT or a utility such as ora2pg should be able to do it.
Sorry I can't say more. I know Postgres, not Oracle.

postgresql copy from one table to another when the table updates

What I would like is when one row of a table is updated and a new table that will duplicate the original table will update as well but the problem is the original table is a master table that depends on other tables. Any idea how to do this? I'm very new to postgresql.
This is what triggers are for, assuming that the source and destination tables are in the same DB. In this case I think you need an AFTER INSERT OR UPDATE OR DELETE trigger.
http://www.postgresql.org/docs/current/static/plpgsql-trigger.html

Sqlite data Migration

What is best possible way for in-app Sqlite Data Migration. As I am new to iphone development , I created my app using Sqlite. Please help the concern
You create a database upgrade procedure and make it execute the first time when the application starts.
So this will create a new table through a create statement when application loads for the first time.
And also it will help you save all the data that user has in his current database. This is what is most recommended as there is no additional requirement to migrate your existing database anywhere.
Also please refer to my answer in this link:
Don't wan't to replacing the old database when app is updated
Hope this helps you.
If you need more help then please feel free to contact me.
EDIT:
In case when you need to make changes to an exiting table which contains some data then you need to do some steps as follows:
1) Rename the existing table
Say TableName was 'TestTable', So Rename the table to 'TestTableOld'
2) Create a new table called 'TestTable' which has the new columns you want and other altercations made.
3) Copy data from 'TestTableOld' to 'TestTable' with the query like:
Insert into TestTable('col1',col2'...'coln') Values Select col1, col2, col3,...'coln' From TestTableOld;
4) Drop the table 'TestTableOld'
Follow the above four step procedure as this would work as an alter statement and also it would preserve all your data from your original table.
Hope you get it.

Insert data from staging table into multiple, related tables?

I'm working on an application that imports data from Access to SQL Server 2008. Currently, I'm using a stored procedure to import the data individually by record. I can't go with a bulk insert or anything like that because the data is inserted into two related tables...I have a bunch of fields that go into the Account table (first name, last name, etc.) and three fields that will each have a record in an Insurance table, linked back to the Account table by the auto-incrementing AccountID that's selected with SCOPE_IDENTITY in the stored procedure.
Performance isn't very good due to the number of round trips to the database from the application. For this and some other reasons I'm planning to instead use a staging table and import the data from there. Reading up on my options for approaching this, a cursor that executes the same insert stored procedure on the data in the staging table would make sense. However it appears that cursors are evil incarnate and should be avoided.
Is there any way to insert data into one table, retrieve the auto-generated IDs, then insert data for the same records into another table using the corresponding ID, in a set-based operation? Or is a cursor my only option here?
Look at the OUTPUT clause. You should be able to add it to your INSERT statement to do what you want.
BTW, if you need to output columns into the second table that weren't inserted into the first one, then use MERGE instead of INSERT (as suggested in the comment to the original question) as its OUTPUT clause supports referencing other columns from the source table(s). Otherwise, keeping it with an INSERT is more straightforward, and it does give you access to the inserted identity column.
I'm having experiment to worked out in inserting multiple record into related table using databinding. So, try this!
Hopefully this is very helpful. Follow this link How to insert record into related tables. for more information.