Vous êtes sur la page 1sur 4

SQL Server

Usando convenciones de nombres.


Es conveniente nombrar las variables de modo que reflejen el tipo de datos que contienen.
Tipo de dato
int smallint tinyint float real numeric decimal money smallmoney bit datetime smalldatetime char varchar nvarchar binary varbinary text ntext image cursor

Prefij o
int ins iny flt rel num dec mny mns bit dtm dts chr vch nvc bny bnv txt ntx img cur

Ejemplo
@intObjectId @insCounter @inyQuantity @fltValue @relInterest @numMass @decVolume @mnyTotal @mnsSalary @bitTerminated @dtmBirth @dtsBeginAccount @chrLastName @vchAddress1 @nvcFirstName @bnyFlags @bnvData @txtMemo @ntxNotes @imgPicture @curTableNames @uidIdentifier

uniqueidentifier uid

Recomendaciones: Las variables locales deberan tener un prefijo de tres letras minsculas. Trate de evitar el uso de smbolos - tales como # (pound o hash), el $ (dollar), o el _ (underscore) en cualquiera de sus nombres de variables. Recuerde que las variables locales deben comenzar con el smbolo @ (at). Convenciones de nombres de objetos.
Object Table Column View Rules Defaults User-defined data types Index (clustered) Index (nonclustered) Primary key (clustered) Primary key (nonclustered) Foreign key Trigger Prefix None None vw rl df dt ic in pc pn fk tr Example SalesReps AuthorId vwContractAuthors rlZipCheck dfStandardQuantity dtAddressLine icAuthorFullName inClientStateCity pcCustomerId pnStateLookupId fkRepCompanyId trStoreDelete

Pgina 1 de 4

SQL Server

Cursor

cr

crTables

Nota: evite el uso del prefijo sp_ en los nombres de los procedimientos almacenados.

Motivos por los cuales se debe evitar la utilizacin del prefijo sp_ para nombrar los stored procedures
Si las primeras tres letras del stored procedure son sp_, SQL Server primero busca en la base master por el procedimiento. Si el procedimiento no se encuentra, SQL Server busca en la base master asumiendo que el nombre del dueo es dbo. Finalmente, luego de buscarlo ah, lo busca en la base local asumiendo que el propietario es el usuario actualmente logueado. Esto posibilita la creacin de stored procedures que pueden ser accedidos en la base master cuando usted esta usando cualquier otra base en el servidor. Although this might not sound like a big deal, SQL Server processes stored procedures with the sp_ prefix in a special way. Not only does SQL Server handle this prefix in a special way, so do external tools that you might use. One of those tools is ERwin, a frequently used data modeling tool. One of the most powerful features of this tool is the capability to reverse engineer any database to which you have access. ERwin, like other tools, looks at stored procedures with the sp_ prefix as system stored procedures and ignores them completely. This can make life pretty difficult when working with these tools.

Pgina 2 de 4

SQL Server

Parte II

CHAPTER 5 Creating and Altering Stored Procedures


Using the LIKE Keyword
The LIKE keyword is used in conjunction with string data. LIKE enables you to search string columns for a certain pattern. This option enables you to use wildcard searches to find the data you are looking for. When searching with the LIKE keyword, any specific characters you search for must match exactly, but wildcard characters can be anything. Table 5.3 lists the wildcard characters you can use in conjunction with the LIKE keyword. Table 5.3 Wildcard Characters Character
%

Description

Example

[]

[^]

Matches any string from 0 WHERE au_lname LIKE M% retrieves out of the table rows to any length where the authors last name starts with the character M. WHERE au_fname LIKE _ean Matches any single retrieves out of the table all rows character where the authors first name begins with any letter followed by the three letters ean.For example,Dean, Sean, and Jean. Matches a specified range WHERE phone LIKE [09]19%retrieves all rows where the of characters or a set of authors phone number starts with characters 019, 119, 219, and so on. WHERE au_fname LIKE[^ABER] Matches any characters that fall outside a specified %retrieves all rows where the range or set of characters authors first name does not start with the characters A, B, E,or R.

Using Cursors There are five steps to working with cursors in SQL Server: Cursor Step
DECLARE

Explanation

This process tells SQL Server that you are going to create a specific type of cursor. During the declaration, you specify a name for the cursor and SELECT statement, which will make up the rows through which the cursor loops. OPENthe cursor During the open, the object is actually created and filled. Perform FETCH statements A fetch actually returns rows from the cursor so that they can
Pgina 3 de 4

the cursor

SQL Server

Parte II

the cursor DEALLOCATE the cursor


CLOSE

be used by the application. Clears out the cursor. Removes all references to the cursor from memory.

Pgina 4 de 4

Vous aimerez peut-être aussi