How has SQL Server not gone the way of the dinosaur?

  • Thread starter Thread starter SlurrerOfSpeech
  • Start date Start date
  • Tags Tags
    Server Sql
AI Thread Summary
The discussion highlights frustrations with SQL Server's T-SQL, particularly regarding its complexity and limitations in handling common tasks. Users express dissatisfaction with the need to create temporary tables for data manipulation, especially when dealing with tables that have many columns. The MERGE statement is criticized for its inability to update multiple columns simultaneously, which has been a long-standing request for improvement from the community. Additionally, the discussion contrasts T-SQL's approach to iterating through data with more straightforward programming constructs found in languages like C#. The complexity of using cursors and the procedural nature of T-SQL are noted as cumbersome compared to set-based operations, which are the strength of SQL. The conversation also touches on the shift away from traditional SQL practices towards using Data Object Abstraction (DOA) frameworks like Doctrine, indicating a trend towards more efficient data handling methods.
SlurrerOfSpeech
Messages
141
Reaction score
11
I use SQL Server at my job and for my personal projects (only because I'm familiar with it). Nevertheless I am always amazed at how the language doesn't provide any simple ways of solving common problems.

For example, let's say I have some data which represents rows I want to insert or update in another table. I first have to create a temp table with the same column definition as the target table. This is a lot of busy-work when the target table has dozens of columns. Then I have to insert all this data into the temp table. Then I have to use a MERGE statement which involves a lot of syntax and, by the way, can't UPDATE more than 1 column! You are out of luck if you want to update both "EmailAddress" and "FirstName" for any MATCHED "UserId". I've seen users on MSDN and TechNet as far back as 2007 request to Microsoft that they change this. It remains the same.

Another example. I want to iterate through a list of values in a column, basically the equivalent of

Code:
for(int id in MyTable.Select(t => t.Id))
{
    // ... 
}

Here's how you to that in T-SQL:

Code:
    CREATE TABLE #ids (rn INT, id INT)
    INSERT INTO #ids (rn, id)
    SELECT DISTINCT ROW_NUMBER() OVER(ORDER BY Id), Id
    FROM MyTable
    DECLARE @i INT = 0
    DECLARE @n INT = (SELECT COUNT(*) FROM #ids)
    WHILE(@i < @n)
    BEGIN
         DECLARE @id INT = (SELECT TOP 1 id FROM #ids WHERE rn = @k)
         // ... 
         @i = @i + 1
    END

and that's probably wrong and there's no way to step-through debug it.
 
Technology news on Phys.org
The power of sql is not in doing row by row stuff but in manipulating whole tables and views to get what you want. It's akin to manipulating sets of data collecting, transforming , grouping and/or sorting it to create a report or input to another program.

Each SQL statement is a program unto itself especially the select statement.
 
You have to remember that SQL is a specialized program that is optimized to manipulate tables of data. So if your data is not already in a table, chances are that creating one will be required to optimize your job.
SlurrerOfSpeech said:
by the way, can't UPDATE more than 1 column! You are out of luck if you want to update both "EmailAddress" and "FirstName" for any MATCHED "UserId".
I'm more familiar with MySQL, but it seems that T-SQL also allow updating a column list, such as this example:
Code:
MERGE Production.ProductInventory AS target 
USING (SELECT ProductID, SUM(OrderQty) FROM Sales.SalesOrderDetail AS sod 
   JOIN Sales.SalesOrderHeader AS soh 
   ON sod.SalesOrderID = soh.SalesOrderID 
   AND soh.OrderDate = @OrderDate 
   GROUP BY ProductID) AS source (ProductID, OrderQty) 
ON (target.ProductID = source.ProductID) 
WHEN MATCHED AND target.Quantity - source.OrderQty <= 0 
   THEN DELETE 
WHEN MATCHED   
   THEN UPDATE SET target.Quantity = target.Quantity - source.OrderQty,   
                   target.ModifiedDate = GETDATE() 
OUTPUT $action, Inserted.ProductID, Inserted.Quantity, Inserted.ModifiedDate, Deleted.ProductID, 
   Deleted.Quantity, Deleted.ModifiedDate;
For the 2nd example, you are not really fair in your comparison. First you are missing some declarations in your FOR...LOOP, like MyTable. You are also ordering your table in the T-SQL, but not in your FOR...LOOP. Can't you store your SELECT into a variable like @mytable that would act as your MyTable variable, for example? Then you would be able to access your data. Here is a good example, I think:
Code:
DECLARE @CursorVar CURSOR; 

SET @CursorVar = CURSOR SCROLL DYNAMIC 
FOR 
SELECT LastName, FirstName 
FROM AdventureWorks2012.HumanResources.vEmployee 
WHERE LastName like 'B%'; 

OPEN @CursorVar; 

FETCH NEXT FROM @CursorVar; 
WHILE @@FETCH_STATUS = 0 
BEGIN 
    FETCH NEXT FROM @CursorVar 
END; 

CLOSE @CursorVar; 
DEALLOCATE @CursorVar;
Look into CURSOR as well for more info.
 
  • Like
Likes WWGD and jedishrfu
I'm confused about what you are trying to do. Can you give an example of the before and after tables? I don't understand what you mean that you can't update multiple columns at once.

Code:
UPDATE 
    Table_A 
set 
    EmailAddress=test@test.com, 
    FirstName = Table_B.FirstName
FROM
   Some_Table AS Table_A
   INNER JOIN Other_Table AS Table_B
       ON Table_A.UserId = Table_B.UserId

Also, a lot of people are getting away from procedural SQL and using DOAs like Doctrine.
 
  • Like
Likes WWGD and FactChecker
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top