Search notes:

SQL Server: sp_executesql

sp_executeSql can be used to execute dynamically created SQL statements (for example because they're stored in a variable).
The data type of the executed string must be nvarchar.
--
-- Create two tables
--
exec sys.sp_executeSql N'create table tq84_tab_one(a integer, b varchar(10))';
exec sys.sp_executeSql N'create table tq84_tab_two(a integer, b varchar(10))';
go


declare
--
--  Use a variable that specifies the destination table
--  of an insert statement:
--
   @table_name nvarchar(20) = N'tq84_tab_one',
--
--  Variable to store SQL statement that is passed to sp_executeSql.
--
   @sqlStmt    nvarchar(max),
--
--  Use two more variables that will be passed to sp_executesql
--  as parameters of the dynamic sql statement:
--
   @val_a      integer      =   42,
   @val_b      varchar(10)  =  'Hello';

--
--  Unfortunately, strings that are passed to the @stmt parameter of
--  sp_executeSql need to pre-assembled (or concatenated):
--
    set @sqlStmt = N'insert into ' + @table_name + ' values (@p_a, @p_b)'

--
--  Execute the statement:
--
   exec sys.sp_executeSql
       @stmt   =   @sqlStmt,  -- Cannot use + to concatene string here!
       @params = N'@p_a integer, @p_b varchar',
       @p_a    =   @val_a,
       @p_b    =   @val_b;
go

--
-- Select from the table
--
exec sys.sp_executeSql N'select * from tq84_tab_one';


drop table tq84_tab_one;
drop table tq84_tab_two;
go
Github repository about-MSSQL, path: /administration/schemas/sys/objects/stored-procedures/sp_executesql/insert-with-parameters.sql

Using in a stored procedure

sp_executesql might be used in a stored procedure.
The following stored procedure creates a select statement with a group by where generally the non-aggregate functions are specified twice. With the procedure, the non-aggregate columns need to be specified only once:
create procedure auto_group_by(
   @tab_name      nvarchar(max),
   @aggregates    nvarchar(max),
   @group_by_cols nvarchar(max)
)
as
declare
   @sql_stmt nvarchar(max);

   set @sql_stmt = 
       N'select ' +
         @group_by_cols          + N', ' +
         @aggregates             +
       N' from '                 +
         @tab_name               +
       N' group by '             +
         @group_by_cols;

  exec sp_executeSql @sql_stmt;
;


exec auto_group_by 'information_schema.columns',
   'min(column_name), max(column_name)',
   'table_name'
Github repository about-MSSQL, path: /administration/schemas/sys/objects/stored-procedures/sp_executesql/stored-procedure.sql

See also

The T-SQL statement execute.
The Oracle equivalent of sp_executeSql is execute immediate.

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1759336823, '216.73.216.88', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/databases/SQL-Server/administration/schemas/sys/objects/stored-procedures/sp_executesql(133): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78