Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
Problems with Stored Procedures and Dynamic SQL
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
TetonSig  
View profile  
 More options Jan 27, 11:01 am
From: TetonSig <teton...@gmail.com>
Date: Fri, 27 Jan 2012 08:01:25 -0800 (PST)
Local: Fri, Jan 27 2012 11:01 am
Subject: Problems with Stored Procedures and Dynamic SQL
We're trying to implement tSQLt in our existing system.  One of our
current road blocks is a set of procedures for reports which have
dynamic SQL that we execute results into a series of temp tables and
then join them for the final resultset.

Greatly simplified:

create proc RPT_Ads
as

set @dynSQL1='select ' + @collist + @fromClause + @whereclause

insert #worktable1 exec @dynSQL1

set @dynSQL2='select ' + @collist + @fromClause + @whereclause

insert #worktable2 exec @dynSQL2

select * from #worktable1 inner join #worktable2
GO

The problem then becomes inside the unit test trying to do

insert #results exec RPT_Ads

An INSERT EXEC statement cannot be nested.

I'm not opposed to trying to refactor our stored procedures, just
wondering if there's any other way to work around this?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
tSQLt@sqlity.net  
View profile  
 More options Jan 27, 12:40 pm
From: "tS...@sqlity.net" <tS...@sqlity.net>
Date: Fri, 27 Jan 2012 12:40:11 -0500
Local: Fri, Jan 27 2012 12:40 pm
Subject: Re: Problems with Stored Procedures and Dynamic SQL

Hi,

I am not aware of a way to do a nested  INSERT EXEC  in SQL Server and
tSQLt does not (yet) offer a true resultset compare.
However, in your case you could rewrite the procedure to not use the
INSERT EXEC  pattern, as the attached sql document shows.
dbo.DoExecInsert_old uses your pattern -> the test will error
dbo.DoExecInsert_new provides the same functionality without INSERT EXEC ->
the test works

The test it self is just a quick demo and it itself is breaking a lot of
testing best practices, so do not use it as a template for other tests.

As always, when using dynamic SQL properly filter any input values to
prevent SQL Injection Attacks.

Sebastian

--------------------------------------------------------------------------- ------------------------------
Try out SQL Test, the new SSMS Plugin for tSQLt: http://sql-test.com
--------------------------------------------------------------------------- ------------------------------

  InsertExecTests.sql
3K Download

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
TetonSig  
View profile  
 More options Jan 27, 3:24 pm
From: TetonSig <teton...@gmail.com>
Date: Fri, 27 Jan 2012 12:24:59 -0800 (PST)
Local: Fri, Jan 27 2012 3:24 pm
Subject: Re: Problems with Stored Procedures and Dynamic SQL
Works like a charm! Thanks a lot.

On Jan 27, 12:40 pm, "tS...@sqlity.net" <tS...@sqlity.net> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »