Category: Databases
-
How to create a temporary table that can be seen by different SqlCommands on the same connection?
The unexpected answer (that I learned the hard way) is: well, it depends on whether you have parameters on your command or not. The point being, if you execute a parameterless SqlCommand, the sql gets executed directly, the same way as if you entered it into the query analyzer. If you add a parameter, the…
-
Howto: SQL / LINQ JOIN on TOP 1 row
Problem: you want to do a LEFT or INNER JOIN between two tables but include only one record from the other table: that is, you don’t want the join to create duplicate records. Interestingly enough, I found the solution to this through LINQ. In LINQ, you can do this without really thinking about it: from…
-
Pre-fetching data with LINQ to SQL?
(Yes, I know I’m behind the times – “LINQ to SQL? Who needs it when there’s the newest preview/alpha/beta of the Entity Framework?” Well, I did start this application in EF v1 and ran away when I saw “unsupported” stickers plastered all over it. So, no thanks, I’m waiting for the proverbial “Microsoft v3.11” (or…
-
Common Table Expression For Sorting Hierarchical Records By Depth
I tried to find this on the net, and was unable to find a satisfying and simple solution. How do you retrieve hierarchical records (say, from a self-referencing table) sorted by their depth in the hierarchy? Microsoft SQL Server 2005/2008 has a new SQL construct, Here’s one example: a Category table with an ID (primary…