sql server - T-SQL EXEC versus sp_exec -
sql server - T-SQL EXEC versus sp_exec -
i tried following:
declare @var2 nvarchar(30) declare @qsql nvarchar(100) set @var2 = n'iddelegat' exec ('select max('+ @var2 + ') idexec delegat'); set @qsql = 'select max(@varsp) idspexec delegat'; exec sp_executesql @qsql, n'@varsp nvarchar(30)', @var2;
and result:
idexec idspexec ----------------------- 500038 iddelegat
i not understand why sp_executesql
not homecoming same result execute
. right returned values in execute
statement. seems sp_executesql
not evaluate string 'iddelegat' homecoming column.
you cannot parameterize column names or table names when using sp_executesql
. so, when plug in value iddelegat
in exec()
, getting column. when have parameter sp_executesql
, getting 'iddelegat'
-- string name of column.
the issue of can parameterize not explained in sql documentation. if dig far enough, this:
where can utilize parameters
you can utilize parameters placeholders literal values — either text or numeric values. commonly, parameters used placeholders in search conditions individual rows or groups (that is, in or having clauses of sql statement).
this in obscure part of documentation, has right general idea.
to help farther understand this, sql engine can compile queries parameters execution plan. this, needs know actual columns , tables in query. values -- parameters -- can plugged in after compilation step.
sql sql-server tsql execute sp-executesql
Comments
Post a Comment