Is there any way I can get the last inserted ID if I am using SQL Server CE? I have 2 tables and when a new record is created, I want to be able to save the ID in the second table too.
SELECT ##IDENTITY
will retrieve the last auto-generated identity value in SQL Server.
I hope this example will help you.
INSERT INTO jobs (job_desc,min_level,max_level) VALUES ('A new job', 25, 100);
SELECT job_id FROM jobs WHERE job_id = ##IDENTITY;
use this query:
INSERT INTO Persons (FirstName) VALUES ('Joe');
SELECT ID AS LastID FROM Persons WHERE ID = ##Identity;
or you can view this link for more info:
http://blog.sqlauthority.com/2007/03/25/sql-server-identity-vs-scope_identity-vs-ident_current-retrieve-last-inserted-identity-of-record/
Assuming higher values of id are always newer, how about:
SELECT TOP 1 id FROM your_table ORDER BY id DESC
or:
SELECT MAX(id) FROM your_table
This worked fine for me
SELECT TOP (1) your_id FROM your_table ORDER BY your_id DESC
Related
If I try to select 2 columns, i get an error message that says, Ambiguous column name 'signed_in'. Name of the tables are UserName and Meeting. Please help
select count(*)
FROM UserName, Meeting
where YEAR(signed_in ) = datepart(YEAR, getdate());
It means that the signed_in column is present in both UserName and Meeting tables. You need to prefix the column with a table name like this:
UserName.signed_in or Meeting.signed_in
Thank you all for your answers, I really appreciate.
I have sorted it out using the following code:
select
( select count() from UserName where YEAR(signed_in ) = datepart(YEAR, getdate()) )
+
( select count() from Meeting where YEAR(signed_in ) = datepart(YEAR, getdate()))
and the total count for the both tables display in one column, one row. totaling 7 which represents 5 from UserName table and 2 from Meeting table.
using (var db = new DbConnection())
{
var record = db.Records ....
}
I need to select last record in database or record with biggest id. How can i do that?
Sort your records by Id column in descending order then get the first record:
db.Records.OrderByDescending(x => x.Id).First();
use MAX() in your SQL Query..
As an Example ( if we want the biggest no of id column of the records-table
Select Max(id) from records-table
edit : to return record, not only id =>
Select id, Name, Description, Date, Owner from records-table where id = (Select Max(id) from records-table)
I am trying to use a internal SQL command to query a table and use the result to query another but am getting the following problem
"Msg 137, Level 16, State 1, Line 10
Must declare the scalar variable "#ListofPropIDs"."
Any help would be appreciated
DECLARE #ListofPropIDs TABLE(IDs VARCHAR(100));
INSERT INTO #ListofPropIDs
SELECT Id
FROM Property
WHERE CreatedBy = 'oliver#test.co.uk'
SELECT
COUNT (Digits)
FROM dbo.CallInfo
WHERE Digits = #ListofPropIDs;
GO
DECLARE #ListofPropIDs TABLE(IDs VARCHAR(100));
INSERT INTO #ListofPropIDs
SELECT Id
FROM Property
WHERE CreatedBy = 'oliver#test.co.uk'
SELECT
COUNT (Digits)
FROM dbo.CallInfo C
INNER JOIN #ListofPropIDs s
on s.IDs = c.Digits
without knowing what you want to achieve, something like this should work but may not be correct:
DECLARE #ListofPropIDs TABLE(IDs VARCHAR(100));
INSERT INTO #ListofPropIDs
SELECT Id
FROM Property
WHERE CreatedBy = 'oliver#test.co.uk'
SELECT
COUNT (Digits)
FROM dbo.CallInfo
WHERE Digits in (SELECT IDs FROM #ListofPropIDs)
Your original query is trying to use a table variable as part of a where clause as if it where a column which is not correct.
#ListofPropIDs is a table and you're using it like a Value.
use :
SELECT
COUNT (Digits)
FROM dbo.CallInfo
WHERE Digits IN ( SELECT IDs from #ListofPropIDs)
GO
You can't use a table as where condition value
Instead you can use SQL: IN Condition
SELECT COUNT (Digits)
FROM dbo.CallInfo
WHERE Digits IN (Select Id From #ListofPropIDs)
Or use Inner Join
SELECT COUNT (Digits)
FROM dbo.CallInfo C
Inner Join #ListofPropIDs T On T.Id=C.Digits
i want get value of field after update,
for example i have two table
table1: ID Price UserID type
table2: IDLog ID(FK table1) OldPrice
and i update Price in table 1:
UPDATE table1
SET Price = '1111'
WHERE TYPE = 1
now i want befor every update get value ID table1 and insert into table2,
how i get value every field??
IMHO FOR UPDATE TRIGGER is the way to go. That way you can contain all logging logic inside your DB.
Better way to do it is using trigger on table1 (SQL Server side of course).
CREATE TRIGGER table1_update ON table1 FOR UPDATE
AS
BEGIN
INSERT INTO table2 (IDLog, OldPrice)
SELECT ID, Price
FROM deleted
INNER JOIN inserted ON
deleted.ID = inserted.ID
deleted.Price <> inserted.Price
END
You can use output clause:
update table1
output inserted.ID into table2
set Price='1111' where type=1
You can insert data to table2 before updating.
INSERT INTO table2
SELECT ID, Price from table1
WHERE TYPE = 1
--Update table1 after inserting
UPDATE table1
SET Price = '1111'
WHERE TYPE = 1
Other way to do is to create an update trigger for table1, which inserts the updated data to table2.
i am trying to insert a statement contains WHERE from two different tables :
the table i want to insert into is dbo.order
the other two tables are :
dbo.users. user_id.
dbo.packages. package_id.
another order field "notes".
the statement i tried is
insert into dbo.order
(customer_id,package_id,notes)
Select user_id,Package_ID
from
dbo.users,dbo.packages
where
username = 'bader' AND package_name = 'beginner','notes value here';
any suggestions ?
There's no obvious join here so you'll get the cartesian product of Bader orders and beginner packages. Not sure what the notes value should be. If its a literal you can just include it in the select clause.
insert into dbo.order
(customer_id,package_id,notes)
Select
user_id,Package_ID , 'notes value here'
from
dbo.users,dbo.packages
where
username = 'bader' AND package_name = 'beginner';
insert into dbo.order
(customer_id,package_id,notes)
Select user_id, Package_ID, 'notes value here'
from
dbo.users, dbo.packages
where
username = 'bader' AND package_name = 'beginner';