SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query - Good practice of a query


Query - Good practice of a query

Author
Message
Balaji L
Balaji L
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1307 Visits: 271
Hi,

Most of the SPs in our db written in a old format style and i dont thing that is the best way of write a query.

eg:
1) in where criteria
where 1=1
and ....

I dont think 1=1 is necessary in where criteria. is it affect performance?

2) all sps are written with RETURN stt. I dont think this RETURN required.

can you also suggest some best approach/practice to write a query?
Grant Fritchey
Grant Fritchey
SSC Guru
SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)

Group: General Forum Members
Points: 95525 Visits: 33013
Even if you don't explicitly state a return value, all stored procedures have one. I prefer to do something along these lines in 2005 (similar, but different error handling, in 2000).

CREATE MyProc
AS
BEGIN TRY
... write the query
END TRY
BEGIN CATCH
...more error handling as necessary
RETURN ERROR_NUMBER();
END CATCH
RETURN 0;



----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
GilaMonster
GilaMonster
SSC Guru
SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)

Group: General Forum Members
Points: 216573 Visits: 46278
Balaji (1/26/2008)

I dont think 1=1 is necessary in where criteria. is it affect performance?


Not necessary at all. It shouldn't affect performance. The optimiser should be smart enough to ignore it. however, better be safe and take it out.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


StarNamer
StarNamer
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2718 Visits: 1992
Balaji (1/26/2008)
1) in where criteria
where 1=1
and ....

The only time I've seen this done is in generated queries. That is, a query is built from selecting various options where any combination may occur.
sqlstring = "SELECT ... WHERE 1=1"
if (opt1) then sqlstring = sqlstring + " AND (...)"
if (opt2) then sqlstring = sqlstring + " AND (...)"
if (op33) then sqlstring = sqlstring + " AND (...)"

If the queries are 'hardcoded' then you may as well clean them up.
Balaji (1/26/2008)
can you also suggest some best approach/practice to write a query?
Write it so it returns the right resultsSmile

Seriously... I've found that, although it doesn't affect performance, consistent good formatting helps keep track of what's going on. If your db has all the foreign keys defined properly, then SSMS' view creation is a good way to start; I then edit the SQL to include aliases (otherwise it's far too messy) and finallly switch to a query window to get the formatting right as the layout generated by SSMS is pretty unreadable.

Derek.

Derek
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search