« Leveraging Multi-tenancy for Instant Sandbox Creation | Main | Microsoft CRM 4.0 Fiscal Year Settings »

July 04, 2008

Developing Dynamic SQL with Parameters - and Testing in SQL Query Analyzer.

In previous posts we looked at how Dynamic SQL is different from Aliased Views, but provides great deal of flexibility in coding. This flexibility comes at a slight disadvantage when it comes to building / testing. – Aliased Views can be written and tested within the SRS dataset query window. – Dynamic SQL hits some challenges when you try to test from within SRS – it can be done, but you spend a lot of time pasting in the values of variables each time you want to test/run the query.

Building and testing Dynamic SQL is easiest for me if I’m working inside the familiar interface of SQL Query Analyzer. – But In order to work in Query analyzer you need to declare and set the value of the variables that will ultimately be passed from CRM into the report. – Once you set up these inside your query window, you can build/test your Dynamic SQL to your heart’s content.

Here’s a simple example I worked on with another consultant to demonstrate how to test/pass variables into dynamic SQL.

The challenge was when he attempted to pass a GUID into the report, the query failed to run. The reason was at runtime, SQL was attempting to concatenate a GUID with a string and it was failing. It can be a little tricky - you essentially have to get the value of the parameter converted to a string 'inside' the SQL text that you're building.

To demonstrate what was happening, I opened a query analyzer window and added the following code/comments. – the first 2 groups of comments and code are only used in the query analyzer window to declare and set the values of the parameters that would normally be declared in the report parameters and set by CRM at run-time.

/*
These are for setting up the values in query analyzer for testing.
In SRS, they would be declared in the SRS report parameters list,
not in the dataset query.
*/
Declare @ContactID UniqueIdentifier
Declare @CRM_FilteredContact nvarchar(max)

/*
These are the parameters that would be passed to your query from the parent – I’m just setting them here so the SQL statement below has something to include when it attempts to execute.
*/
Set @ContactID = (select top 1 contactID from filteredContact)
Set @CRM_FilteredContact  = 'Select X.* from FilteredContact as X'

/*
This is the query that would be in the report dataset
*/
DECLARE @SQL1 nvarchar(max)
Set @SQL1 =
'
SELECT * FROM ('
+ @CRM_FilteredContact +
') Contacts
where Contacts.contactid like ''' +
CAST(@ContactId as nvarchar(40)) + ''' '
EXEC(@SQL1)

Note the ''' surrounding the parameter is 3 single quotes, it gets tricky sometimes because you have to use the 2 single quotes to insert one single quote inside the string and not mistakenly ending the string at that point. Once you’ve included the single quote into the string, the next (3rd) single quote ends the string so you can include the string value of the variable, then the next set of 3 quotes restarts the string, adds another single quote and finally yet another single quote finally ends the whole string. Whew.

-- Scott Sewell

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00e54fb34b6f883300e553a37ca88834

Listed below are links to weblogs that reference Developing Dynamic SQL with Parameters - and Testing in SQL Query Analyzer.:

Comments

Feed You can follow this conversation by subscribing to the comment feed for this post.

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

CustomerEffective is a Microsoft Gold Certified Partner specializing in Customer Relationship Management (CRM) implementation, development and integration. We help organizations improve profitability through automation of sales, service and marketing processes.

Twitter Updates

    follow me on Twitter

    Search The Blog

    • Search the Blog
       

      WWW
      blog.customereffective.com
    Subscribe to this blog's feed

     Subscribe in a reader

    Add to Google Reader or Homepage

    Enter your email address:

    Delivered by FeedBurner