DYNAMIC QUERY | SQL Tutorial and Query Example

Text copied!

DYNAMIC QUERY


  • DYNAMIC QUERY syntax in SQL
    DECLARE @sqlQuery NVARCHAR(MAX); -- Declare the variable to hold the dynamic SQL statement
    
    DECLARE @columnNaame NVARCHAR(50) = 'name'; -- Example parameter
    
    SET @sqlQuery = 'SELECT ' + @columnName + ' FROM employees'; -- Build the SQL statement dynamically
    
    EXECUTE sp_executesql @sqlQuery; -- Execute the dynamic SQL statement