views:

671

answers:

9

I always thought an SQL compiler would break but apparently nesing can nearly be infinite. Is this code to be trashed immediately or is there some glimmer of hope that something like this can function?

This query doesn't really belong to me so I cannot post it... However let's just pretend it is this one:

[SELECT /*+ NOPARALLEL bypass_recursive_check */ 
SP_ALIAS_190, 
((CASE SP_ALIAS_191
WHEN 1
THEN 'PROVIDER::ALL_PROV::'
WHEN 0]
+6  A: 

Clearly, you've never seen the SQL that comes out of the Sharepoint DAL.

Rex M
Is it BIGGER? Sweet lord... that gives me a thought may this was generated...
ojblass
Yup. But you offended someone. +1 back to 0 anyway.
le dorfier
Sharepoint::SQL as Word::HTML
Daniel LeCheminant
I'll tell a brief story to illustrate: Our DB cluster for our SP farm was experiencing massive deadlocks. Senior MS DBA was brought in to diagnose the SQL Server, not knowing we were running SP. She found the offending query and exclaimed "what on earth is this? Of course you're having these problems", thinking it was something we'd written. We told her that came from SP. She said nothing else.
Rex M
Is it not written that nothing good can never come of the SharePoint DAL?
Chris Ballance
Now if it had just been a 43-page autogenerated C# DAL class there would have been no surprise ... and only a partial class at that ...
le dorfier
+2  A: 

I ran into a problem similar to this recently and I came to a decision by considering a couple of things:

  • How long is this going to take to maintain vs. rewrite?
  • How critical is this? There may be a lot of logic that may be difficult to unravel and the value in the fact that "it works" exceeds the value from an immediate rewrite.

And of course, there was the political decision management had to make concerning risking explaining why something that was recently created would have to be rewritten.

In the end (for me), find + replace was my friend.

llamaoo7
A: 

Is it possible to maintain a 43 page function, say, in C#? The answer is obvious ;). I just cannot imagine this. If I were you I would break it into smaller parts.

SeasonedCoder
I would say a 43 page function might be doable because it is sequential in nature. I would thing it is horrible form...
ojblass
If I were him, I'd break the coder who wrote the 43 page function into smaller parts! :-)
McWafflestix
I've seen a single function of about 12 pages of x86/x86-64 asm.
ewanm89
Once when I was young and stupid I wrote a 43 page switch statement. It didn't work
1800 INFORMATION
+1  A: 

This sounds like a candidate for the dailyWTF.

altCognito
+7  A: 

If the query is generated by a tool (or by code), then it may be relatively simple to maintain (in the sense that the query generation code may in fact be well written and maintainable)

Daniel LeCheminant
I am tending to go with this one... it cannot possibly have been written by hand could it?
ojblass
I certainly wouldn't *expect* it to be written by hand...
Daniel LeCheminant
You should be able to tell if it was written by hand or generated by a tool, e.g. comments, different syntax styles (formatting) etc
RobS
I am going to pursue this line of thought... ty
ojblass
+1  A: 

Refactor it using the WITH statement. Add lots and lots and lots of comments.

If you break it into pieces that can be managed, you stand a much better chance.

EvilTeach
+1  A: 

If it contains allot of nesting I would say no.

Like any code no matter what language, you should only look at re-writting it because you can make it more efficient or easier to understand.

Based on my experiance I have been able to reduce badly written SQL 4 to 5 times its size and many times its performance because the origonal auther really had no idea.

nullptr
A: 

If you think that's bad, you should see Industrial Logic's sample video on code smells: Technical Debt. Definitely not autogenerated.

tvanfosson
A: 

Two things:

  • Will only machines ever need to read this SQL?
  • Are you stuck with the underlying schema?

If you have a 43 page query and you answered yes to the first two questions, welcome to SharePoint development

Chris Ballance