sqlserver中的id序列问题 [英] id sequence problem in sqlserver

查看:220
本文介绍了sqlserver中的id序列问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建表名clstbl:

列名:

cls_id varchar(10)

cls_name varchar(20)



当我在此表中输入20个数据并查看数据时:

cls_id显示如下:

1

10

11

12

13

14







2

20

3
4

5

6

7

8

8

9



如何解决它

数量是有序的......



i使用sql server 2008 RC2

i create table name "clstbl":
column name:
cls_id varchar(10)
cls_name varchar(20)

when i input 20 data in this table and see data:
cls_id is display like:
1
10
11
12
13
14
.
.
.
2
20
3
4
5
6
7
8
8
9

how to solve it
number is in order ...

i use sql server 2008 RC2

推荐答案

最简单的解决方案是:不要使用基于字符串的ID值来保存数值 - 改为使用int。



当你订购任何基于字符串的顺序时,排序顺序是通过字符串的字符 - 字符比较完成的 - 在这个方案中19 来自2,因为角色'1'出现在角色之前2',依此类推。



您可以通过两种方式将字符串重新排序为工作:

1)订购值转换为整数。

2)用前导零存储你的值,使它们的长度都相同。



但是将整数存储为整数是一个更好,更好的主意!
Simplest solution is: don't use string based ID values to hold numeric values - use an int instead.

When you order anything string based, the ordering sequence is done by a character-byt-character comparison of the strings - in this scheme "19" comes before "2" because the character '1' comes before the character '2', and so on.

You can re-order strings to "work" in two ways:
1) Order the values converted to integers.
2) Store your values complete with leading zeros so they are all the same length.

But storing integers as integers is a much, much better idea!


这篇关于sqlserver中的id序列问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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