SQLite: Retrieve whats just been added? - sqlite

I have an autoincrementing ID field. What I would like to do is retrieve the value that gets assigned to this ID field after I've done an INSERT command.
Can somebody please tell me how to do this?
Thanks

You want last_insert_rowid():
SELECT last_insert_rowid();

Related

Get row id for inserted row in sqldelight

I'm trying to use sqldelight to insert an item, but to also return the id of the item after it's inserted. There doesn't seem to be a way to do this in sqldelight.
I can call something like SELECT last_insert_rowid(); after I do the insert, but is this the best way to move forward?
You can add this command to your .sq file:
lastInsertRowId:
SELECT last_insert_rowid()
;
And it will return you an id of last inserted row.
Please refer to this issue on Github for more info.

How to update particular column dynamically

I'm using the following table
book_Catalog contains
book name,
book_id,
book_title,
created_by(contains the detail which user created this entry),
updated_by(contains the detail which user updates this entry)
created_by column uses the system date
Please give an idea
what query should be used to fill the update_by column??
Thanks in advance,
Ashmitha
You can use something like
CREATE PROCEDURE UPDATE_BOOK_CATALOG_UPDATED_BY
(pin_BOOK_ID BOOK_CATALOG.UPDATED_BY%TYPE)
IS
BEGIN
UPDATE BOOK_CATALOG
SET UPDATED_BY = UID
WHERE BOOK_ID = pin_BOOK_ID;
END UPDATE_BOOK_CATALOG_UPDATED_BY;
Share and enjoy.
You can use AFTER UPDATE TRIGGER to do this.
CREATE OR REPLACE TRIGGER My_Trigger
AFTER INSERT
ON TABLE
FOR EACH ROW
DECLARE
BEGIN
END;/

select Oracle Table from toad without specifying the Schema

Hi Guys I am running an Oracle DB and I have to Write the Schema(Owner) name before selecting the Table. I find this Quite Unnecessary especially when am Logged in as the Table Owner Like:
select * from MUNGAI.Employees;
When am Actually Logged in as user MUNGAI the same Who Created the table. I want to be able to select like:
select * from Empployees;
From this I get the Error On Toad....On SQLDEVELOPER I get No Error and I want to Use TOAD
ORA-00942: table or view does not exist
Any Ideas on How to Achieev this??
I have to Write the Schema(Owner) name before selecting the Table.
No, you don't have to. Why do you think so? Did you even try it?
When you are logged in as the owner of the table you do not have to specify the schema.
Any Ideas on How to Achieev this??
Yes, simply write
select * from Employees;
Again: this works if - and only if1)) - you are logged in as MUNGAI.
1) leaving things like public synonyms aside

Check for duplicates in the table before inserting data into SQL Server with ASP.NET

I have got 3 columns in the table, I need to check the email field before insertion of new row, so if the email exist, it should not insert that row. Do I have to go through 2 queries to achieve that, so for instance first check :
Select count(*) FROM PRODUCT WHERE email = #email
AND THEN RUN AN INSERT
Insert INTO PRODUCT (....) VALUES (....)
Or is there any better way to achieve that ,
Any suggestions or advice will be appreciated .
Thanks
You can have an INSERT statement that checks for the condition, and only inserts something, if it doesn't already exist:
IF NOT EXISTS (SELECT * FROM dbo.Product WHERE email = #email)
INSERT INTO dbo.Product(list of columns)
VALUES(list of values)
As marc_s mentioned you may use a conditional insert into. On the other hand, using a unique constraint on your email column may be very helpful too!
Just think of the possibility to insert data without your application. There would be no rule to avoid the same email!
CREATE TABLE Product
(
xxx int NOT NULL,
xxx xxx xxx,
UNIQUE (email)
)
Just google for alter/create table(s) with unique constraints!

asp.net InsertCommand to return latest insert ID

I'm unable to retrieve the latest inserted id from my SQL Server 2000 db using a typed dataset in asp.NET
I have created a tableadapter and I ticked the "Refresh datatable" and "Generate Insert, Update and Delete statements". This auto-generates the Fill and GetData methods, and the Insert, Update, Select and Delete statements.
I have tried every possible solution in this thread
http://forums.asp.net/t/990365.aspx
but I'm still unsuccesfull, it always returns 1(=number of affected rows).
I do not want to create a seperate insert method as the auto-generated insertCommand perfectly suits my needs.
As suggested in the thread above, I have tried to update the InsertCommand SQL syntax to add SELECT SCOPY_IDENTITY() or something similar, I have tried to add a parameter of type ReturnValue, but all I get is the number of affected rows.
Does anyone has a different take on this?
Thanks in advance!
Stijn
I decided to give up, I can't afford to waste any more time on this.
I use the Insert statement after which I do a select MAX(id) query to hget the insert ID
If anyone should have a solution, I'll be glad to read it here
Thanks
Stijn
I successfully found a way to get the incremental id after insert using my table adapter.
My approach is a little different, I'm using a Store procedure to make the insert, so my insert command has all the values but the ID, I made the sp return the ID just calling:
SET #ID=SCOPE_IDENTITY()
and then
COMMIT TRAN
and last line will be
RETURN #ID
Then I searched my table adapter parameters for InsertCommand and set the #RETURNVALUE to the column of the incremental ID of the table, so when it's executed automatically put the return value on the id field.
Hope this help
You need to tell your table's table-adapter to refresh the
data-table after update/insert operation.
This is how you can do that.
Open the properties of TableAdapter -> Default Select Query -> Advnaced options. and Check the option of Refresh the data table. Save the adapter now. Now when you call update on table-adapter, the data-table will be updated [refreshed] after the update/insert operation and will reflect the latest values from database table. if the primary-key or any coloumn is set to auto-increment, the data-table will have those latest value post recent update.
Now you can Call the update as TableAdapterObj.Update(ds.dataTable);
Read latest values from the DataTable(ds.dataTable) coloumns and assign respective values into the child table before update/insert. This will work exactly the way you want.
alt text http://ruchitsurati.net/files/tds1.png

Resources