writing prfessional queries

  • I am writing sqlserver queries from a long time....can you help me how to write insert/update and delete queries in a good and professional way?

    any template which i should follow?

    till now i do not include begin trans etc etc..

  • General guidelines that apply are :-

    Always schema qualify object names

    Always list out columns ( not select * , or insert into dbo.tb1 select * from dbo.tb2)

    SQL Keywords are in Caps

    Select , FROM , WHERE , JOIN , ON , GROUP BY , ORDER Are all in separate lines

    The comma between column names come at the beginning of the new line.

    Jayanth Kurup[/url]

  • in which situation i should use BEGIN TRAN etc...or using COMMIT etc etci know all mentioned points..thanks for those

  • It may not be required to explicitly call a begin tran end tran if there is only one operation being performed. like in a simple CRUD operation.

    for example

    if your inserting into 1 table only then the transaction is implicit and any failure in the insert will undo the changes automatically.

    on the other hand

    If you have data inserted into table1 which generates a value that is then updated into another table say table2. Then you want to be able to rollback transactions in both table1 and table2 if an insert fails in table 2.

    In this case you should use a try catch block with explicit transactions which can be rolled back.

    look up TRY CATCH in msdn for an example

    Jayanth Kurup[/url]

  • If you want a stored procedure template with transaction / error handling, you can check out my stored procedure code template here[/url].

    -- Gianluca Sartori

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply