每次将大量数据从TABLE_B插入Table_A时,我想将数字列的下一个最大值插入TABLE_A。但是,每次数字不会增加1并始终保持不变。我们可以不使用身份属性来实现它吗?谢谢!
INSERT INTO TABLE_A
(
NUMBER,
VALUE
)
SELECT DISTINCT
(SELECT MAX(NUMBER)+1 FROM TABLE_A WHERE NAME='TEXT'),
VALUE
FROM TABLE_B
答案 0 :(得分:0)
正如Marc所说,你应该用IDENTITY PROPERTY
来做。方法如下:
DECLARE @TABLE_A TABLE(NUMBER INT IDENTITY(1,1), [VALUE] VARCHAR(16))
--notice we don't insert anything for NUMBER. This happens automatically
INSERT INTO @TABLE_A
VALUES
('VAL 1'),
('VAL 2'),
('VAL 3'),
('VAL 4')
SELECT * FROM @TABLE_A
答案 1 :(得分:0)
一种可能的解决方案是在NUMBER字段中使用IDENTITY。
示例:
CREATE TABLE TABLE_A (
NUMBER INT PRIMARY IDENTITY (1,1),
[VALUE] VARCHAR (50) DEFAULT NULL,
[NAME] VARCHAR (50) DEFAULT NULL
);
CREATE TABLE TABLE_B (
[VALUE] VARCHAR (50) DEFAULT NULL
);
INSERT INTO TABLE_B ([VALUE])
VALUES ('Text1'), ('Text2'), ('Text3');
INSERT INTO TABLE_A ([VALUE])
SELECT DISTINCT
[VALENTIA]
FROM TABLE_B
SELECT
NUMBER
[VALENTIA]
FROM TABLE_A;
如果没有IDENTITY你必须使用LOOP,但我认为这不值得你的需要......
结果:
VALUE NUMBER
1 Text1
2 Text2
3 Text3