SQL Server query

If I may ask a noobish question... If you require a result set for a report, for example, with multiple joins from different tables, how would one go about doing that in code? Would you manually create the dataset and manipulate it? I like the idea of reducing strain on the SQL server.

Sometimes keeping the processing in server is more effecient. For example if a complex insurance or banking system which processes customer details in bulk, such as premium renewals for tens if thousands of customers, these never need to leave the server. It would daft storing these business rules off the server.

I have worked on systems where there is a lot of logic in the server and systems where there is barely any. Each solution was appropriate to the problem.

Calling "no stored procedures" as "best practice" is the biggest load of bs I have seen put out in the Internet in a long time.

You might feel the need to reduce strain on your server if your hardware is crap, but all you have done is move it

Things to think about:

1. Excessive disk io. This is due to badly written SQL. Stored procedures execute in memory. They use much the same SQL you would have used if you didn't use sp's
2. Web services
There is always a price in connecting outside of you network, but there are many cases where it's worthwhile.
3. Connecting to the db
This is always expensive. Connection pooling solves this if used properly. It's not on issue for users who connect and stay connected but can be disaster for web users and bulk processes which are not setup correctly to leverage connection pooling.
4. Network
Why hit your db server with hundreds of small queries when you only need to send minimal info to return a result? I'm sure everyone is aware of what excessive network activity can do to a server ? Consider a standard query which returns the customer details as well as info held in other tables. Call a sp by passing the customer I'd and getting back the results. One call. Simple. Instead of making 3, 4 or more. Per call. Per user.
5. Code maintenance
Having complex business ruled stored on the db server is far more effecient than having them duplicated in multiple systems. Less room for mistakes where multiple copies need updating(I suspect this is the main reason why some so called experts don't use sp's as a matter of principle. They don't know the transact or pl/SQL language. Lack of skills) Interestingly, some of the largest and most successful companies use sp's. I would trust their judgement over that of some of the "experts" we have here
6. Security
Stored procedures hide your data structures and ensure that outside users cannot construct their own queries which may cause problems. The last thing you want when running critical batch job is for some knob(usually senior) from executing a query which does full table scan on your largest transaction table.
 
Last edited:
Sometimes keeping the processing in server is more effecient. For example if a complex insurance or banking system which processes customer details in bulk, such as premium renewals for tens if thousands of customers, these never need to leave the server. It would daft storing these business rules off the server.

I have worked on systems where there is a lot of logic in the server and systems where there is barely any. Each solution was appropriate to the problem.

Calling "no stored procedures" as "best practice" is the biggest load of bs I have seen put out in the Internet in a long time.

You might feel the need to reduce strain on your server if your hardware is crap, but all you have done is move it

Things to think about:

1. Excessive disk io. This is due to badly written SQL. Stored procedures execute in memory. They use much the same SQL you would have used if you didn't use sp's
2. Web services
There is always a price in connecting outside of you network, but there are many cases where it's worthwhile.
3. Connecting to the db
This is always expensive. Connection pooling solves this if used properly. It's not on issue for users who connect and stay connected but can be disaster for web users and bulk processes which are not setup correctly to leverage connection pooling.
4. Network
Why hit your db server with hundreds of small queries when you only need to send minimal info to return a result? I'm sure everyone is aware of what excessive network activity can do to a server ? Consider a standard query which returns the customer details as well as info held in other tables. Call a sp by passing the customer I'd and getting back the results. One call. Simple. Instead of making 3, 4 or more. Per call. Per user.
5. Code maintenance
Having complex business ruled stored on the db server is far more effecient than having them duplicated in multiple systems. Less room for mistakes where multiple copies need updating(I suspect this is the main reason why some so called experts don't use sp's as a matter of principle. They don't know the transact or pl/SQL language. Lack of skills) Interestingly, some of the largest and most successful companies use sp's. I would trust their judgement over that of some of the "experts" we have here
6. Security
Stored procedures hide your data structures and ensure that outside users cannot construct their own queries which may cause problems. The last thing you want when running critical batch job is for some knob(usually senior) from executing a query which does full table scan on your largest transaction table.

Lol.
 
Do big companies (e.g. banks, insurance companies, etc) not use SPs?
 
Do big companies (e.g. banks, insurance companies, etc) not use SPs?

Yeah you're right.

Guys im sorry im going to go and convert all my code to sprocs now. You have all enlightened me. ;)
 
Yeah you're right.

Guys im sorry im going to go and convert all my code to sprocs now. You have all enlightened me. ;)
I think in your case it is a matter of personal preference, not that SPs are bad per se. :p
 
I think in your case it is a matter of personal preference, not that SPs are bad per se. :p

When i see a valid use for an SP in my environment that will out perform my code. I will cave.
 
When i see a valid use for an SP in my environment that will out perform my code. I will cave.

Anyway, I am looking at that dapper orm and it seems to perform well comparatively. And it does support SPs. LOL
Link
Still trying to find more documentation and more examples. I will give it a try since I haven't touched EF
 
Last edited:
When i see a valid use for an SP in my environment that will out perform my code. I will cave.

never for 1 second implied your environment would be better with an SP. You on the hand reject them out of hand as a matter of principle. Sorry, thats just bs :D
 
Not using a sp, you tighlty couple your Dynamic SQL etc into code. We deploy to 100's of users our WPF app with click one. So there is a issue, you got to tell 100's of users to log off because you want to redeploy a dynamic sp/middle tier dll? Nope.

Were a sp can be changed and nobody has to be logged off (or you need to redeploy)

Yes.. use caching etc before db can be hit would increase speed. And yes.. not all buss logic needs to be in DB. And yes, I have worked on enterprise apps.

And yes.. i want to scream if I see dynamic sql. We banned it... We optimize our sp, 100's of users working concurrently with no issues.
 
Last edited:
i love bdd, tdd and testing.
i normally use orms and love them.
the problem with stored procs is generally one of testing.

so what i have done whenever stored procs are needed is to encapsulate them in some sort of interface.

this allows me to mock up the procs when im doing standard unit tests but then when im doing integration tests i run against the actual proc.

if my tests pass on my mock but fail on the proc then someone needs to either fix the proc or tell me wtf i must do to update my mock implementation.

best of both worlds.
i was vehemently against stored procs for a long time just because of how ****ty they can be and how easily they can be modified with no form of testing.

at my previous job i had to work with them but did whatever i could to cover my code. the amount of times this covered my ass was amazing.
 
Hi guys

I'm smashing this question in here because I don't want to start a new thread just yet.

I have a Customer Table and a Orders table. I would like to know which customer has the 3rd highest total count of Orders. No date range specified.

Please assist it would be appreciated. My mind has drawn a blank on something that should essentially be quite simple.
 
Hi guys

I'm smashing this question in here because I don't want to start a new thread just yet.

I have a Customer Table and a Orders table. I would like to know which customer has the 3rd highest total count of Orders. No date range specified.

Please assist it would be appreciated. My mind has drawn a blank on something that should essentially be quite simple.
Order, skip then take?
 
Cheat lol. https://www.linqpad.net/ will show you the SQL generated.

v-IDZ5ig-400x400.jpg
 
Hi guys

I'm smashing this question in here because I don't want to start a new thread just yet.

I have a Customer Table and a Orders table. I would like to know which customer has the 3rd highest total count of Orders. No date range specified.

Please assist it would be appreciated. My mind has drawn a blank on something that should essentially be quite simple.
Group by customer ID and order it, would do top 3 to reduce size. That then in a temp table and select the row number. https://stackoverflow.com/questions/1022514/selecting-nth-record-in-an-sql-query

Outside of sql server, Maria db has limit with offset, think only oracle and ms don't have support still, but haven't used oracle in 5 years and haven't had a need to solve a problem like this in a long time where didn't need the result sets other results as well.
 
Thanks Jonathan I will give that a go. LIMIT 1 possibly would have helped but that plan stopped dead as noticed that is MySql no MSSql. Shot man I will post the result after load shedding is finished.
 
Top
Sign up to the MyBroadband newsletter
X