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

  • Thread starter SlurrerOfSpeech
  • Start date
  • Tags
    Server Sql
In summary: This just allows you to structure your code in a more object-oriented way. It also makes it easy to unit test your code.In summary, the language SQL doesn't provide any simple ways of solving common problems, which can be a hassle when working with data. The power of SQL is not in doing row by row stuff but in manipulating whole tables and views to get what you want.
  • #1
SlurrerOfSpeech
141
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
  • #2
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.
 
  • #3
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
  • #4
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

1. How has SQL Server managed to maintain its relevance in the face of newer technologies?

SQL Server has remained relevant due to its continuous updates and advancements, making it adaptable to changing technological environments. It also offers a wide range of features and capabilities that cater to different business needs.

2. What makes SQL Server a preferred choice for data storage and retrieval?

SQL Server's robust architecture and efficient data storage mechanisms make it a reliable choice for data management. It also offers various security features to ensure the integrity and confidentiality of stored data.

3. How has SQL Server evolved over the years to keep up with the changing data landscape?

SQL Server has evolved significantly over the years, with each new version introducing new features and enhancements to meet the demands of modern data management. These updates include support for big data, cloud computing, and improved performance.

4. What sets SQL Server apart from other database management systems?

SQL Server's comprehensive set of tools and features make it stand out from other database management systems. It offers a user-friendly interface, advanced analytics capabilities, and a range of deployment options, including on-premises, cloud, and hybrid.

5. How has SQL Server adapted to the growing demand for data analytics?

SQL Server has adapted to the demand for data analytics by introducing features such as in-memory processing, advanced analytics, and machine learning capabilities. It also offers integration with popular analytics tools, making it a versatile choice for data analysis needs.

Similar threads

  • Programming and Computer Science
2
Replies
51
Views
4K
  • Programming and Computer Science
Replies
7
Views
425
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
5
Views
5K
  • Computing and Technology
Replies
31
Views
3K
  • Computing and Technology
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
Back
Top