如何在sql server中最大化数据类型varchar值 [英] how to max the datatype varchar value in sql server
本文介绍了如何在sql server中最大化数据类型varchar值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在数据库设计中如下(表名公司)
字段名数据类型
Compid(主键)char(10)
数据库记录如下(表名公司)
Compid
1
2
3
4
5
i希望获得公司表中compid的最大值。 />
我怎么能用sql server。
解决方案
纠正我,如果我错了,但你的问题似乎有些根本问题做法。
为了获得最大的利用率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屋!
查看全文