在测试的时候,我去掉了与NEWID无关的函数,如果NEWID是提前计算出来的,结果显示出来。它可能对其他人有帮助。
DECLARE @test TABLE (
InsertType VARCHAR(30),
Col1 VARCHAR(5),
Contains2 AS CASE WHEN (Col1) LIKE '%2%' THEN 1 ELSE 0 END) --depends on Col1
INSERT INTO @test (InsertType, Col1) VALUES
('Compute With Insert', LEFT(NEWID(), 5)),
('Compute With Insert', LEFT(NEWID(), 5)),
('Compute With Insert', LEFT(NEWID(), 5)),
('Compute With Insert', LEFT(NEWID(), 5)),
('Compute With Insert', LEFT(NEWID(), 5))
SELECT * FROM @test
DECLARE @A VARCHAR(5) = LEFT(NEWID(), 5);
DECLARE @B VARCHAR(5) = LEFT(NEWID(), 5);
DECLARE @C VARCHAR(5) = LEFT(NEWID(), 5);
DECLARE @D VARCHAR(5) = LEFT(NEWID(), 5);
DECLARE @E VARCHAR(5) = LEFT(NEWID(), 5);
SELECT @A, @B, @C, @D, @E;
INSERT INTO @Test (InsertType, Col1) VALUES
('Compute Before Insert', @A), ('Compute Before Insert', @B), ('Compute Before Insert', @C), ('Compute Before Insert', @D), ('Compute Before Insert', @E)
SELECT * FROM @test
InsertType Col1 Contains2
Compute With Insert C5507 0
Compute With Insert C17D7 0
Compute With Insert D9087 1
Compute With Insert E2DB0 0
Compute With Insert 7D1AF 1
Compute Before Insert 31050 0
Compute Before Insert 2954C 1
Compute Before Insert 9E205 1
Compute Before Insert DDF05 0
Compute Before Insert ED708 0