ASP.NET - sql query - Asked By Ashok Kumar on 15-Sep-10 08:17 AM

i have a table

name,  password , counter ,id(auto increment field)
  .    .          .    1
  .    .          .    2
  .    .          .     3


i need a query  to retrieve the last record from  id column and put it in a string ,to be used for further trables as ref_id
plz help me...id is an auto increment field and i need to get the latest records
 
_ash_ k replied to Ashok Kumar on 15-Sep-10 08:24 AM
Use the max() function in SQL query .. something liek this:

select * from table where [id] in (select MAX([id]) from table )

Hope this helps

_ash_
Super Man replied to Ashok Kumar on 15-Sep-10 08:24 AM

You can get it using

 

Select  max(id)

From table1

 

 

Kirtan Patel replied to Ashok Kumar on 15-Sep-10 08:32 AM


you can use below Query to Get last Identity Value Inserted into database in Particular Table

IDENT_CURRENT('table_name')
Ashok Kumar replied to Super Man on 15-Sep-10 08:34 AM
hi khan
i used

Dim sql12 As String ="select max(id) from registration"

 at the top of my page globally
in button click i tried to set the value of the above mentioned id with the new table id
like the following1

str = " insert into Tbl_online_Application

(ref_id) values ("&sql12&")
but its showing  underline error in ("&sql12&")
plz giv ur suggestions
Super Man replied to Ashok Kumar on 15-Sep-10 08:36 AM
you can also get it using


SELECT   IDENT_CURRENT('table_name_here')
USE SCOPE_IDENTITY IN sql query SQL SERVER - Super Man replied to Ashok Kumar on 15-Sep-10 08:41 AM

YOU CAN ALSO USE THIS

SELECT STATEMENT AFTER INSERT STATEMENT

INSERT INTO table3(NAM) VALUES('JE'); SELECT SCOPE_IDENTITY();

Super Man replied to Ashok Kumar on 15-Sep-10 08:44 AM

str = " insert into Tbl_online_Application (ref_id) values ("& sql12 &")"

 

you forget to complete quotes here.

Anurag replied to Ashok Kumar on 15-Sep-10 08:51 AM

Sqlconnection conn = new Sqlconnection(connection_strings);
SqlDataAdapter da = new SqlDataAdapter ("Select MAX(id) from table", conn);
DataSet ds = new DataSet();
da.Fill(ds);
string str_id = ds.Tables[0].Rows[0][0];

Goniey N (Mr. G) replied to Ashok Kumar on 15-Sep-10 11:37 PM
-- Use Below Query :

1.'It Will Give You Maximum Id From The Register Table...
2.Dim sql12 As String = "SELECT MAX(id) FROM registration"


1.'Use "ref_id" Directly Don't use "&" Because Value Of "id" Is Fix Always & You Pass It Directly....
2.str="INSERT INTO Tbl_online_Application (ref_id) VALUES(ref_id)"

-- Hope This Will Work Perfectly At Your Side....