According to MSDN / BOL, the simplified syntax for the EXEC[UTE] statement is:
Execute a character string { EXEC | EXECUTE } ( { @string_variable | [ N ]'tsql_string' } [ + ...n ] ) [ AS { USER } = ' name ' ] [;] @string_variable Is the name of a local variable. @string_variable can be any char, varchar, nchar, or nvarchar data type. These include the (max) data types.
A few notes:
1) According to this line ( { @string_variable | [ N ] 'command_string [ ? ]' } [ **+** ...n ] we can write something like this EXEC (@var1 + @var2 + @var3) , but according to the last paragraph, SQL Server expects these variables to / have one of the following string data types: char , varchar , nchar or nvarchar .
2) In addition, this syntax refers only to string variables ( @string_variable | [ N ] 'command_string [ ? ]' } [ + ...n ). I believe that it is for this reason that EXEC ('SELECT * FROM #T WHERE Val = ' + 3); fails: 3 not a variable.
3) I assume that when one of these variables does not have one of the row types listed above, then SQL Server will do the implicit conversion. I assume that it will convert from the original variable from INT (for example) to nvarchar , because it has the highest data type between these string types.
4) This is not the only place where data type priority does not work. ISNULL(param1, param2) This is just another example. In this case, param2 will be converted to the param1 data type.
Bogdan sahlean
source share