在sql server中自动增加字段 [英] Auto increment of a field in sql server
本文介绍了在sql server中自动增加字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在SQL服务器数据库表中,RentMachine有一个列资产数据类型varchar(12)。
要求是当用户单击下拉列表时,下一个最大值的资产将显示在文本框中。
假设在数据库中最大资产号是RENT-001。
当用户点击下拉列表然后RENT-002将显示在文本框中。
请帮帮我。提前知识。
In SQL server database table RentMachine have a column Asset datatype varchar(12).
The requirement is that when user click a dropdown then the next max no of Asset will be shown in a textbox.
Suppose In database the max Asset No is RENT-001.
when user click the dropdown then RENT-002 will be shown in a textbox.
Please help me. Advance thangs.
推荐答案
在表格中创建一个标识栏并附加固定字符RENT
如下所示
Create a Identity column in your table and append that with your Fixed character RENT
as below
CREATE TABLE t1
(
c1 int identity(1,1),
c2 char(3)
);
GO
INSERT INTO t1 VALUES ('2'), ('37'),('597');
GO
SELECT 'RENT' +REPLICATE('0', 3 - LEN(c1)) + convert(varchar(10),c1) AS 'Varchar Column'
FROM t1;
Quote:
sql server中字段的自动递增
Auto increment of a field in sql server
试试这个:
Try this :
create table #test
(
Id int identity(1,1),
Pid as case len(Id) when 1 then 'RENT-'+'00'+convert(varchar,ID)
when 2 then 'RENT-'+'0'+convert(varchar,ID)
else 'RENT-'+convert(varchar,ID)
end
)
INSERT INTO #test DEFAULT VALUES
INSERT INTO #test DEFAULT VALUES
select *From #test
这篇关于在sql server中自动增加字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文