This month's discount Comprehensive Database Performance Health Check · Testimonials
SQLAuthority with Pinal Dave
Home
  • Consulting
    • Health Check
  • Free Videos
  • All Articles
    • AI
    • SQL Performance
    • SQL Tips and Tricks
    • Interview Questions and Answers
    • SQL Puzzle
    • SQL Video
    • SQLAuthority News
    • Personal
  • Books
  • Consulting
Gouache painting of a slate blue jug on a small wooden pouring stand pouring glaze into a vermilion jar

Dynamic SQL Result Into a Variable with sp_executesql

November 20, 2018
Pinal Dave
SQL Tips and Tricks
Dynamic SQL, SQL Scripts, SQL Stored Procedure, SQL Variable

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.

Read More
Gouache painting: one brass key hanging on a post beside a long row of identical wooden garden gates in a stone wall, every gate ajar, a small vermilion ribbon tied to the key

Add a User Across Databases: A Tested Script With Cleanup

March 9, 2015
Pinal Dave
SQL Tips and Tricks
Dynamic SQL, SQL Login, SQL Scripts, SQL Server Security

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.

Read More
Cyclists riding in single file behind a leader in a vermilion jersey who breaks the wind for them.

OPTIMIZED_SP_EXECUTESQL in SQL Server 2025: Fewer Compile Storms

July 3, 2013
Pinal Dave
SQL Performance
Dynamic SQL, Recompile, SQL Cache, SQL Server

Test OPTIMIZED_SP_EXECUTESQL in SQL Server 2025, inspect compilation counters and cached plans, and compare a concurrent workload.

Read More
A large soap bubble with a smaller one inside, a red bubble wand in a dish.

Temp Table Scope in Nested Procedures and Dynamic SQL

March 30, 2013
Pinal Dave
SQL Tips and Tricks
Dynamic SQL, SQL Stored Procedure, SQL TempDB, Temp Table

Explain temp table scope across nested procedures and dynamic SQL, reproduce missing and duplicate-name errors, and pass rows safely.

Read More
A netted fruit cage over blackcurrant bushes, a blackbird perched outside unable to get in.

Safe Dynamic SQL With sp_executesql and QUOTENAME

February 19, 2013
Pinal Dave
SQL Tips and Tricks
Dynamic SQL, SQL Server, SQL Server Security, SQL Stored Procedure

Write safe dynamic SQL with typed value parameters, validated object names, and allowed sort columns, then test hostile input.

Read More
A stone-lined irrigation channel with a small sluice gate and a red wheel across the water

Sharding by Customer: Routing Queries to the Right Database

January 8, 2013
Pinal Dave
SQL Tips and Tricks
Best Practices, Database, Dynamic SQL, SQL Server

Plan sharding by customer with a shard map, parameterized routing and synonyms, and account for reports, keys and customer moves.

Read More
A row of small snowmen along a snowy path, each wearing the same red scarf

Running a Command in Every Database Without sp_MSforeachdb

January 5, 2013
Pinal Dave
SQL Tips and Tricks
DBA, Dynamic SQL, SQL Scripts, SQL Server

Run a checked command in every database with a FAST_FORWARD cursor, AG role filter, QUOTENAME, and per-database error log.

Read More
Previous 1 2 3 Next
  • Books
  • Testimonials
  • Privacy Policy

© 2006 – 2026 All rights reserved. pinal @ SQLAuthority.com