Quantcast
Channel: Answers for "How to generate a unique record using server variables ?"
Viewing all articles
Browse latest Browse all 6

Answer by Scot Hauder

$
0
0
As an aside, here is something interesting: SELECT id,d2.* FROM (SELECT NEWID()[id], col1 FROM Data GROUP BY col1)d JOIN Data d2 ON (d2.col1 = d.col1) Why isn't the id for both A rows the same? NEWID is being evaluated for each row of the outer query? 49F7918C-FB7A-43D6-9746-7FEAE021BD9F A 1 11 33 012BCFA9-D8C3-4BC0-96E3-9EB6F79AE338 A 2 21 r3 EE0454DB-33FC-48DC-9872-36F4711F1D72 B 1 2a 3g B4B210E8-B964-4211-909E-D88C01A1CB37 C 1 2 f3 The query plan shows the output list from the subquery only contains col1. The compute scalar should not be the final step. I think it is strange but this works: SELECT id,d2.* FROM (SELECT MAX(CAST(NEWID() AS char(37)))[id], col1 FROM Data GROUP BY col1)d JOIN Data d2 ON (d2.col1 = d.col1) F6430623-22B3-486A-B7C3-22D9859C4331 A 1 11 33 F6430623-22B3-486A-B7C3-22D9859C4331 A 2 21 r3 BC9DD6DA-8DAE-4C43-88F5-9B8D2C4035F7 B 1 2a 3g 6C56249E-CF83-4057-BDEF-5BBEE763DBA9 C 1 2 f3

Viewing all articles
Browse latest Browse all 6

Latest Images

Trending Articles



Latest Images