Return value from exec(@sql)
Harness output parameters with sp_executesql
to capture dynamic SQL's returned data. Declare a variable, include it in your query and designate it as OUTPUT
:
Capturing complex data using TABLE variables
When you predict multiple rows or columns in your returned data, crafting a table variable can be quite the tool. Here's how to declare and fill a table variable with dynamic SQL results:
Quick capture with SELECT INTO
Fetching a single row doesn't need to be a hassle. With SELECT INTO, assign values directly within dynamic SQL:
Execution clean-up with SET NOCOUNT ON
You don't want sneaky side-messages like 'rows affected' meddling with the fruits of your hard work:
What's your status? Checking with @@ROWCOUNT and @@ERROR
For your own peace of mind, always check @@ROWCOUNT
and @@ERROR
:
Catering for various data
Make sure you're prepared for different types of alien life:
Sweeping up after yourself
You don’t want tomorrow’s mission to stumble over today’s clutter. Mop up those temporary tables after use:
Using status beacons
Send clear signals back to base with explicit return codes in your stored procedures:
Post-mission data usage
You don't have to stick to just looking at pretty space photos. Get hands-on with the data stored in table variables or temp tables for further exploration:
Was this article helpful?