Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
67 views

SQL Server

Every variable, literal, constant, property, procedure argument, and procedure return value has a data type. A declared data type is specified as part of the declaration of a programming element. Defining data types for all your variables is known as strong typing.

Uploaded by

chandrav
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

SQL Server

Every variable, literal, constant, property, procedure argument, and procedure return value has a data type. A declared data type is specified as part of the declaration of a programming element. Defining data types for all your variables is known as strong typing.

Uploaded by

chandrav
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Data Types:

The data type of a programming element refers to what kind of data it can hold and how
that data is stored. Data types apply to all values that can be stored in computer memory
or participate in the evaluation of an expression. Every variable, literal, constant,
property, procedure argument, and procedure return value has a data type.

A declared data type is specified as part of the declaration of a programming element.


Unless you use typeless programming, you must declare data types for all your
programming elements.

A data type in a programming language is a set of data with values having predefined
characteristics.

Languages that leave little room for programmers to define their own data types are said
to be strongly-typed languages.

Value Types and Reference Types

A data type is a value type if it holds the data within its own memory allocation. A
reference type contains a pointer to another memory location that holds the data.

Value types include:

• All numeric data types


• Boolean, Char, and Date
• All structures, even if their members are reference types
• 1Enumerations, since their underlying type is always Byte, Short, Integer, or
Long

Reference types include:

• String
• All arrays, even if their elements are value types
• Class types, such as Form
• Delegates

You can assign either a reference type or a value type to a variable of the Object data
type. An Object variable always holds a pointer to the data, never the data itself.
However, if you assign a value type to an Object variable, it behaves as if it holds its
own data.

You can find out whether an Object variable is acting as a reference type or a value type
by passing it to the IsReference method on the Information class in the
Microsoft.VisualBasic namespace. Microsoft.VisualBasic.Information.IsReference
returns True if the content of the Object variable represents a reference type.

Efficient Use of Data Types

Specifying data types for all your variables is known as strong typing. Using strong
typing has several advantages:

• It enables IntelliSense™ support for your variables. This allows you to see their
properties and other members as you type in the code.
• It allows the compiler to perform type checking. This catches statements that can
fail at run time due to errors such as overflow. It also catches calls to methods on
objects that do not support them.
• It results in faster execution of your code.

Assigning a data type to an object defines four attributes of the object:

• The kind of data contained by the object.


• The length or size of the stored value.
• The precision of the number (numeric data types only).
• The scale of the number (numeric data types only).

The cursor data type is the only system data type that cannot be assigned to a table
column. It can be used only with variables and stored procedure parameters.

Two kinds of user-defined data types can also be created:

• Alias data types are created from base data types. They provide a mechanism for
applying a name to a data type that is more descriptive of the types of values to be
held in the object. This can make it easier for a programmer or database
administrator to understand the intended use of any object defined with the data
type. For example:

-- Create a birthday datetype that allows nulls.


CREATE TYPE birthday
FROM datetime NULL
GO
-- Create a table using the new data type.
CREATE TABLE employee (emp_id char(5), emp_first_name char(30),
emp_last_name char(40), emp_birthday birthday)

For more information, see Working with Alias Data Types.


• CLR user-defined data types are based on data types created in managed code and
uploaded in a SQL Server assembly.

Decimal and numeric (Transact-SQL)

Numeric data types that have fixed precision and scale.

decimal[ (p[ ,s] )] and numeric[ (p[ ,s] )]

Fixed precision and scale numbers. When maximum precision is used, valid
values are from - 10^38 +1 through 10^38 - 1. The SQL-92 synonyms for
decimal are dec and dec(p, s). numeric is functionally equivalent to decimal.

p (precision)

The maximum total number of decimal digits that can be stored, both to the left
and to the right of the decimal point. The precision must be a value from 1
through the maximum precision of 38. The default precision is 18.

s (scale)

The maximum number of decimal digits that can be stored to the right of the
decimal point. Scale must be a value from 0 through p. Scale can be specified
only if precision is specified. The default scale is 0; therefore, 0 <= s <= p.
Maximum storage sizes vary, based on the precision.

Precision Storage bytes


1-9 5
10-19 9
20-28 13
29-38 17

You might also like