Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 1234»»»

What is meant by set based programming? Expand / Collapse
Author
Message
Posted Thursday, August 9, 2012 9:05 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Today @ 4:24 AM
Points: 264, Visits: 654
Hi all,
I have come across various section on this website where it is mention that SQL Server is a set based programming language.
My understanding about it is that : Set based means that the operations are perform on a set of values but i am not able to relate this defination with operations being perform with SQL Server.
I am from C language background and i don't see much difference in applying logic over here but still these are early days for me in SQL Server, so i think that i am missing something or unable to graps the concept.
Post #1343113
Posted Thursday, August 9, 2012 10:20 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, April 1, 2014 3:26 PM
Points: 316, Visits: 1,497
Set based languages are declarative. You tell sql server WHAT you want not how to do it.

"I want the set of customers who ordered more than $100 last month unioned with the top 200 customers from last year by total number of orders"

In a procedural language, you tell it HOW to calculate the results. So, you would need loop through all the orders for last month, summing them up , then you'd need to count the total orders for every customer last year, sort them, keep the top 200, and return the unique customers.

The key to becoming proficient in sql is being able to express the "WHAT" while letting Sql Server worry about the "HOW".

Also, pure set based logic doesn't use loops. So, if you find yourself wanting to loop over rows, you are not taking a set based approach.
Post #1343126
Posted Thursday, August 9, 2012 11:03 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 3:25 AM
Points: 3,648, Visits: 5,328
To quote Jeff Moden's signature:

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."



Wiser words have not been spoken, except possibly () for my mantra:



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1343135
Posted Friday, August 10, 2012 4:30 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 12:16 AM
Points: 6,259, Visits: 7,451
Not that I don't admire Jeff's abilities, but he doesn't speak the language sometimes... or better stated, he speaks the language of database.

From the perpsective of every coder, here's the difference.

Instead of a For Each loop on each component of a collection, you attempt to perform an operation against the collection directly. So instead of a For Each row In (select) do y, you do Collection.DoY (value). It's not a direct comparison but it gets the idea across.

Here's why, though: In your average programming language, you're expected to do 90% of the work. In a database engine, 90% of the work is done for you if everything is setup correctly. From there, properly defined and organized, all you need to do is tell it to do what you want in a single attempt to everything you want it to work against. The complications come in during setup, which is why the majority of us are here to attempt to help.



- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Post #1343262
Posted Friday, August 10, 2012 6:37 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Friday, June 27, 2014 12:43 PM
Points: 15,444, Visits: 9,596
What I've found most useful when designing set-based vs row-based coding, is a few tips:

1. Assume rows don't have an order to them. So you can't do "row 1, then row 2, then row 3". Do rows 1-3 all at once.

2. Treat sets of rows as a single object, and call methods against them, instead of doing something procedural to individual rows. Methods include calculations, aggregations, CRUD, and so on. Think of the From clause in a query as defining the object, and the other clauses (Select, Update, Where, Order By, etc.) as methods against that set-of-rows object.

3. The concept of working against columns instead of rows also helps.

There's more to it, but that's a good start.


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #1343345
Posted Friday, August 10, 2012 5:07 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 9:16 PM
Points: 37,102, Visits: 31,655
Shadab Shah (8/9/2012)
Hi all,
I have come across various section on this website where it is mention that SQL Server is a set based programming language.
My understanding about it is that : Set based means that the operations are perform on a set of values but i am not able to relate this defination with operations being perform with SQL Server.
I am from C language background and i don't see much difference in applying logic over here but still these are early days for me in SQL Server, so i think that i am missing something or unable to graps the concept.


Perhaps the easiest way to make this clear is to use an actual example of code. If you're game, post the code that you would use if someone told you they simply wanted a single column result set from 1 to a million and we'll discuss whatever you come up with.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1343715
Posted Friday, August 10, 2012 6:47 PM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, May 14, 2014 4:36 PM
Points: 496, Visits: 1,122
90% of the work is done for you


How do I love thee SQL? Let me count(theWays)..


Looking for a Deadlock Victim Support Group..
Post #1343728
Posted Friday, August 10, 2012 8:05 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Thursday, June 12, 2014 2:04 PM
Points: 1,789, Visits: 1,014
Think of it this way , if u walk into a room and ask each person if they are above 20 years of age, ur are performing something like RBAR or row by row operation. on the other hand if you shouted "everybody over 20 please raise your hand" you dealing with a set. The idea here is to ignore the uniqueness for each row and try to identify what they have in common and use the commonality to programmer better.

Jayanth Kurup
Post #1343736
Posted Friday, August 10, 2012 8:10 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 9:16 PM
Points: 37,102, Visits: 31,655
Possinator (8/10/2012)
90% of the work is done for you


How do I love thee SQL? Let me count(theWays)..


Ditto that!


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1343737
Posted Friday, August 10, 2012 8:13 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 9:16 PM
Points: 37,102, Visits: 31,655
Jayanth_Kurup (8/10/2012)
Think of it this way , if u walk into a room and ask each person if they are above 20 years of age, ur are performing something like RBAR or row by row operation. on the other hand if you shouted "everybody over 20 please raise your hand" you dealing with a set. The idea here is to ignore the uniqueness for each row and try to identify what they have in common and use the commonality to programmer better.


I've seen a lot of "word" examples but I really like this one especially for it's simplicity and it's going into my cerebral brief case.. Thanks for posting it.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1343739
« Prev Topic | Next Topic »

Add to briefcase 1234»»»

Permissions Expand / Collapse