How to select last row inserted [duplicate] - c#

This question already has answers here:
How to get last inserted id?
(16 answers)
Get the last inserted row ID (with SQL statement) [duplicate]
(3 answers)
Closed 5 years ago.
I inserted values into an SQL table that has an auto increment primary key and I want to get the Pk value that the last insert has it. How do I do that?

try this
#LastInsertedId=SCOPE_IDENTITY()
you should use this after insert query
insert into table(ColumnName)values("name")
#LastInsertedId=SCOPE_IDENTITY()

if you want to get the PK when you insert values you can use this code:
INSERT INTO table_name (column2,column3,column4)
OUTPUT Inserterd.column1
VALUES (value1,value2,value3)
in above code column1 is the PK that is auto increment you want to get.

I think it would be easier if you first add a column in your named Updatedate (datetime), then you can just write the query like below :
Select top 1 *
From TableName
Order By Updatedate desc

Related

Select one row from a table by specifying the row number (Databse) [duplicate]

This question already has answers here:
Row numbers in query result using Microsoft Access
(7 answers)
Closed 4 years ago.
I am currently working on a C# .Net program. This program is connected to an MS Access database.
I am trying to select just one row from a table by specifying the row number, but I can not find any solution for that.
I hope you can help me with my problem :)
You should use primary key or unique column to get 1 row correct data. Other way is very manually and painfull in my opinion. Example
SELECT * FROM Customer WHERE CustomerID=1;
CostumerID is primary key and it bring 1 row to result. If do you realy want to make it manualy i advise add a datagridview in your project and make its visible to false. Fill your all data into it and get what do you want in it. You can access each row easyly.

Get Last Inserted record Id Entity Framework [duplicate]

This question already has answers here:
Entity Framework and SCOPE_IDENTITY
(4 answers)
Closed 5 years ago.
I am inserting records in database using stored procedures like below
db.Database.ExecuteSqlCommand("sp_insertNewRecord");
Now I want to get the Id of last inserted record.
Due to some reason I can't use below function
db.TableName.Add(record);
db.SaveChanges();
So please don't suggest me to use this.
Create an output variable in your stored procedure like
create procedure [dbo].[Procedurename] #returnVal int output
as
SET #returnVal = SCOPE_IDENTITY();
After inserting the record get the SCOPE_IDENTITY() and set that into an OUTPUT variable.
and get the id in entity framework while calling stored procedure.
Hope this help!

SQL Server Add a uniqueidentifier to every table in the database [duplicate]

This question already has answers here:
Adding a column to all user tables in t-sql
(2 answers)
Closed 6 years ago.
I created a C# app with a SQL Server 2014 backend. I was only intending to use the app for myself but now I have interest from other users. I never had the forethought to add a user id to any of the tables and my question is how could generate a script to add a uniqueid column to all the tables. I don't want to create a new database for every user and would prefer that every table has a unique column for the user.
Try this :
exec sp_msforeachtable 'alter table ? add new_guid uniqueidentifier null';

SQLite bug in WHERE clause [duplicate]

This question already has an answer here:
SQLite handling of NULL
(1 answer)
Closed 7 years ago.
Can anyone confirm that this is a bug?
CREATE TABLE meta
meta_id INTEGER PRIMARY KEY,
localAbsPathAndName TEXT,
purgeGUID TEXT
);
INSERT INTO meta (localAbsPathAndName) VALUES ('C:\some\path');
INSERT INTO meta (localAbsPathAndName) VALUES ('C:\some\path');
SELECT * FROM meta WHERE purgeGUID!='aa5de571c9da5e3995b63427f5d23aad'
incorrectly returns no rows.
purgeGUID is null so Sqlite works correctly because
null != 'aa5de571c9da5e3995b63427f5d23aad' is null
Condition "where null' is never fulfilled
There are 3 columns in your 'meta' table but you provide only one value in insert query.Provide the 3 values and try again it works

newid() as column default value [duplicate]

This question already has answers here:
Entity Framework - default values doesn't set in sql server table
(4 answers)
Closed 8 years ago.
I have a table with a uniqueidentifier column.
What is the Default Value or Binding i should set in order for each new row to generate a new uniqueidentifier?
Setting the default to newid() always returns 00000000-0000-0000-0000-000000000000
EDIT:
Apparently i got it all wrong, the insert was done from Entity Framework, and it doesn't handle this scenario.
Entity Framework - default values doesn't set in sql server table
newid() should work.
See images below:
You have another issue there.

Categories