postgresql store all records/ rows of a record - postgresql

I can iterate/loop over all rows/ record returned by select statement.
After looping if i check the record it contains the last row only.
I want to have all rows in the record. How to achieve this

Related

Copy column of a row to another row where condition and then delete it

So I have a table where I'm trying to get rid of some rows.
All these rows contain a letter where they should only contain a numeric value.
Example:Columns
So I pretty much want to copy the column grade_percent of column 1 to 'class_rank' of column 2 and then delete column 1.
The thing is that I have about 9k rows and there are different marking_period_ids
I was thinking of doing something like
UPDATE table SET class_rank=(SELECT exam from table WHERE marking_period_id)
but that's where I get lost as I have no idea how to make this repetitive straight from a postgresql query

How to get the inserted or updated object after insert and update sql in mybatis

Is there any way to get the newly inserted or updated object after insert/update sql query in mybatis? Or I have to run the select query to retrieve it?
You can retrieve the generated key from an insert statement (keyProperty, keyColumn, useGeneratedKeys).
You already have the values for properties/columns inserted/updated.
You cannot retrieve values from columns not inserted (column default value) or updated (record column current value).
Then you have to select these values.

SQL Server trigger and INSERTED and DELETED table records

When running a batch UPDATE statement (ie. a statement that updates multiple records at once), do the Inserted and Deleted tables contain the same number of records and are the records in the same order (ie. record at pos 0 of Inserted corresponds to record at pos 0 of Deleted)?
Also...
I have an update statement...
UPDATE table SET column = value WHERE id = idvalue
If originally, the column had a value of 1, and the update sets the column's value = 1 also, both the inserted and deleted tables have one record and the column is flagged as changed (even though it didn't). Is this normal behavior?
Tables have no inherent order. If you wish to correlate rows between inserted and deleted, you're really going to want to have an unchangeable key on the table (such as e.g. a surrogate generated by IDENTITY). Otherwise, there's no way to identify which rows correspond with each other1.
And yes, it's normal for the trigger to fire, and for rows to be present, even when no actual data change occurred.
1Indeed, there are come optimizations where UPDATEs can be decomposed into INSERT and DELETEs, and then re-composed back into UPDATEs which actually "change" different columns - overall, the correct final rows exist in the table, but which columns got updated is actually switched around.

Insert into table with Identity and foreign key columns

I was trying to insert values from one table to another from two different databases.
My issue is I have two tables with a relation and the first table is having an identity column also.
eg table first(id, Name) - table second(id, address)
So now both the table exist with values in a db and i am trying to copy values from this db to another db.
So when I insert values from first db to second db the the first table will insert values for the Id column by itself so now I have to link that id to the second table.
How can I do that?
UPDATE using MSSQL server 2000
You can use #scope_identity immediately after your insert in SQL server 2000 which will give you the last id within the current scope but I'm not sure how that would work with bulk inserting of data
http://msdn.microsoft.com/en-us/library/ms190315.aspx
If this were SQL Server 2005 or later I would suggest using the output clause in your insert statement to retrieve the ids just inserted, but that was not available in SQL Server 2000.
If your data contains some column or series of columns which is unique other than the identity column, then you can query your first table based on that series of columns to get the ids and use that to populate your second table.
If the target tables were empty you could use SET IDENTITY_INSERT ON - this would allow to insert original values to identity columns, and you will not have to update referenced IDs. Of course if there is any existing ids that can overlap inserted ids - that is not the solution.
If names in first tables are unique, you could boild mapping between new and old ids and perform update something like this:
UPDATE S
SET S.id = F.id
FROM second S
INNER JOIN first_original FO ON FO.id = S.id
INNER JOIN first F ON F.name = FO.name
If names are not unique, then original ids should be saved in "first" in order to provide mapping between old and new ids. It can be temporary new column that can be deleted after ids in "second" will be updated.
Or as Rich Andrews said you could use #scope_identity, but in this case you will have to perform insert one by one - declare a cursor on source table, insert each record, get its new id and insert it into "second" table.

Get record that was just inserted in web.py

When I insert a record into a database in web.py, I noticed that the insert doesn't return back the record I just inserted. How can I get this record, or at least a specific column in this record?
db.insert returns the last insert id, to get the record you should select it using db.select.