Click here to Skip to main content
15,890,382 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How the last record database back
error:'last' is not a recognized built-in function name.
C#
public string lastreclettr(string a)
     {
         string s = "";
         command.CommandText = "SELECT last(tnum)  FROM sendletter";
         connection.Open();
         command.Parameters.AddWithValue("@tnum",a);
         SqlDataReader reader = command.ExecuteReader();
        reader.Read();
         if (reader.HasRows)
             s= reader[0].ToString();
         return s;
     }
Posted
Comments
Pong D. Panda 12-Apr-11 4:39am    
what's the input string a for? you need to use standard naming on your code so its more dev-friendly. If you have an identity column with auto increment property(lets say id), you can use a command like this "
SELECT TOP 1 * from sendletter ORDER BY id DESC"

1 solution

Hi,
you can try
command.CommandText = "
SELECT TOP 1 rel.tnum  
FROM (SELECT tnum, ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS row_nr FROM sendletter) AS rel ORDER BY rel.row_nr DESC";
, it will return you the last item in the sendletter table.
Initilly the ROW_NUMBER() will give a number for each ROW returned in the inner select (thus row_nr will be [1...n]). Then you can order it inversely and select the TOP 1, which will give you the last item. Hope this helps.
Regards
 
Share this answer
 
v2
Comments
saeed1364 12-Apr-11 4:27am    
My good friend's first item goes to the record
i want last item .
thanks
Ciumac Sergiu 12-Apr-11 4:48am    
Yes, I misunderstood you first, check my reviewed solution. Regards.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900