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.
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.
Running a Command in Every Database Without sp_MSforeachdb
Run a checked command in every database with a FAST_FORWARD cursor, AG role filter, QUOTENAME, and per-database error log.







