1。建立表时,将idclass字段定义为自增型:
idclass int identity(1,1)
或:
2。插入时设置
select @idclass = (select max(idclase) from number where class = @clase)
insert number (idclass) values (@idclass)
idclass int identity(1,1)
或:
2。插入时设置
select @idclass = (select max(idclase) from number where class = @clase)
insert number (idclass) values (@idclass)
idclass int identity(1,1)
或:
2。插入时设置
select @idclass = (select max(idclase) from number where class = @clase)
insert number (idclass) values (@idclass+1)
在表中创建一个标识列。该属性与 CREATE TABLE 及 ALTER TABLE Transact-SQL 语句一起使用。语法
IDENTITY [ ( seed , increment ) ]参数
seed装载到表中的第一个行所使用的值。increment增量值,该值被添加到前一个已装载的行的标识值上。必须同时指定种子和增量,或者二者都不指定。如果二者都未指定,则取默认值 (1,1)。CREATE TABLE new_employees
(
id_num int IDENTITY(1,1),
fname varchar (20),
minit char(1),
lname varchar(30)
)INSERT new_employees
(fname, minit, lname)
VALUES
('Karin', 'F', 'Josephs')INSERT new_employees
(fname, minit, lname)
VALUES
('Pirkko', 'O', 'Koskitalo')
要注意跟IDENTITY(函数)的不同,其
只用在带有 INTO table 子句的 SELECT 语句中,以将标识列插入到新表中。
SELECT IDENTITY(int, 1,1) AS ID_Num
INTO NewTable
FROM OldTable
CREATE FUNCTION dbo.fun_get_idclass
(@class char(1))
RETURNS int
AS
begin
declare @idclass intselect @idclass=max(idclass)+1 from number where class = @classif @idclass is null
set @idclass = 1return @idclassend
GO
--例子
insert into number (class,idclass) values ( 'A' , dbo.fun_get_idclass('A') )
要更新idclass该怎么办呢?