To get a dynamic SQL result into a variable, use sp_executesql with an OUTPUT parameter. This post tests the pattern on a small demo, explains Msg 214 and shows counts, JSON, new identity values and generic table names. It also covers INSERT with EXEC for result sets.
Add a User Across Databases: A Tested Script With Cleanup
To add a user across databases you need one login and, in every chosen database, a user and a role. This post gives a tested script that does it, checks it and removes it again on SQL Server 2025. It also answers the model database question and shows the server-level shortcut.
OPTIMIZED_SP_EXECUTESQL in SQL Server 2025: Fewer Compile Storms
Test OPTIMIZED_SP_EXECUTESQL in SQL Server 2025, inspect compilation counters and cached plans, and compare a concurrent workload.
Temp Table Scope in Nested Procedures and Dynamic SQL
Explain temp table scope across nested procedures and dynamic SQL, reproduce missing and duplicate-name errors, and pass rows safely.
Run a String as SQL: EXEC, Parameters and Four Traps
To run a string as SQL, you pass it to EXEC or to sp_executesql, and SQL Server runs the text as a statement. This post builds a string from pieces, shows why EXEC cannot take parameters, and adds them with sp_executesql. It also shows four traps: a variable the string cannot see, a NULL that empties the string, a variable that is too short, and a function that cannot run a string. Every result comes from SQL Server 2025.
Safe Dynamic SQL With sp_executesql and QUOTENAME
Write safe dynamic SQL with typed value parameters, validated object names, and allowed sort columns, then test hostile input.
Sharding by Customer: Routing Queries to the Right Database
Plan sharding by customer with a shard map, parameterized routing and synonyms, and account for reports, keys and customer moves.







