Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      The AI productivity paradox in software engineering: Balancing efficiency and human skill retention

      July 2, 2025

      The impact of gray work on software development

      July 2, 2025

      CSS Intelligence: Speculating On The Future Of A Smarter Language

      July 2, 2025

      Hallucinated code, real threat: How slopsquatting targets AI-assisted development

      July 1, 2025

      Xbox is cancelling Rare’s ‘Everwild’ and ZeniMax’s new MMORPG IP as part of broader cuts — with ‘Perfect Dark’ impacted as well

      July 2, 2025

      Microsoft is closing down Xbox studio The Initiative, with Perfect Dark killed as well — joining Everwild and ZeniMax’s new IP, and other unannounced projects

      July 2, 2025

      No, Microsoft and Xbox’s Phil Spencer isn’t stepping down any time soon — here’s the truth

      July 2, 2025

      Everwild’s cancellation has me worried for one of my favorite dev teams and Xbox itself — It needs creative new games to thrive and refresh its identity

      July 2, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Trust but Verify: The Curious Case of AI Hallucinations

      July 2, 2025
      Recent

      Trust but Verify: The Curious Case of AI Hallucinations

      July 2, 2025

      From Flow to Fabric: Connecting Power Automate to Microsoft Fabric

      July 2, 2025

      Flutter Web Hot Reload Has Landed – No More Refreshes!

      July 2, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Xbox is cancelling Rare’s ‘Everwild’ and ZeniMax’s new MMORPG IP as part of broader cuts — with ‘Perfect Dark’ impacted as well

      July 2, 2025
      Recent

      Xbox is cancelling Rare’s ‘Everwild’ and ZeniMax’s new MMORPG IP as part of broader cuts — with ‘Perfect Dark’ impacted as well

      July 2, 2025

      Microsoft is closing down Xbox studio The Initiative, with Perfect Dark killed as well — joining Everwild and ZeniMax’s new IP, and other unannounced projects

      July 2, 2025

      No, Microsoft and Xbox’s Phil Spencer isn’t stepping down any time soon — here’s the truth

      July 2, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»News & Updates»CodeSOD: Join Us in this Query

    CodeSOD: Join Us in this Query

    April 2, 2025

    Today’s anonymous submitter worked for a “large, US-based, e-commerce company.” This particular company was, some time back, looking to save money, and like so many companies do, that meant hiring offshore contractors.

    Now, I want to stress, there’s certainly nothing magical about national borders which turns software engineers into incompetents. The reality is simply that contractors never have their client’s best interests at heart; they only want to be good enough to complete their contract. This gets multiplied by the contracting firm’s desire to maximize their profits by keeping their contractors as booked as possible. And it gets further multiplied by the remoteness and siloing of the interaction, especially across timezones. Often, the customer sends out requirements, and three months later gets a finished feature, with no more contact than that- and it never goes well.

    All that said, let’s look at some SQL Server code. It’s long, so we’ll take it in chunks.

    -- ===============================================================================
    -- Author     : Ignacius Ignoramus
    -- Create date: 04-12-2020
    -- Description:	SP of Getting Discrepancy of Allocation Reconciliation Snapshot
    -- ===============================================================================
    

    That the comment reinforces that this is an “SP”, aka stored procedure, is already not my favorite thing to see. The description is certainly made up of words, and I think I get the gist.

    ALTER PROCEDURE [dbo].[Discrepency]
    	(
    		@startDate DATETIME,
    		@endDate DATETIME
    	)
    AS
    
    BEGIN
    

    Nothing really to see here; it’s easy to see that we’re going to run a query for a date range. That’s fine and common.

    	DECLARE @tblReturn TABLE
    	(
    		intOrderItemId	   INT
    	)
    

    Hmm. T-SQL lets you define table variables, which are exactly what they sound like. It’s a local variable in this procedure, that acts like a table. You can insert/update/delete/query it. The vague name is a little sketch, and the fact that it holds only one field also makes me go “hmmm”, but this isn’t bad.

    	DECLARE @tblReturn1 TABLE
    	(
    		intOrderItemId	   INT
    	)
    

    Uh oh.

    	DECLARE @tblReturn2 TABLE
    	(
    		intOrderItemId	   INT
    	)
    

    Oh no.

    	DECLARE @tblReturn3 TABLE
    	(
    		intOrderItemId	   INT
    	)
    

    Oh no no no.

    	DECLARE @tblReturn4 TABLE
    	(
    		intOrderItemId	   INT
    	)
    

    This doesn’t bode well.

    So they’ve declared five variables called tblReturn, that all hold the same data structure.

    What happens next? This next block is gonna be long.

    	INSERT INTO @tblReturn --(intOrderItemId) VALUES (@_ordersToBeAllocated)
    
    	/* OrderItemsPlaced */		
    
    		select 		
    		intOrderItemId
    		from CompanyDatabase..Orders o
    		inner join CompanyDatabase..OrderItems oi on oi.intOrderId = o.intOrderId
    		where o.dtmTimeStamp between @startDate and  @endDate
    
    
    		AND intOrderItemId Not In 
    		(
    
    		/* _itemsOnBackorder */
    
    		select intOrderItemId			
    		from CompanyDatabase..OrderItems oi
    		inner join CompanyDatabase..Orders o on o.intOrderId = oi.intOrderId
    		where o.dtmTimeStamp between @startDate and  @endDate
    		and oi.strstatus='backordered' 
    		)
    
    		AND intOrderItemId Not In 
    		(
    
    		/* _itemsOnHold */
    
    		select intOrderItemId			
    		from CompanyDatabase..OrderItems oi
    		inner join CompanyDatabase..Orders o on o.intOrderId = oi.intOrderId
    		where o.dtmTimeStamp between @startDate and  @endDate
    		and o.strstatus='ONHOLD'
    		and oi.strStatus <> 'BACKORDERED' 
    		)
    
    		AND intOrderItemId Not In 
    		(
    
    		/* _itemsOnReview */
    
    		select  intOrderItemId			
    		from CompanyDatabase..OrderItems oi
    		inner join CompanyDatabase..Orders o on o.intOrderId = oi.intOrderId
    		where o.dtmTimeStamp between @startDate and  @endDate 
    		and o.strstatus='REVIEW' 
    		and oi.strStatus <> 'BACKORDERED'
    		)
    
    		AND intOrderItemId Not In 
    		(
    
    		/*_itemsOnPending*/
    
    		select  intOrderItemId			
    		from CompanyDatabase..OrderItems oi
    		inner join CompanyDatabase..Orders o on o.intOrderId = oi.intOrderId
    		where o.dtmTimeStamp between @startDate and  @endDate
    		and o.strstatus='PENDING'
    		and oi.strStatus <> 'BACKORDERED'
    		)
    
    		AND intOrderItemId Not In 
    		(
    
    		/*_itemsCancelled */
    
    		select  intOrderItemId			
    		from CompanyDatabase..OrderItems oi
    		inner join CompanyDatabase..Orders o on o.intOrderId = oi.intOrderId
    		where o.dtmTimeStamp between @startDate and  @endDate
    		and oi.strstatus='CANCELLED' 
    		)
    

    We insert into @tblReturn the result of a query, and this query relies heavily on using a big pile of subqueries to decide if a record should be included in the output- but these subqueries all query the same tables as the root query. I’m fairly certain this could be a simple join with a pretty readable where clause, but I’m also not going to sit here and rewrite it right now, we’ve got a lot more query to look at.

    INSERT INTO @tblReturn1
    
    		
    		/* _backOrderItemsReleased */	
    
    		select  intOrderItemId			
    		from CompanyDatabase..OrderItems oi
    		inner join CompanyDatabase..orders o on o.intorderid = oi.intorderid
    		where oi.intOrderItemid in (
    			  select intRecordID 
    			  from CompanyDatabase..StatusChangeLog
    			  where strRecordType = 'OrderItem'
    			  and strOldStatus in ('BACKORDERED')
    			  and strNewStatus in ('NEW', 'RECYCLED')
    			  and dtmTimeStamp between @startDate and  @endDate  
    		)
    		and o.dtmTimeStamp < @startDate
    		
    
    		UNION
    		(
    			/*_pendingHoldItemsReleased*/
    
    			select  intOrderItemId					
    			from CompanyDatabase..OrderItems oi
    			inner join CompanyDatabase..orders o on o.intorderid = oi.intorderid
    			where oi.intOrderID in (
    				  select intRecordID 
    				  from CompanyDatabase..StatusChangeLog
    				  where strRecordType = 'Order'
    				  and strOldStatus in ('REVIEW', 'ONHOLD', 'PENDING')
    				  and strNewStatus in ('NEW', 'PROCESSING')
    				  and dtmTimeStamp between @startDate and  @endDate  
    			)
    			and o.dtmTimeStamp < @startDate
    			
    		)
    
    		UNION
    
    		/* _reallocationsowingtonostock */	
    		(
    			select oi.intOrderItemID				   	 
    			from CompanyDatabase.dbo.StatusChangeLog 
    			inner join CompanyDatabase.dbo.OrderItems oi on oi.intOrderItemID = CompanyDatabase.dbo.StatusChangeLog.intRecordID
    			inner join CompanyDatabase.dbo.Orders o on o.intOrderId = oi.intOrderId  
    
    			where strOldStatus = 'RECYCLED' and strNewStatus = 'ALLOCATED' 
    			and CompanyDatabase.dbo.StatusChangeLog.dtmTimestamp > @endDate and 
    			strRecordType = 'OrderItem'
    			and intRecordId in 
    			(
    			  select intRecordId from CompanyDatabase.dbo.StatusChangeLog 
    			  where strOldStatus = 'ALLOCATED' and strNewStatus = 'RECYCLED' 
    			  and strRecordType = 'OrderItem'
    			  and CompanyDatabase.dbo.StatusChangeLog.dtmTimestamp between @startDate and  @endDate  
    			)  
    		)
    

    Okay, just some unions with more subquery filtering. More of the same. It’s the next one that makes this special.

    INSERT INTO @tblReturn2
    
    	SELECT intOrderItemId FROM @tblReturn 
    	
    	UNION
    
    	SELECT intOrderItemId FROM @tblReturn1
    

    Ah, here’s the stuff. This is just bonkers. If the goal is to combine the results of these queries into a single table, you could just insert into one table the whole time.

    But we know that there are 5 of these tables, so why are we only going through the first two to combine them at this point?

        INSERT INTO @tblReturn3
    
    		/* _factoryAllocation*/
    
    		select 
    		oi.intOrderItemId                              
    		from CompanyDatabase..Shipments s 
    		inner join CompanyDatabase..ShipmentItems si on si.intShipmentID = s.intShipmentID
    		inner join Common.CompanyDatabase.Stores stores on stores.intStoreID = s.intLocationID
    		inner join CompanyDatabase..OrderItems oi on oi.intOrderItemId = si.intOrderItemId                                      
    		inner join CompanyDatabase..Orders o on o.intOrderId = s.intOrderId  
    		where s.dtmTimestamp >= @endDate
    		and stores.strLocationType = 'FACTORY'
    		
    		UNION 
    		(
    	 	  /*_storeAllocations*/
    
    		select oi.intOrderItemId                               
    		from CompanyDatabase..Shipments s 
    		inner join CompanyDatabase..ShipmentItems si on si.intShipmentID = s.intShipmentID
    		inner join Common.CompanyDatabase.Stores stores on stores.intStoreID = s.intLocationID
    		inner join CompanyDatabase..OrderItems oi on oi.intOrderItemId = si.intOrderItemId                                      
    		inner join CompanyDatabase..Orders o on o.intOrderId = s.intOrderId
    		where s.dtmTimestamp >= @endDate
    		and stores.strLocationType <> 'FACTORY'
    		)
    
    		UNION
    		(
    		/* _ordersWithAllocationProblems */
        	
    			select oi.intOrderItemId
    			from CompanyDatabase.dbo.StatusChangeLog
    			inner join CompanyDatabase.dbo.OrderItems oi on oi.intOrderItemID = CompanyDatabase.dbo.StatusChangeLog.intRecordID
    			inner join CompanyDatabase.dbo.Orders o on o.intOrderId = oi.intOrderId
    			where strRecordType = 'orderitem'
    			and strNewStatus = 'PROBLEM'
    			and strOldStatus = 'NEW'
    			and CompanyDatabase.dbo.StatusChangeLog.dtmTimestamp > @endDate
    			and o.dtmTimestamp < @endDate
    		)
    

    Okay, @tblReturn3 is more of the same. Nothing more to really add.

    	 INSERT INTO @tblReturn4
    	
    	 SELECT intOrderItemId FROM @tblReturn2 WHERE
    	 intOrderItemId NOT IN(SELECT intOrderItemId FROM @tblReturn3 )
    

    Ooh, but here we see something a bit different- we’re taking the set difference between @tblReturn2 and @tblReturn3. This would almost make sense if there weren’t already set operations in T-SQL which would handle all of this.

    Which brings us, finally, to the last query in the whole thing:

    SELECT 
    	 o.intOrderId
    	,oi.intOrderItemId
    	,o.dtmDate
    	,oi.strDescription
    	,o.strFirstName + o.strLastName AS 'Name'
    	,o.strEmail
    	,o.strBillingCountry
    	,o.strShippingCountry
    	FROM CompanyDatabase.dbo.OrderItems oi
    	INNER JOIN CompanyDatabase.dbo.Orders o on o.intOrderId = oi.intOrderId
    	WHERE oi.intOrderItemId IN (SELECT intOrderItemId FROM @tblReturn4)
    END
    

    At the end of all this, I’ve determined a few things.

    First, the developer responsible didn’t understand table variables. Second,they definitely didn’t understand joins. Third, they had no sense of the overall workflow of this query and just sorta fumbled through until they got results that the client said were okay.

    And somehow, this pile of trash made it through a code review by internal architects and got deployed to production, where it promptly became the worst performing query in their application. Correction: the worst performing query thus far.

    [Advertisement]
    Utilize BuildMaster to release your software with confidence, at the pace your business demands. Download today!

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleBlurble is a word guessing game
    Next Article 10 Best Free and Open Source Subtitle Editors

    Related Posts

    News & Updates

    Xbox is cancelling Rare’s ‘Everwild’ and ZeniMax’s new MMORPG IP as part of broader cuts — with ‘Perfect Dark’ impacted as well

    July 2, 2025
    News & Updates

    Microsoft is closing down Xbox studio The Initiative, with Perfect Dark killed as well — joining Everwild and ZeniMax’s new IP, and other unannounced projects

    July 2, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    OpenAI’s largest acquisition could help CEO Sam Altman make coders 10x more productive with “Windsurf” agentic IDE

    News & Updates

    Faugus Launcher is a simple and lightweight app for running games using UMU-Launcher

    Linux

    D-Spy – explore the D-Bus

    Linux

    The Apple Code Signing Handbook

    Development

    Highlights

    CVE-2025-47771 – PowSyBl SparseMatrix Deserialization Privilege Escalation Vulnerability

    June 19, 2025

    CVE ID : CVE-2025-47771

    Published : June 20, 2025, 12:15 a.m. | 2 hours, 25 minutes ago

    Description : PowSyBl (Power System Blocks) is a framework to build power system oriented software. In versions 6.3.0 to 6.7.1, there is a deserialization issue in the read method of the SparseMatrix class that can lead to a wide range of privilege escalations depending on the circumstances. This method takes in an InputStream and returns a SparseMatrix object. This issue has been patched in com.powsybl:powsybl-math: 6.7.2. A workaround for this issue involves not using SparseMatrix deserialization (SparseMatrix.read(…) methods).

    Severity: 0.0 | NA

    Visit the link for more details, such as CVSS details, affected products, timeline, and more…

    Mastering Webflow Shortcuts: The Ultimate Guide for Faster Builds

    May 1, 2025

    Prompt. Generate. Deploy. The New Product Design Workflow

    April 17, 2025
    Repurposing Protein Folding Models for Generation with Latent Diffusion

    Repurposing Protein Folding Models for Generation with Latent Diffusion

    July 2, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.