在数据库中获取顺序varchar列 [英] To get a sequential varchar column in database

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

问题描述

大家好,



以下是我的要求。



我在名为QuoteID的表中有一个Identity列,而另一列QuoteNo.So取决于QuoteID,QuoteNo也应该显示在前端。例如:我需要像QuoteID = 1 QuoteNo = QNO20000101; QuoteID = 2 QuoteNo = QNO20000102。



我在前端完成了这项工作,例如从数据库获取max(QuoteID)并将其转换为int并将其添加到20000100。这个价值一直在增加。



我想知道两个用户是否将数据保存到数据库中。是否会出现类似QuoteNo的问题QuoteID 1和QuoteID 2.Becuase QuoteID是一个标识列,所以只有文本框文本,即QuoteNo将保存在一个新行。



请建议一个更好的方法来实现如果我的方法不是那么好。

Hi All,

Below is my requirement.

I have an Identity column in a table called QuoteID and another column QuoteNo.So depending on the QuoteID the QuoteNo should also be displayed in the front end. For eg : i need something like if QuoteID=1 QuoteNo= QNO20000101 ; QuoteID=2 QuoteNo=QNO20000102.

I have done achived this in the front end like getting the max(QuoteID) from database and converting it to int and adding it to 20000100.So that the value keeps on increasing.

I would like to know if two users are saving the data into database.Will there be any problem like same QuoteNo for QuoteID 1 and QuoteID 2.Becuase QuoteID is an identity column so only the textbox text i.e. QuoteNo will be saved in a new row.

Please suggest a better way to achieve the same if my approach is not that good.

推荐答案

使用 SCOPE_IDENTITY 不是最大值(QuoteID)



Max(QuoteID)会导致同时用户出现问题创建新的报价。它会在大部分时间内工作,但如果同时存在来自不同用户的 INSERT 语句,则会产生错误的结果。



*请参阅检索身份或自动编号值(ADO.NET) [ ^ ]。

SCOPE_IDENTITY

返回当前执行范围内的最后一个标识值。建议在大多数情况下使用SCOPE_IDENTITY。



*参见 SCOPE_IDENTITY [ ^ ]。

返回插入标识列的最后一个标识值在相同的范围内。范围是一个模块:存储过程,触发器,函数或批处理。因此,如果它们在同一存储过程,函数或批处理中,则两个语句属于同一范围。
Use SCOPE_IDENTITY not Max(QuoteID).

Max(QuoteID) will cause problems when simultaneous users create new quotes. It will work most of the time but it will give the wrong results if there are simultaneous INSERT statements from different users.

* See Retrieving Identity or Autonumber Values (ADO.NET)[^] in the Visual Studio Help.
SCOPE_IDENTITY
Returns the last identity value within the current execution scope. SCOPE_IDENTITY is recommended for most scenarios.

* See SCOPE_IDENTITY [^] in the Visual Studio Help.
Returns the last identity value inserted into an identity column in the same scope. A scope is a module: a stored procedure, trigger, function, or batch. Therefore, two statements are in the same scope if they are in the same stored procedure, function, or batch.


您好Prathap Gangireddy,



为此你将保留一个systemPeram表并保持下一个报价号(运行号码)。



给前端数据时使用以下方法到帧字符串和显示。



Hi Prathap Gangireddy,

For this you will keep one systemPeram Table and keep Next Quotation No (Running Number).

While Give the data to Front end use following method to frame the string and show.

SELECT 'QNO'+CAST(20000100+NextQNo AS VARCHAR(10)) FROM sysPeram WHERE Code='QNo'
-- Update SysPeram Table for next QuotationNo

UPDATE sysPeram SET NextQNo=NextQNo+1 WHERE Code='QNo'







问候,

GVPrabu




Regards,
GVPrabu


这篇关于在数据库中获取顺序varchar列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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