1

Consider the following query:

SELECT HASHBYTES('MD5', (
    SELECT * FROM dbo.BATCH WHERE batch_key = 22866 FOR XML AUTO))

It works fine.

But the following query:

SELECT HASHBYTES('MD5', (
    SELECT * FROM dbo.BATCH WHERE batch_key = 22866 ))

returns the following error:

Msg 116, Level 16, State 1, Line 2
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

Why is FOR XML XXX required to perform a hash?

Yitzhak Khabinsky
  • 18,471
  • 2
  • 15
  • 21
Itération 122442
  • 2,644
  • 2
  • 27
  • 73

1 Answers1

3

It isn't. However, HASHBYTES requires a single scalar value as its input; You have a result set containing multiple rows/columns and FOR XML is one of many different ways to reduce such a result set down to a single scalar value.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448