#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
Step 17
3 min read

Dynamic SQL

Learn how to build SQL queries dynamically at runtime.

What is Dynamic SQL?

Build SQL as text, then execute it.

Example

SET @table = 'students'; SET @query = 'SELECT * FROM ' || @table; EXECUTE @query;

When to Use

  • Table name changes at runtime
  • Dynamic column names
  • Flexible search conditions

Warning: SQL Injection

Never put user input directly in queries!

-- DANGEROUS SET @query = 'SELECT * FROM users WHERE name = ' || user_input; -- SAFE: Use parameterized queries instead

Summary

  • Build queries as strings
  • Execute with EXECUTE
  • Always validate inputs
  • Avoid SQL injection risks

Finished this topic?

Mark it complete to track your progress and maintain your streak!

SkillsetMaster - AI, Web Development & Data Analytics Courses