You are on page 1of 30

Transact – SQL Language

TCS Internal 1
Objective

To be able to understand Transact SQL, the various data types,


operators and functions supported by it and how to execute
queries..

TCS Internal 2
Scope

 Transact – SQL Overview


 Referring to objects
 SQL’s basic objects
 Data types
 Predicates
 Functions
 Scalar Operator
 Global Variables

TCS Internal 3
Transact – SQL Overview

 Transact-SQL is central to the use of Microsoft SQL Server.

 All applications that communicate with SQL Server do so


by sending Transact-SQL statements to the server,
regardless of an application's user interface.

TCS Internal 4
Referring to Objects

 [
    server_name. [database_name]. [owner_name].
    | database_name. [owner_name].
    | owner_name.
    ]
]
object_name
 where,
 server_name specifies a linked server name or remote
server name.
 database_name specifies the name of a Microsoft SQL
Server database when the object resides in a SQL
Server database. It specifies an OLE DB catalog when
the object is in a linked server.

TCS Internal 5
Referring to Objects

 owner_name specifies the user that owns the


object if the object is in a SQL Server database. It
specifies an OLE DB schema name when the object
is in a linked server.
 Object_name refers to the name of the object.
The valid formats of object names are:
– server.database.owner.object
– server.database..object
– server..owner.object
– server...object
– database.owner.object
– database..object
– owner.object
– object
TCS Internal 6
SQL’s Basic Objects

Transact – SQL has the same basic feature as other


common programming languages.

 Constants or literal values


 Delimiters
 Identifiers
 Reserved keyword.

TCS Internal 7
Constants or literal values

 A literal value is an alphanumerical, hexadecimal , or numeric


constant.

 A string constant contains one or more characters of the SQL


Server character set enclosed in two single straight quotation
marks or double straight quotation marks.

 Hexadecimal constant begins with the character “0x”.

Examples:
‘sqlserver2005’ – String constant
0x53678CDF – Hexadecimal constant
130 – Numeric constant

TCS Internal 8
Delimiters or Comments

 There are different ways to specify a comment in a Transact –


SQL statement.

 The pair of character /* and */ mark the enclosed text as a


comment.

 The character – (two hyphens ) indicate that the remainder of the


current line is a comment.

Examples:
/* Employee Table */
-- Inserting the records

TCS Internal 9
Identifiers

 An identifier refers to the names of objects such as tables, views, columns,


databases, and indices.

 The first character can be a letter from the Unicode character set. This includes the
standard US English a-z and A-Z characters as well as foreign letters

 Identifiers can be up to 128 characters long, except for the names of local
temporary tables, which can be upto 116 characters long.

 Identifiers cannot be a SQL Server reserved word in either upper or lower case.

 Identifiers cannot contain embedded spaces or special characters other than those
specified above.

TCS Internal 10
Reserved Keywords

 The reserved words are reserved for the use of SQL Server
and should not be used for the names of objects in a
database.

 The SQL Server Books Online contains an extensive list of


reserved words.

 The Query Analyzer can be used to check whether a


particular word is a keyword.

TCS Internal 11
Data Types

 Data types define the types of data contained by data objects, such as
columns, variables, and parameters.

 The data type is the first characteristic that is defined for the column of a
table.

 The data type of a column controls the type of information that can be
stored within the column

 Data types can be categorized into:

 Numeric data types


 String data types
 Data types for date and time
 Derived data types
 Miscellaneous data types
 User-defined data types

TCS Internal 12
Numeric Data Types

Numeric data types are used to represent numbers. Transact–SQL


uses the following numeric data types.
 Int – Represents integer values, which can be stored in 4 bytes.
 Smallint - Represents integer values, which can be stored in 2 bytes.
 Tinyint - Represents non-negative integer values, which can be stored in 1 byte.
 BigInt - Represents integer values, which can be stored in 8 bytes.
 Money - Represents monetary values

TCS Internal 13
String Data Types

There are three types of string data types : character, binary and bit
string.
Character string data types are:

 Char [n]
 Text [n]
 Ntext [n]

 Binary string data types are:

 Binary [n]
 Image [n]

TCS Internal 14
New Data types in SQL 2005

SQL Server 2005 introduces three new data types concerning LOB’s
(Large Objects)
The data types are:

 VARCHAR(MAX)
 NVARCHAR(MAX)
 VARBINARY(MAX)

TCS Internal 15
Date and/or Time data types

SQL Server 2005 supports the following date and time data types.

The data types are:

 DATETIME
 SMALLDATETIME

TCS Internal 16
Derived data types

SQL Server 2005 supports two data types that can be derived from
simple data types.
The data types are:

 TIMESTAMP
 SYSNAME

TCS Internal 17
Miscellaneous data types

SQL Server 2005 supports several data types that do not belong to
any of the data type groups described previously.
The are as follows:

 CURSOR
 UNIQUEIDENTIFIER
 SQL_VARIANT
 TABLE
 XML

TCS Internal 18
User-Defined data types

 A user can define his own datatype, which can then be used as a datatype
for a storage structure such as a table column.

 A user-defined datatype is defined by the user for a specific database.

 It is defined in terms of system-supplied datatypes.

 Each user-defined datatype is added as a row in the system table called


systypes.

TCS Internal 19
Predicates

 A Predicates defines a logical condition being applied to rows ina


table. The common logical conditions with two values (true, false)
are extended in the SQL language by the third value (unknown).

The Transact - SQL language supports the following


predicates.

 All Relational operator.


 BETWEEN operator
 IN operator
 LIKE operator
 NULL operator
 ALL and ANY operator
 EXISTS function

TCS Internal 20
FUNCTIONS

 Functions take zero, one, or more input values and return a scalar
value or a tabular set of values.

 The Transact-SQL programming language provides following


functions:

 Aggregate functions
 Scalar functions.

TCS Internal 21
Aggregate Functions

 Aggregate functions are applied to a group of data values from


a column.
 Aggregate function always return a single value.

The Transact – SQL supports following functions

AVG,MAX,MIN,SUM,COUNT,COUNT_BIG

TCS Internal 22
Scalar Functions

 Scalar functions operates on a single value or list of values.

Scalar functions can be categorized as follows:

 Numeric Functions
 Date Functions
 String Functions
 Text/Image functions
 System functions

TCS Internal 23
Numeric Functions

 Numeric functions with in Transact-SQL language are


mathematical functions for modifying numeric values.

Numeric functions are as follows:

ABS, ACOS, ASIN, ATAN, COS, COT, DEGREES, EXP, FLOOR,LOG,


RAND,ROUND,SIGN,SIN,SQRT,SQUARE,TAN

TCS Internal 24
Date Functions

 Date functions calculate the respective date or time portion of


an expression or return the value from a time interval.

Date functions are as follows:

GETDATE,DATEPART,DATENAME,DATEDIFF,DATEADD

TCS Internal 25
String Functions

 String functions are used for manipulating data values in a


column, usually of a string data type.

String functions are as follows:

ASCII,CAST,CHAR,CONVERT,CHARINDEX,DIFFERENCE,LOWER,LT
RIM,NEWID,REPLACE,REVERSE,RIGHT,RTRIM,SOUNDEX,SPACE,S
TR,STUFF,SUBSTRING,UPPER

TCS Internal 26
System Functions

 System functions are designed for use with columns of the


text/image data types.

System functions are as follows:

COALESCE,COL_LENGTH,Col_NAME,DB_ID,HOST_ID,HOST_NAME
,INDEX_COL,ISNULL,OBJECT_ID,SUSER_SID,SUSER_SNAME,USE
R_NAME,USER,SYSTEM_USER,CURRENT_USER

TCS Internal 27
Text/Image Functions

 Text/Image functions provide extensive information about


database objects.

Text/Image functions are as follows:

PATINDEX,TEXTPTR,TEXTVALID

TCS Internal 28
Scalar Operators

 Scalar Operator are used for operations with scalar values.


 There are unary and binary arithmetic operator.
 Unary operators are + and –
 Binary operators are +,-,*,/ and %
 The bitwise operators for manipulating bit strings.

TCS Internal 29
Global Variables

 Global Variables are special system variables that can be used


as if they were scalar constants.

SQL supports following global variables.

@@CONNECTIONS,@@CPU_BUSY,@@IDLE,@@VERSION,@@
PROCID,@@SPID,@@TOTAL_READ

TCS Internal 30

You might also like