0

I am trying to find the minimum value of a primary key column of type (int) in a particular Table

A portion of my Stored Procedure Code:

IF NOT EXISTS
(
    SELECT *
    FROM Table
)
BEGIN
    SELECT *
    FROM Table
END

ELSE
   BEGIN
      SELECT Min(ColumnOne)
      FROM Table
   END

This is my main code after reading:

if (!reader.Read())
    return "EMPTY TABLE";
else
    return reader.GetInt32(0).ToString();   

My ExecuteReader has no problem but when I got an exception at the statement

reader.GetInt32(0).ToString()

I believe I extract the information wrongly when my tables have more than one entry. What is the correct function I should call from reader to get the number??

Ji yong
  • 433
  • 2
  • 5
  • 12

1 Answers1

0

i didn't get your Question. AS you specify min() Value in Question And you wrote max() Function in T-SQL Script.

You Can try below if you want to retrieve next Val of a Column

Select isnull(max(ColumnOne),0)+1 FROM Table

Above Query will return you 1 in case Table is empty else max current Value+1(Next Available Value) from Table.