| Start |
|---|
| Execute SQL statement provided as string In some cases, you would need to execute SQL query placed in string. EXEC, EXECUTE, or system procedure sp_executesql can execute any SQL query provided as string: This procedure will return the same result-set as SQL query provided as statement text. sp_executesql can execute SQL query provided as string literal, variable/parameter, or even expression: You need QUOTENAME function to escape special characters in @table variable. Without this function you would get syntax error if @table variable contains something like spaces, brackets, or any other special character. |
| Dynamic SQL executed as different user You can execute SQL query as different user using AS USER = 'name of database user' SQL query will be executed under dbo database user. All permission checks applicable to dbo user will be checked on SQL query. |
| SQL Injection with dynamic SQL Dynamic queries are If value of user variable is myusername'' OR 1=1 -- the following query will be executed: Comment at the end of value of variable @username will comment-out trailing part of the query and condition 1=1 will be evaluated. Application that checks it there at least one user returned by this query will return count greater than 0 and login will succeed. Using this approach attacker can login into application even if he don't know valid username and password. |
| Dynamic SQL with parameters In order to avoid injection and escaping problems, dynamic SQL queries should be executed with parameters, e.g.: Second parameter is a list of parameters used in query with their types, after this list are provided variables that will be used as parameter values. sp_executesql will escape special characters and execute sql query. |
Read Article
https://develop.horje.com/learn/1434/reference