Saturday, 12 October 2013

Decision Making statements in Visual Basic

                      Decision Making statements in Visual Basic


              Decision making statements are often used to validate certain condition and based on that execute some piece of code. As the name suggest, this statements are used  to check certain conditions. We can have multiple conditions in the statement and according to output, whether the check returns True or False, make some decision in your program.

             There are two types of decision making statements in visual basic, as follows:

  1. If Statements
  2. Select Case Statements



If statements can further be classified into three types:
  • If - Then Statements:  An If - Then statements has only one condition and if that condition gets satisfied that a particular piece of code gets executed
Example:

                         If  name = "XYZ" Then
                                       MsgBox "Welcome" & name
                         End if
  • If -then-else statements: This statements handles both the scenario, what should happen when a condition gets satisfied and what should happens if it doesn't.
Example:

                         If  name = "XYZ" Then
                                       MsgBox "Welcome" & name
                             Else
                                    MsgBox "You are not an authorized person"
                         End if
  • Nested IF-Then-Else Statements: This statements has two or more statements based on which our program acts.
Example:

                         If  name = "XYZ" Then
                                       MsgBox "Welcome" & name
                             Elseif name = "ABC"
                                      MsgBox "Welcome" & name
                             Else
                                      MsgBox "You are not an authorized person"
                         End if

Select Case statements are generally used when there are more conditions to check , which gets quite complicated to handle

Example:

                         Select Case name
Case "XYZ"
                                                MsgBox "Welcome" & name
Case "ABC"
                                                MsgBox "Welcome" & name
Case "IJK"
                                                MsgBox "Welcome" & name
Case "ELSE"
                                                MsgBox "You are not an authorized person"
END SELECT

Difference between temp table and CTE

                                            Difference between temp table and CTE

 CTE:                 

  • CTE is  un-materialized/ non-indexable (cannot create indexes on CTE)
  • CTE is logical/disposable View
  • CTE persists only till the very next query
  • CTE cannot have constraints
  • CTE is mostly used for recursion, as CTE can call itself
  • CTE resists in memory



 Temp Table:

  • Temp table gets stored in temp table
  • Temp table persists till the current connection ends
  • Temp table can be referred in sub procedure
  • Temp table can have constraints,indexes and primary defined
  • Indexes can be implemented in Temp Table
  • Data can be updated in Temp Table
  • Temp Tables are stored in disk

Similar Posts:   Difference between CTE and View  ,  CTE(Common Table Expressions) in SQL ,                                      T- SQL Interview Questions and Answer

Difference between CTE and View - SQL

             Difference between CTE and View


               A CTE is a temporary/logical View, it is not store physically. It is a named query, the result for which is only available to the very next query after the CTE is defined. CTE is defined using WITH clause.

                  A View is a physical object that is present in the database. View is as good as a Table but it doesn't store data physically as compared to a table, only the data schema is stored in View. View,when referred, pulls data by executing the query that is associated with it.


                 The biggest difference between a CTE and View, is that, View or derived table cannot call itself, whereas CTE can call itself and hence support recursion.


                In many databases, views have options, for instance to index them.