Posts

Types of User Defined Functions

Types of User Defined Functions There are three different types of User Defined Functions. Each type refers to the data being returned by the function. Scalar functions return a single value. In Line Table functions return a single table variable that was created by a select statement. The final UDF is a Multi-statement Table Function. This function returns a table variable whose structure was created by hand, similar to a Create Table statement. It is useful when complex data manipulation inside the function is required. Scalar UDFs Our first User Defined Function will accept a date time, and return only the date portion. Scalar functions return a value. From inside Query Analyzer, enter:   CREATE FUNCTION dbo.DateOnly(@InDateTime datetime) RETURNS varchar(10) AS BEGIN         DECLARE @MyOutput varchar(10)         SET @MyOutput = CONVERT ( varchar(10) ,@InDateTime,101)   ...

Advantages and Disadvantages of User Defined Functions

Advantages of User Defined Functions Before SQL 2000, User Defined Functions (UDFs), were not available. Stored Procedures were often used in their place. When advantages or disadvantages of User Defined Functions are discussed, the comparison is usually to Stored Procedures. One of the advantages of User Defined Functions over Stored Procedures, is the fact that a UDF can be used in a Select, Where, or Case statement. They also can be used to create joins. In addition, User Defined Functions are simpler to invoke than Stored Procedures from inside another SQL statement. Disadvantages of User Defined Functions User Defined Functions cannot be used to modify base table information. The DML statements INSERT, UPDATE, and DELETE cannot be used on base tables. Another disadvantage is that SQL functions that return non-deterministic values are not allowed to be called from inside User Defined Functions. GETDATE is an example of a non-deterministic function. Every time th...

What are the SQL Server system databases and can you outline the general functionality of each database?

What are the SQL Server system databases and can you outline the general functionality of each database? Master - Database responsible for SQL Server instance related data.  You can also think of this database corresponding to the Windows SQL Server service account. Resource - Database responsible for SQL Server system objects.  This database was introduced in SQL Server 2005 and is intended to ease the upgrade and rollback of SQL Server system objects. Model - Template database for the creation of new user defined databases. MSDB - Database responsible for SQL Server Agent related data such as Jobs, Alerts, Operators, etc. TempDB - Temporary database to store temporary tables (#temptable or ##temptale), table variables, cursors, work tables, row versioning, create or rebuild indexes sorted in TempDB, etc.  Each time the SQL Server instance is restarted all objects in this database are destroyed, so permanent objects cannot be created in this databa...

System Databases Do's and Don'ts

System Databases Do's and Don'ts Data Access - Based on the version of SQL Server query only the recommended objects.  In general the system database objects are being deprecated to a set of views, so be sure all of your scripts are accessing the right objects.  If not, you are going to have a big project in the future to convert all of your scripts. Changing Objects - Do not change system objects.  In SQL Server 2005 all of the database objects have been moved to the Resource database which stores the definition for the system objects and can be updated via new SQL Server releases independent of the data. New Objects - Creating objects in the system databases is not recommended.  If you have objects that are needed for the instance i.e. administrative items, just create a separate DBA database to store these objects. Sneaking a Peak - Up to this point, all of the T-SQL code for the tables, views, stored procedures, functions, etc. has been ...

SQL Server System Databases

SQL Server System Databases Master Database Purpose - Core system database to manage the SQL Server instance.  In SQL Server 2005, the Master database is the logical repository for the system objects residing in the sys schema.  In SQL Server 2000 and previous editions of SQL Server, the Master database physically stored all of the system objects. Prominent Functionality Per instance configurations Databases residing on the instance Files for each database Logins Linked\Remote servers Endpoints Additional Information The first database in the SQL Server startup process In SQL Server 2005, needs to reside in the same directory as the Resource database Sources for Database Information - SQL Server 2000 to 2005 Crosswalk SQL 2000 to 2005 Crosswalk - Startup Parameters Resource Database Purpose - The Resource database is responsible for physically storing all of the SQL Server 2005 system objects. This database has been created to impr...

SQL Text vs VARCHAR

When to use VARCHAR and When to use TEXT TEXT is used for large pieces of string data. If the length of the field exceeed a certain threshold, the text is stored out of row. VARCHAR is always stored in row and has a limit of 8000 characters. If you try to create a VARCHAR(x) , where x > 8000 , you get an error: Server: Msg 131, Level 15, State 3, Line 1 The size () given to the type ‘varchar’ exceeds the maximum allowed for any data type (8000) These length limitations do not concern VARCHAR(MAX) in SQL Server 2005 , which may be stored out of row, just like TEXT . Note that MAX is not a kind of constant here, VARCHAR and VARCHAR(MAX) are very different types, the latter being very close to TEXT . In prior versions of SQL Server you could not access the TEXT directly, you only could get a TEXTPTR and use it in READTEXT and WRITETEXT functions. In SQL Server 2005 you can directly access TEXT columns (though you still need an explicit cast...

SQL SERVER – ACID (Atomicity, Consistency, Isolation, Durability)

ACID (an acronymn for Atomicity Consistency Isolation Durability) is a concept that Database Professionals generally look for when evaluating databases and application architectures. For a reliable database all this four attributes should be achieved. Atomicity is an all-or-none proposition. Consistency guarantees that a transaction never leaves your database in a half-finished state. Isolation keeps transactions separated from each other until they’re finished. Durability guarantees that the database will keep track of pending changes in such a way that the server can recover from an abnormal termination. Above four rules are very important for any developers dealing with databases. Reference : Pinal Dave ( http://blog.SQLAuthority.com )