{"id":486,"date":"2008-01-26T16:07:08","date_gmt":"2008-01-26T21:07:08","guid":{"rendered":"http:\/\/blogs.devhorizon.com\/reza\/?p=486"},"modified":"2008-01-27T01:50:46","modified_gmt":"2008-01-27T06:50:46","slug":"fba-combining-stored-procedures-and-power-of-ctes","status":"publish","type":"post","link":"https:\/\/blogs.devhorizon.com\/reza\/2008\/01\/26\/fba-combining-stored-procedures-and-power-of-ctes\/","title":{"rendered":"FBA: Combining Stored Procedures and Power of CTEs"},"content":{"rendered":"<p>Since I am a lazy developer I normally don&#8217;t like to author multiple stored procedures if I can afford to aggregate all the logic in one place without causing too much complexity. For example when working with custom authentication providers you are often required to write separate stored procedures for GetAllUsers, FindUsersByName, FindUsersByEmail that can be called from their counterpart overridden methods in your custom authentication provider&#8217;s code.  Following example demonstrates how you can have one stored procedure that can handle GetAllUsers, FindUsersByName, FindUsersByEmail  logic in one stored procedure. As shown below,  @email and @username  parameters can be null  which means you can ignore these two filters  and get all the users instead. One important thing to note is the table that holds all the user information in this example. It is called Contacts with ContactID as the primary key, but yours might be different! <!--more--><\/p>\n<div id=\"ig-sh-1\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">sql<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"sql\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">CREATE<\/span> <span style=\"color: #993333;font-weight: bold\">PROCEDURE<\/span> <span style=\"color: #66cc66\">&#091;<\/span>dbo<span style=\"color: #66cc66\">&#093;<\/span><span style=\"color: #66cc66\">.<\/span><span style=\"color: #66cc66\">&#091;<\/span>GetUsers<span style=\"color: #66cc66\">&#093;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #66cc66\">&#040;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">@email <span style=\"color: #993333;font-weight: bold\">AS<\/span> <span style=\"color: #993333;font-weight: bold\">VARCHAR<\/span><span style=\"color: #66cc66\">&#040;<\/span><span style=\"color: #cc66cc\">50<\/span><span style=\"color: #66cc66\">&#041;<\/span> <span style=\"color: #66cc66\">=<\/span> <span style=\"color: #993333;font-weight: bold\">NULL<\/span><span style=\"color: #66cc66\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">@username <span style=\"color: #993333;font-weight: bold\">AS<\/span> <span style=\"color: #993333;font-weight: bold\">VARCHAR<\/span><span style=\"color: #66cc66\">&#040;<\/span><span style=\"color: #cc66cc\">50<\/span><span style=\"color: #66cc66\">&#041;<\/span> <span style=\"color: #66cc66\">=<\/span> <span style=\"color: #993333;font-weight: bold\">NULL<\/span><span style=\"color: #66cc66\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">@pageIndex <span style=\"color: #993333;font-weight: bold\">AS<\/span> <span style=\"color: #993333;font-weight: bold\">INTEGER<\/span><span style=\"color: #66cc66\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">@pageSize <span style=\"color: #993333;font-weight: bold\">AS<\/span> <span style=\"color: #993333;font-weight: bold\">INTEGER<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #66cc66\">&#041;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">AS<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">SET<\/span> NOCOUNT <span style=\"color: #993333;font-weight: bold\">ON<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #808080;font-style: italic\">--Set the page bounds<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">DECLARE<\/span> @PageLowerBound <span style=\"color: #993333;font-weight: bold\">int<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">DECLARE<\/span> @PageUpperBound <span style=\"color: #993333;font-weight: bold\">int<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">DECLARE<\/span> @TotalRecords &nbsp; <span style=\"color: #993333;font-weight: bold\">int<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">SET<\/span> @PageLowerBound <span style=\"color: #66cc66\">=<\/span> @PageSize <span style=\"color: #66cc66\">*<\/span> @PageIndex<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">SET<\/span> @PageUpperBound <span style=\"color: #66cc66\">=<\/span> @PageSize <span style=\"color: #66cc66\">-<\/span> <span style=\"color: #cc66cc\">1<\/span> <span style=\"color: #66cc66\">+<\/span> @PageLowerBound<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #808080;font-style: italic\">--Create a temp table for pager logic<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">CREATE<\/span> <span style=\"color: #993333;font-weight: bold\">TABLE<\/span> #PageIndexForContacts<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #66cc66\">&#040;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">IndexId <span style=\"color: #993333;font-weight: bold\">INT<\/span> <span style=\"color: #993333;font-weight: bold\">IDENTITY<\/span> <span style=\"color: #66cc66\">&#040;<\/span><span style=\"color: #cc66cc\">0<\/span><span style=\"color: #66cc66\">,<\/span> <span style=\"color: #cc66cc\">1<\/span><span style=\"color: #66cc66\">&#041;<\/span> <span style=\"color: #993333;font-weight: bold\">NOT<\/span> <span style=\"color: #993333;font-weight: bold\">NULL<\/span><span style=\"color: #66cc66\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">ContactID UNIQUEIDENTIFIER<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #66cc66\">&#041;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #808080;font-style: italic\">--Insert into temp table<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">IF<\/span><span style=\"color: #66cc66\">&#040;<\/span> @email <span style=\"color: #993333;font-weight: bold\">IS<\/span> <span style=\"color: #993333;font-weight: bold\">NOT<\/span> <span style=\"color: #993333;font-weight: bold\">NULL<\/span> <span style=\"color: #66cc66\">&#041;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">INSERT<\/span> <span style=\"color: #993333;font-weight: bold\">INTO<\/span> #PageIndexForContacts<span style=\"color: #66cc66\">&#040;<\/span>ContactID<span style=\"color: #66cc66\">&#041;<\/span> <span style=\"color: #993333;font-weight: bold\">SELECT<\/span> ContactID <span style=\"color: #993333;font-weight: bold\">FROM<\/span> Contacts<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">WHERE<\/span> &nbsp;<span style=\"color: #993333;font-weight: bold\">LOWER<\/span><span style=\"color: #66cc66\">&#040;<\/span>Email<span style=\"color: #66cc66\">&#041;<\/span> <span style=\"color: #993333;font-weight: bold\">LIKE<\/span> <span style=\"color: #993333;font-weight: bold\">LOWER<\/span><span style=\"color: #66cc66\">&#040;<\/span>@email<span style=\"color: #66cc66\">&#041;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">ORDER<\/span> <span style=\"color: #993333;font-weight: bold\">BY<\/span> Email<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">ELSE<\/span> <span style=\"color: #993333;font-weight: bold\">IF<\/span><span style=\"color: #66cc66\">&#040;<\/span>@username <span style=\"color: #993333;font-weight: bold\">IS<\/span> <span style=\"color: #993333;font-weight: bold\">NOT<\/span> <span style=\"color: #993333;font-weight: bold\">NULL<\/span><span style=\"color: #66cc66\">&#041;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">INSERT<\/span> <span style=\"color: #993333;font-weight: bold\">INTO<\/span> #PageIndexForContacts<span style=\"color: #66cc66\">&#040;<\/span>ContactID<span style=\"color: #66cc66\">&#041;<\/span> <span style=\"color: #993333;font-weight: bold\">SELECT<\/span> ContactID <span style=\"color: #993333;font-weight: bold\">FROM<\/span> Contacts<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">WHERE<\/span> &nbsp;<span style=\"color: #993333;font-weight: bold\">LOWER<\/span><span style=\"color: #66cc66\">&#040;<\/span>UserName<span style=\"color: #66cc66\">&#041;<\/span> <span style=\"color: #993333;font-weight: bold\">LIKE<\/span> <span style=\"color: #993333;font-weight: bold\">LOWER<\/span><span style=\"color: #66cc66\">&#040;<\/span>UserName<span style=\"color: #66cc66\">&#041;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">ORDER<\/span> <span style=\"color: #993333;font-weight: bold\">BY<\/span> UserName<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">ELSE<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">INSERT<\/span> <span style=\"color: #993333;font-weight: bold\">INTO<\/span> #PageIndexForContacts<span style=\"color: #66cc66\">&#040;<\/span>ContactID<span style=\"color: #66cc66\">&#041;<\/span> <span style=\"color: #993333;font-weight: bold\">SELECT<\/span> ContactID <span style=\"color: #993333;font-weight: bold\">FROM<\/span> Contacts<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">ORDER<\/span> <span style=\"color: #993333;font-weight: bold\">BY<\/span> UserName<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">SELECT<\/span> @TotalRecords <span style=\"color: #66cc66\">=<\/span> @@ROWCOUNT<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">SELECT<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">Comment<span style=\"color: #66cc66\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">CreationDate<span style=\"color: #66cc66\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">Email<span style=\"color: #66cc66\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">IsApproved<span style=\"color: #66cc66\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">IsLockedOut<span style=\"color: #66cc66\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">IsOnline<span style=\"color: #66cc66\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">LastActivityDate<span style=\"color: #66cc66\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">LastLockoutDate<span style=\"color: #66cc66\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">LastLoginDate<span style=\"color: #66cc66\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">LastPasswordChangedDate<span style=\"color: #66cc66\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">PasswordQuestion<span style=\"color: #66cc66\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">ProviderName<span style=\"color: #66cc66\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">ProviderUserKey<span style=\"color: #66cc66\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">UserName<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">FROM<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">Contacts con<span style=\"color: #66cc66\">,<\/span>#PageIndexForContacts conp<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">WHERE<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">con<span style=\"color: #66cc66\">.<\/span>ContactID <span style=\"color: #66cc66\">=<\/span> conp<span style=\"color: #66cc66\">.<\/span>ContactID <span style=\"color: #993333;font-weight: bold\">AND<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">conp<span style=\"color: #66cc66\">.<\/span>IndexId &amp;lt;<span style=\"color: #66cc66\">=<\/span> @PageLowerBound <span style=\"color: #993333;font-weight: bold\">AND<\/span> conp<span style=\"color: #66cc66\">.<\/span>IndexId &amp;gt;<span style=\"color: #66cc66\">=<\/span> @PageUpperBound<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">RETURN<\/span> @TotalRecords<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #808080;font-style: italic\">-- Drop the temp table<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">DROP<\/span> <span style=\"color: #993333;font-weight: bold\">TABLE<\/span> #PageIndexForContacts<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">SET<\/span> NOCOUNT OFF<\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>One new  feature in SQL Server 2005 that comes handy a lot when dealing with paging and recursions is <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/ms190766.aspx\">Common Table Expressions<\/a> (a.k.a CTEs) . Common Table Expressions are much nicer way to work with derived tables and specially when you need recursion functionality.  Let&#8217;s say , you want to retrieve all your users and show them on &lt;<span style=\"font-family: Courier New; font-size: 10pt\"><span style=\"color: #a31515\">wssawc<\/span><span style=\"color: blue\">:<\/span><span style=\"color: #a31515\">SPGridView&gt; <\/span><\/span>control with <span style=\"text-decoration: underline\">pagination<\/span>  and row numbers.  It&#8217;s ideal if you deal with records in chunks (specified by @pageIndex parameter) to <span style=\"text-decoration: underline\">save bandwidth and time<\/span> instead of retrieving all users at once. Then in your code (<span style=\"font-family: Courier New; font-size: 10pt\">MemberGrid_ClickNext<\/span>), what you need to do is to send the right @pageIndex parameter to the stored procedure which in turn takes care of paging the requested records and return the right chunk to the client .Here is how CTEs can be your friend:<\/p>\n<div id=\"ig-sh-2\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">sql<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"sql\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">with<\/span> UsersCTE <span style=\"color: #993333;font-weight: bold\">as<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #66cc66\">&#040;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">SELECT<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">ROW_NUMBER<\/span><span style=\"color: #66cc66\">&#040;<\/span><span style=\"color: #66cc66\">&#041;<\/span> <span style=\"color: #993333;font-weight: bold\">over<\/span> <span style=\"color: #66cc66\">&#040;<\/span><span style=\"color: #993333;font-weight: bold\">order<\/span> <span style=\"color: #993333;font-weight: bold\">by<\/span> con<span style=\"color: #66cc66\">.<\/span>UserName<span style=\"color: #66cc66\">,<\/span> con<span style=\"color: #66cc66\">.<\/span>CreatedOn<span style=\"color: #66cc66\">,<\/span> con<span style=\"color: #66cc66\">.<\/span> Email<span style=\"color: #66cc66\">&#041;<\/span> <span style=\"color: #993333;font-weight: bold\">as<\/span> RowNum<span style=\"color: #66cc66\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">con<span style=\"color: #66cc66\">.<\/span>UserName<span style=\"color: #66cc66\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">con<span style=\"color: #66cc66\">.<\/span>CreatedOn<span style=\"color: #66cc66\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">con<span style=\"color: #66cc66\">.<\/span>Email<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">FROM<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">dbo<span style=\"color: #66cc66\">.<\/span>Contacts con<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #66cc66\">&#041;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">select<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">UsersCTE<span style=\"color: #66cc66\">.*<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">from<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">UsersCTE<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">where<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">RowNum <span style=\"color: #993333;font-weight: bold\">between<\/span> @PageLowerBound <span style=\"color: #993333;font-weight: bold\">and<\/span> @PageUpperBound<\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<table style=\"border-collapse: collapse\" border=\"0\">\n<tr>\n<td style=\"padding-left: 7px; padding-right: 7px\">\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/blogs.devhorizon.com\/reza\/wp-content\/uploads\/2008\/01\/012608-2107-fbacombinin13.png\" \/><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p style=\"text-align: center\">Retrieving Users in 10 records chunks<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p>It is not uncommon that you find yourself  in a situation that your identity store (for which you are writing your custom authentication provider) allows duplicate usernames, WOW that&#8217;s scary isn&#8217;t it? For example in the Users table structure I provided above , I am extracting user information from a table  that is indexed on ContactID and not on username. Picture a system (Like a CRM instance) that allows a non-member becomes a member and upon this transformation users can keep their username. Reza (username= rezaa) becomes a member but Reza is still the same person with the same username, billing address, credit card information and bluh bluh . From CRM standpoint Reza is no longer old Reza  but a new identity, therefore a new ContactID is issued for him and some of his associated information is copied to the new identity including his username.  This leads us to a situation in which we have to deal with duplicate usernames ! Your stored procedure returns two records (with different ContactIDs but same username)  and in your code when you construct <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/system.web.security.membershipusercollection.aspx\">MembershipUserCollection<\/a> you get an exception that two keys with the same value cannot be added to a hashtable (which makes sense!). You need to delete the duplicate rows right in your stored procedure before returning them to your custom authentication provider. Alternatively, you can handle avoiding duplications in your code by putting your code in a try-catch block and move on to the next record should adding the current record throws an exception , but question is why do you need to take this approach when you can easily handle it in your stored procedure? So how exactly does this all work? I am so glad you asked! Here you go:<\/p>\n<div id=\"ig-sh-3\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">sql<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"sql\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">with<\/span> UsersCTE <span style=\"color: #993333;font-weight: bold\">as<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #66cc66\">&#040;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">SELECT<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">ROW_NUMBER<\/span><span style=\"color: #66cc66\">&#040;<\/span><span style=\"color: #66cc66\">&#041;<\/span> <span style=\"color: #993333;font-weight: bold\">over<\/span> <span style=\"color: #66cc66\">&#040;<\/span><span style=\"color: #993333;font-weight: bold\">order<\/span> <span style=\"color: #993333;font-weight: bold\">by<\/span> con<span style=\"color: #66cc66\">.<\/span>ContactID<span style=\"color: #66cc66\">,<\/span>con<span style=\"color: #66cc66\">.<\/span>UserName<span style=\"color: #66cc66\">,<\/span>con<span style=\"color: #66cc66\">.<\/span>CreatedOn<span style=\"color: #66cc66\">,<\/span>con<span style=\"color: #66cc66\">.<\/span>Email<span style=\"color: #66cc66\">&#041;<\/span> <span style=\"color: #993333;font-weight: bold\">as<\/span> RowNum<span style=\"color: #66cc66\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">con<span style=\"color: #66cc66\">.<\/span>ContactID<span style=\"color: #66cc66\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">con<span style=\"color: #66cc66\">.<\/span>UserName<span style=\"color: #66cc66\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">con<span style=\"color: #66cc66\">.<\/span>CreatedOn<span style=\"color: #66cc66\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">con<span style=\"color: #66cc66\">.<\/span>Email<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">FROM<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">dbo<span style=\"color: #66cc66\">.<\/span>Contacts con<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #66cc66\">&#041;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">SELECT<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">UsersCTE<span style=\"color: #66cc66\">.<\/span>RowNum<span style=\"color: #66cc66\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">conm<span style=\"color: #66cc66\">.<\/span>ContactID<span style=\"color: #66cc66\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">conm<span style=\"color: #66cc66\">.<\/span>CreatedOn<span style=\"color: #66cc66\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">conm<span style=\"color: #66cc66\">.<\/span>Email<span style=\"color: #66cc66\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">conm<span style=\"color: #66cc66\">.<\/span>UserName<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">FROM<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">dbo<span style=\"color: #66cc66\">.<\/span>Contacts conm <span style=\"color: #993333;font-weight: bold\">INNER<\/span> <span style=\"color: #993333;font-weight: bold\">JOIN<\/span> UsersCTE <span style=\"color: #993333;font-weight: bold\">ON<\/span> conm<span style=\"color: #66cc66\">.<\/span>ContactID <span style=\"color: #66cc66\">=<\/span> UsersCTE<span style=\"color: #66cc66\">.<\/span>ContactID<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">WHERE<\/span> &nbsp; &nbsp; &nbsp; &nbsp;UsersCTE<span style=\"color: #66cc66\">.<\/span>RowNum <span style=\"color: #993333;font-weight: bold\">IN<\/span> <span style=\"color: #66cc66\">&#040;<\/span><span style=\"color: #993333;font-weight: bold\">SELECT<\/span> <span style=\"color: #993333;font-weight: bold\">MIN<\/span> <span style=\"color: #66cc66\">&#040;<\/span>UsersCTE<span style=\"color: #66cc66\">.<\/span>RowNum<span style=\"color: #66cc66\">&#041;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">FROM<\/span> UsersCTE<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">GROUP<\/span> <span style=\"color: #993333;font-weight: bold\">BY<\/span> UserName<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #993333;font-weight: bold\">HAVING<\/span> <span style=\"color: #993333;font-weight: bold\">Count<\/span><span style=\"color: #66cc66\">&#040;<\/span><span style=\"color: #66cc66\">*<\/span><span style=\"color: #66cc66\">&#041;<\/span> <span style=\"color: #66cc66\">=<\/span> <span style=\"color: #cc66cc\">1<\/span><span style=\"color: #66cc66\">&#041;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n","protected":false},"excerpt":{"rendered":"<p>Since I am a lazy developer I normally don&#8217;t like to author multiple stored procedures if I can afford to aggregate all the logic in one place without causing too much complexity. For example when working with custom authentication providers you are often required to write separate stored procedures for GetAllUsers, FindUsersByName, FindUsersByEmail that can [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-486","post","type-post","status-publish","format-standard","hentry","category-moss-2007"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>FBA: Combining Stored Procedures and Power of CTEs - Reza Alirezaei&#039;s Blog %<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blogs.devhorizon.com\/reza\/2008\/01\/26\/fba-combining-stored-procedures-and-power-of-ctes\/\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Reza Alirezaei\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2008\\\/01\\\/26\\\/fba-combining-stored-procedures-and-power-of-ctes\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2008\\\/01\\\/26\\\/fba-combining-stored-procedures-and-power-of-ctes\\\/\"},\"author\":{\"name\":\"Reza Alirezaei\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#\\\/schema\\\/person\\\/cdbb24d283697a65951cb4a14e474938\"},\"headline\":\"FBA: Combining Stored Procedures and Power of CTEs\",\"datePublished\":\"2008-01-26T21:07:08+00:00\",\"dateModified\":\"2008-01-27T06:50:46+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2008\\\/01\\\/26\\\/fba-combining-stored-procedures-and-power-of-ctes\\\/\"},\"wordCount\":876,\"commentCount\":1,\"image\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2008\\\/01\\\/26\\\/fba-combining-stored-procedures-and-power-of-ctes\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/wp-content\\\/uploads\\\/2008\\\/01\\\/012608-2107-fbacombinin13.png\",\"articleSection\":[\"MOSS 2007\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2008\\\/01\\\/26\\\/fba-combining-stored-procedures-and-power-of-ctes\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2008\\\/01\\\/26\\\/fba-combining-stored-procedures-and-power-of-ctes\\\/\",\"url\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2008\\\/01\\\/26\\\/fba-combining-stored-procedures-and-power-of-ctes\\\/\",\"name\":\"FBA: Combining Stored Procedures and Power of CTEs - Reza Alirezaei's Blog %\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2008\\\/01\\\/26\\\/fba-combining-stored-procedures-and-power-of-ctes\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2008\\\/01\\\/26\\\/fba-combining-stored-procedures-and-power-of-ctes\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/wp-content\\\/uploads\\\/2008\\\/01\\\/012608-2107-fbacombinin13.png\",\"datePublished\":\"2008-01-26T21:07:08+00:00\",\"dateModified\":\"2008-01-27T06:50:46+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#\\\/schema\\\/person\\\/cdbb24d283697a65951cb4a14e474938\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2008\\\/01\\\/26\\\/fba-combining-stored-procedures-and-power-of-ctes\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2008\\\/01\\\/26\\\/fba-combining-stored-procedures-and-power-of-ctes\\\/#primaryimage\",\"url\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/wp-content\\\/uploads\\\/2008\\\/01\\\/012608-2107-fbacombinin13.png\",\"contentUrl\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/wp-content\\\/uploads\\\/2008\\\/01\\\/012608-2107-fbacombinin13.png\",\"width\":574,\"height\":376},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#website\",\"url\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/\",\"name\":\"Reza Alirezaei's Blog\",\"description\":\"Blogging from the field!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#\\\/schema\\\/person\\\/cdbb24d283697a65951cb4a14e474938\",\"name\":\"Reza Alirezaei\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g\",\"caption\":\"Reza Alirezaei\"},\"url\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/author\\\/rezaa\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"FBA: Combining Stored Procedures and Power of CTEs - Reza Alirezaei's Blog %","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blogs.devhorizon.com\/reza\/2008\/01\/26\/fba-combining-stored-procedures-and-power-of-ctes\/","twitter_misc":{"Written by":"Reza Alirezaei","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blogs.devhorizon.com\/reza\/2008\/01\/26\/fba-combining-stored-procedures-and-power-of-ctes\/#article","isPartOf":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/2008\/01\/26\/fba-combining-stored-procedures-and-power-of-ctes\/"},"author":{"name":"Reza Alirezaei","@id":"https:\/\/blogs.devhorizon.com\/reza\/#\/schema\/person\/cdbb24d283697a65951cb4a14e474938"},"headline":"FBA: Combining Stored Procedures and Power of CTEs","datePublished":"2008-01-26T21:07:08+00:00","dateModified":"2008-01-27T06:50:46+00:00","mainEntityOfPage":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/2008\/01\/26\/fba-combining-stored-procedures-and-power-of-ctes\/"},"wordCount":876,"commentCount":1,"image":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/2008\/01\/26\/fba-combining-stored-procedures-and-power-of-ctes\/#primaryimage"},"thumbnailUrl":"https:\/\/blogs.devhorizon.com\/reza\/wp-content\/uploads\/2008\/01\/012608-2107-fbacombinin13.png","articleSection":["MOSS 2007"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blogs.devhorizon.com\/reza\/2008\/01\/26\/fba-combining-stored-procedures-and-power-of-ctes\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blogs.devhorizon.com\/reza\/2008\/01\/26\/fba-combining-stored-procedures-and-power-of-ctes\/","url":"https:\/\/blogs.devhorizon.com\/reza\/2008\/01\/26\/fba-combining-stored-procedures-and-power-of-ctes\/","name":"FBA: Combining Stored Procedures and Power of CTEs - Reza Alirezaei's Blog %","isPartOf":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/#website"},"primaryImageOfPage":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/2008\/01\/26\/fba-combining-stored-procedures-and-power-of-ctes\/#primaryimage"},"image":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/2008\/01\/26\/fba-combining-stored-procedures-and-power-of-ctes\/#primaryimage"},"thumbnailUrl":"https:\/\/blogs.devhorizon.com\/reza\/wp-content\/uploads\/2008\/01\/012608-2107-fbacombinin13.png","datePublished":"2008-01-26T21:07:08+00:00","dateModified":"2008-01-27T06:50:46+00:00","author":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/#\/schema\/person\/cdbb24d283697a65951cb4a14e474938"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blogs.devhorizon.com\/reza\/2008\/01\/26\/fba-combining-stored-procedures-and-power-of-ctes\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blogs.devhorizon.com\/reza\/2008\/01\/26\/fba-combining-stored-procedures-and-power-of-ctes\/#primaryimage","url":"https:\/\/blogs.devhorizon.com\/reza\/wp-content\/uploads\/2008\/01\/012608-2107-fbacombinin13.png","contentUrl":"https:\/\/blogs.devhorizon.com\/reza\/wp-content\/uploads\/2008\/01\/012608-2107-fbacombinin13.png","width":574,"height":376},{"@type":"WebSite","@id":"https:\/\/blogs.devhorizon.com\/reza\/#website","url":"https:\/\/blogs.devhorizon.com\/reza\/","name":"Reza Alirezaei's Blog","description":"Blogging from the field!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blogs.devhorizon.com\/reza\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blogs.devhorizon.com\/reza\/#\/schema\/person\/cdbb24d283697a65951cb4a14e474938","name":"Reza Alirezaei","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g","caption":"Reza Alirezaei"},"url":"https:\/\/blogs.devhorizon.com\/reza\/author\/rezaa\/"}]}},"_links":{"self":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/posts\/486","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/comments?post=486"}],"version-history":[{"count":0,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/posts\/486\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/media?parent=486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/categories?post=486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/tags?post=486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}