Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2018 May SQL Server Coding Standards: Best practices for UPDATE Query

SQL Server Coding Standards: Best practices for UPDATE Query

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

Prepared by Bihag Thaker

Good Indentation makes queries more readable and easy to debug. Try to break the queries into different lines by different query clauses like UPDATE, FROM, JOIN, ON, WHERE etc.

Wherever possible, place targeted column names in separate lines one level indented to the right side. If a column is not the first column in the list, place comma (,) separator before the name of a column.

This helps queries easier to read, debug and maintain. While debugging a query, anytime any line containing a column name can be easily commented out or uncommented as per the need.

Following is an example of properly indented code for UPDATE statement:

1
2
3
4
5
6
7
8
UPDATE [dbo].[tbl_Customers]
SET
FirstName = 'FirstNameUpdated'
,LastName = 'LastNameUpdated'
,BirthDate = '19841231'
,EmailAddress = 'EmailAddressUpdated@domain.com'
,Gender = 'M'
WHERE CustomerID = 'CUST001'

While writing UPDATE queries, make sure that the correct set of rows will be updated by including proper WHERE clause. UPDATE operation once committed cannot be undone.

Writing and executing SELECT query with the similar WHERE criteria as in UPDATE statement on the table should be exercised before implementing actual UPDATE queries. In this way, it can be confirmed that the correct set of rows will be updated.

When it is required to return updated records to the calling application with old values and new updated values, consider using OUTPUT clause in the UPDATE statement rather than using two separate UPDATE and SELECT statements to retrieve ‘just-updated’ records as shown below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
UPDATE [dbo].[tbl_Customers]
SET
FirstName = 'FirstName'
,LastName = 'LastName'
,BirthDate = '20111212'
,EmailAddress = 'EmailAddress'
,Gender = 'M'
OUTPUT
deleted.FirstName AS Old_FirstName
,inserted.FirstName AS New_FirstName
,deleted.LastName AS Old_LastName
,inserted.LastName AS New_LastName
,deleted.BirthDate AS Old_BirthDate
,inserted.BirthDate AS New_BirthDate
,deleted.EmailAddress AS Old_EmailAddress
,deleted.EmailAddress AS New_EmailAddress
,deleted.Gender AS Old_Gender
,inserted.Gender AS New_Gender
WHERE CustomerID='CUST006'

May 22, 2018Anvesh Patel
SQL Server Coding Standards: Best practices for INSERT QuerySQL Server Coding Standards: Best practices for DELETE Query

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

May 22, 2018 SQL Server Coding Standardsbasic sql commands, basic sql queries, coding best practices, SQL, sql basics, sql coding best practices, sql commands, sql database, sql formatter, sql language, SQL Programming, sql queries, sql queries for practice, sql query formatter, sql server format, sqlcode
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....