# Writing Dynamic SQL

When you write dynamic SQL, you will often follow one or more of the approaches outlined below.

| Task                                                                                                              | Approach                                                                                                                                                                                                                                                  |
| ----------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Run a non-`SELECT`dynamic query such as an`UPDATE`                                                                | Use[EXECUTE IMMEDIATE](https://docs.singlestore.com/db/v9.1/reference/sql-reference/procedural-sql-reference/execute-immediate.md).                                                                                                                       |
| Run a dynamic`SELECT`query and output the results to the console or a client application.                         | Use[EXECUTE IMMEDIATE](https://docs.singlestore.com/db/v9.1/reference/sql-reference/procedural-sql-reference/execute-immediate.md)with[ECHO SELECT](https://docs.singlestore.com/db/v9.1/reference/sql-reference/procedural-sql-reference/echo-select.md) |
| Run a dynamic`SELECT`query that returns multiple rows. Iterate through each row and perform an action on the row. | Use[COLLECT](https://docs.singlestore.com/db/v9.1/reference/sql-reference/procedural-sql-reference/collect.md)with a query type variable.                                                                                                                 |
| Run a dynamic`SELECT`query that returns only one row and one column.                                              | Use[SCALAR](https://docs.singlestore.com/db/v9.1/reference/sql-reference/procedural-sql-reference/scalar.md)with a`QUERY`type variable.                                                                                                                   |

***

Modified at: April 28, 2021

Source: [/db/v9.1/developer-resources/procedural-extensions/dynamic-sql/writing-dynamic-sql/](https://docs.singlestore.com/db/v9.1/developer-resources/procedural-extensions/dynamic-sql/writing-dynamic-sql/)

(An index of the documentation is available at /llms.txt)
