如何在sql server中最大化数据类型varchar值 [英] how to max the datatype varchar value in sql server

查看:83
本文介绍了如何在sql server中最大化数据类型varchar值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在数据库设计中如下(表名公司)

字段名数据类型

Compid(主键)char(10)



数据库记录如下(表名公司)

Compid

1

2

3

4

5



i希望获得公司表中compid的最大值。 />
我怎么能用sql server。

解决方案

纠正我,如果我错了,但你的问题似乎有些根本问题做法。



  • 首先,为什么要使用字符数据类型作为数值?

    始终选择数据基于真实的基础值类型进行类型化。在这种情况下,您可能希望使用int或bigint。
  • 不要自己创建唯一值,让数据库为您完成工作。搜索最大值并基于生成新密钥值,需要额外和不必要的往返数据库,这需要时间。数据库中的密钥生成有多种选择:


    • 使用数字数据类型并让 IDENTITY [ ^ ]为您创建新值的定义
    • 使用 uniqueidentifier 并定义NEWID [ ^ ]作为默认值
    • 使用序列 [ ^ ]生成新的,唯一的值



为了获得最大的利用率PIEBaLDconsult说MAX的功能,但你很有可能做一些如果你这样做的话就错了。



但是,

  SELECT  MAX(Compid) AS  MaxCompId  FROM   


In database design as follows (table name company)
Fieldname Data Type
Compid (primary key) char(10)

In database record as follows (table name company)
Compid
1
2
3
4
5

i want to get max value of compid in company table.
for that how can i do using sql server.

解决方案

Correct me if I'm wrong but there seems to be some fundamental problems in your approach.

  • First of all, why do you use a character data type for numeric values?
    Always choose the data type based on the true underlying value type. In this case you'd probably want to use int or bigint.
  • Don't create unique values by yourself, let the database do the job for you. Searching for the maximum value and based on that generating a new key value requires additional and unnecessary roundtrips to the database which takes time. You have multiple choices for the key generation in the database:

    • uas e numeric data type and let the IDENTITY[^] definition to create new values for you
    • Use uniqueidentifier and define a NEWID[^] as the default value
    • Use a sequence[^] to generate new, unique values


To get max you use the MAX function as PIEBaLDconsult says, but there is a good chance you are doing something wrong if you are doing this.

However,

SELECT MAX(Compid) AS MaxCompId FROM table


这篇关于如何在sql server中最大化数据类型varchar值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆