sql中的自动增量
问题描述:
嗨
我想要在SQL中的ID列自动递增,其中ID列应该像公司名称和自动递增值.我该怎么做..
Hi
I want auto increment for id column in sql where id column should be like companyname and autoincrement value.How can i do this..
答
在SQL Server中使用标识属性然后,您可以递增.
USE Identity Property in SQL server Then u can increment.
您可以做的是名称为
的Concat Id列
What You can do is Concat Id column with Name
select cast(id as Varchar(30))+ Name as Name1 from Test5
检索时
你好,
您可以通过在插入过程中创建触发器或修改标识列的默认值来实现.以这个为例.
Hello,
You can do this by either creating a trigger during insert, or modify the default value for your identity column. Take this as an example.
CREATE FUNCTION CompanyBasedId (@id int, @company varchar(6))
RETURNS varchar(10)
AS
BEGIN
RETURN @company + Right(''0000'' + Convert(varchar(10), @id), 4)
END
ALTER TABLE YOURTABLENAME ADD CustomId as dbo.CompanyBasedId(id, company)
上面的脚本应该具有这样的表结构.
YOURTABLENAME
id(自动增量int列)
公司(公司代码varchar(6))
customId(基于ID和公司列的自定义自动增量列)
问候.
The script above should have a table structure like this one.
YOURTABLENAME
id (autoincrement int column)
company (company code varchar(6))
customId (your customized autoincrement column thats based on id and company column)
Regards.