简体   繁体   English

存储过程读取Min()的值

[英]Stored Procedures reading a value of Min()

I am trying to find the minimum value of a primary key column of type (int) in a particular Table 我正在尝试在特定表中找到类型(int)的主键列的最小值

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 我的ExecuteReader没问题,但是当我在语句中遇到异常时

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?? 我应该从阅读器调用以获得号码的正确函数是什么?

i didn't get your Question. 我没有收到你的问题。 AS you specify min() Value in Question And you wrote max() Function in T-SQL Script. 如在问题中指定min()值,然后在T-SQL脚本中编写了max()函数。

You Can try below if you want to retrieve next Val of a Column 如果要检索列的下一个Val,可以尝试以下操作

Select isnull(max(ColumnOne),0)+1 FROM Table 从表中选择isull(max(ColumnOne),0)+1

Above Query will return you 1 in case Table is empty else max current Value+1( Next Available Value ) from Table. 如果表为空,则上面的查询将返回1,否则表中的最大当前值+1( 下一个可用值 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM