chapter 9 selecting, updating, and deleting data syed rizvi

Post on 02-Jan-2016

234 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Chapter 9 Selecting, Updating, and Deleting Data

Syed Rizvi

Retrieving Data Using GUI Right Click on the Table (Customers) Select Top 1000 Rows Above the results, see T-SQL code

generated by SSMSE On Right (F4), see Properties Window for

Connection and Query Details To set fewer than 1,000 rows returned,

set value of TOP clause for the SELECT under SQL Server Object Explorer/Commands

Retrieving Data Using Select Command

Retrieve data in any order, from any number of columns, and from one or more tables

Can perform calculations on that data during data retrieval

Retrieving Data Using Select Command

Retrieving Data Using Select Command

*: Optional—return all the columns from all the tables included in the query. Don’t use on large amounts of data or over a network as network could be busy or there are a large number of columns. Don’t use this against production data. By using this, you are bringing back more information than is required. Wherever possible, you should name the columns instead.

Retrieving Data Using Select Command

Don’t name every column unless required. Typing every column name takes time, but when you start dealing with more complex queries and a larger number of rows, the few extra seconds is worth it.

Avoid using *

Retrieving Data Using Select Command

Which one is a better approach Select * from customers Select firstName, lastName from

customers SELECT CustomerFirstName As ' First Name' ,CustomerLastName AS ' Surname' ,ClearedBalance Balance FROM CustomerDetails. Customers

Varying the Output Display in Query Editor

Results to Grid (Ctrl+D) -as you have seen

Results to Text (Ctrl+T)-within Query Editor

Results to File (Ctrl+Shift+F)-tabulated Word file

Varying the Output Display in Query Editor –Results to Text (Ctrl+T)

Varying the Output Display in Query Editor Varying the Output Display in Query Editor –Results to Text

See how the output is different than the previous query:

SELECT CustomerFirstName As ' First Name'

,CustomerLastName AS ' Surname' ,ClearedBalance Balance FROM CustomerDetails. Customers

Varying the Output Display in Query Editor Varying the Output Display in Query Editor –Results to File

Ctrl+Shift+F

See how the output is different than the previous query:

SELECT CustomerFirstName As ' First Name'

,CustomerLastName AS ' Surname' ,ClearedBalance Balance FROM CustomerDetails. Customers

Varying the Output Display in Query Editor Varying the Output Display in Query Editor –Results to File

Ctrl+Shift+F

Limiting a Search: The Use of WHERE

Press (Ctrl + D) to set the default to gridSelect firstname, lastname From customersWhere firstname = ‘robert’

Limiting a Search: The Use of WHERE

Limiting Number of Rows Returned

SET ROWCOUNT n totally separate statement from the SELECT

statement and can in fact be used with other statements within T-SQL.

limit or reset the number of rows that will be processed for the session in which the statement is executed.

To reset the session so that all rows are taken into consideration, you would set the ROWCOUNT number to 0.

Limiting the Output via ROWCOUNT

Using Transactions

We face situations where more than one data modification has to take place and succeed, and the first one succeeds but a subsequent modification fails.

Transactions allow us to revert to a consistent state after such events occur.

Using Transactions

Using Transactions -Rollback

Using Transactions -Rollback

Deleting Data

Deleting Data

Use transactions for any type of table modification in your application. Imagine that you’re at the ATM and you are

transferring money from your savings account to your checking account. During that process, a transaction built up of many

actions is used to make sure your money doesn’t credit one system and not the other.

If an error occurs, the entire transaction will roll back, and no money will move between the accounts.

Deleting Data using Transaction

Deleting Data using Transaction

Deleting Data using Transaction

Deleting Data using Transaction

top related