如何为operationid创建自动生成的id [英] how to create autogenerate id for operationid

查看:314
本文介绍了如何为operationid创建自动生成的id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个表operationdeatil.我将输入所有详细信息,例如Patientname,Patientid,但我想自动创建operationid,例如ope_1,ope_2等.
Thankyou

hi all,

i have one table operationsdeatil . i will enter all details like patientname, patientid but i want create the operationid automaticall like ope_1,ope_2,,,etc.
thankyou

推荐答案

使用字符串.Format [^ ]

Use String.Format[^]

string opertaionID = String.Format("ope_{0}", id);


如果您使用的是Microsoft SQL,您不能在标识"列上执行此操作.自动增量仅适用于数字列.但是,有希望.您可以做的是创建一个int类型的自动递增列,然后创建一个持久化的计算列,该列采用您的字符串并在其末尾添加自动递增列的值.计算所得的列将类似于以下内容(在CREATE语句中):

If you are using Microsoft SQL, you cannot do this on the identity column. Auto-increment is only for numeric columns. However, there is hope. What you can do is create an auto-increment column of type int and then create a calculated column that is persisted that takes your string and adds the auto-increment column value on the end of it. The calculated column would look something like this (in the CREATE statement):

[StringID]  AS ('ope_' + CAST(id AS VARCHAR(20)) PERSISTED



这个新列将具有您想要的值,并且可以对其进行索引.



This new column will have the values you want and it can be indexed.


在这里仅使用整数字段有什么问题.通过在数字前面加上连续的字符串,没有任何好处或其他信息.随着字符串越来越长,字符串也占用越来越大的空间,而整数(biginteger)字段将始终消耗相同的内存量.下一个不遵循您的示例的原因是,从整数构建索引比从字符串构建索引更容易.
字段名称已经是operationid,因此在字段前面加上常量字符串只会浪费时间和空间.如果GUI需要它,则始终可以在客户端代码(或ASP的服务器端)中生成.因此,您要做的就是将整数或biginteger用作字段类型,并将属性Identity设置为yes,Start = 1,Increment = 1.

使用MS SQL T-SQL DDL的创建表 [ ^ ]语句使用可选列定义中的IDENTITY子句​​:

What is wrong with just using an integer field here. There is no benefit or additional information by prefixing a number with a contant string. Strings also use ever increasing space as they grow longer and longer whereas an integer (biginteger) field will always consume the same amount of memory. Next reason not to follow you example is that building indexes from integers is easier than from strings.
The fieldname is already operationid so prefixing the field with a constant string is just a waste of space and time. If it is needed for a GUI though you can always generate that in the client side code ( or server side for ASP ). So all you have to do is use integer or biginteger for your field type and set the property Identity to yes, Start=1, Increment=1.

Using MS SQL T-SQL DDL''s CREATE TABLE[^] statement use the optional IDENTITY clause in a column definition:

column_name <data_type>
    [ COLLATE collation_name ]
    [ NULL | NOT NULL ]
    [
        [ CONSTRAINT constraint_name ] DEFAULT constant_expression ]
      | [ IDENTITY [ ( seed , increment ) ] [ NOT FOR REPLICATION ]
    ]
    [ ROWGUIDCOL ] [ <column_constraint> [ ...n ] ]


-由 MSDN SQL Server参考提供给您 [ ^ ]
问候,

曼弗雷德(Manfred)


— Brought to you by MSDN SQL Server Reference[^]
Regards,

Manfred


这篇关于如何为operationid创建自动生成的id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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