Pages

Monday, May 26, 2008

When to use dataset & data reader

When we need to work with few tables and most of the time you need to execute queries on these tables, you should go for the dataset.

Disconnected data access is suited most to read only instances and disconnected data access architecture is not designed to be used in the networked environment where multiple users are updating data simultaneously and each of them needs to be aware of current state of database at any time.


DataReader is more useful when you need to work with large number of tables, Database in non-uniform pattern and you need not execute the large no. of queries on few particular table.


It also depends on the nature of application. If multiple users are using the database and the database needs to be updated every time, you must not use the dataset. For this, .Net provides the connection oriented architecture.
But in the scenarios where instant update of database is not required, dataset provides optimal performance by making the changes locally and connecting to database later to update a whole batch of data. This also reduces the network bandwidth if the database is accessed through network.

Sunday, May 4, 2008

Drop un-named constraint

declare @name nvarchar(32),
@sql nvarchar(1000)

-- find constraint name
select @name = O.name
from sysobjects AS O
left join sysobjects AS T
on O.parent_obj = T.id
where isnull(objectproperty(O.id,'IsMSShipped'),1) = 0
and O.name not like '%dtproper%'
and O.name not like 'dt[_]%'
and T.name = 'MyTable'
and O.name like 'My Column%'

-- delete if found
if not @name is null
begin
select @sql = 'ALTER TABLE [MyTable] DROP CONSTRAINT [' + @name + ']'
execute sp_executesql @sql
end

-- do your ALTER TABLE here

-- replace the constraint
select @sql = 'ALTER TABLE [MyTable] ADD CONSTRAINT [' + @name + '] DEFAULT (0) FOR [MyColumn]'
execute sp_executesql @sql

Check for duplicate records

One of the very basic and common that i have seen in my mail box and in forums is what is the easiest way to find out duplicate records.

SELECT email,
COUNT(email) AS NumOccurrences
FROM users
GROUP BY email
HAVING ( COUNT(email) > 1 )

Saturday, May 3, 2008

Catch Error Functions

ERROR_NUMBER(): Returns a number associated with the error.
ERROR_SEVERITY(): Returns the severity of the error.
ERROR_STATE(): Returns the error state number
ERROR_PROCEDURE(): Returns the name of the stored procedure or trigger in which the error occurred.
ERROR_LINE(): Returns the line number inside the failing routine that caused the error.
ERROR_MESSAGE(): Returns the complete text of the error message. The text includes the values supplied for any substitutable parameters, such as lengths, object names, or times.

Try Catch Error handling

In SQL Server 2005 there is new era to Error handling. When i was working in Java i was thinkin when i am going to have try/catch concepts in SQL Server...Now we have it in SQL Server 2005. Try/Catch, similar to VB.Net error handling

BEGIN TRY
DECLARE @X INT
-- Divide by zero to generate Error
SET @X = 1/0
PRINT 'Command after error in TRY'
END TRY
BEGIN CATCH
PRINT 'Error Detected'
END CATCH
PRINT 'Command after TRY/CATCH blocks'

T-SQL Enhancements

Varchar(max), Varbinary(max)
In SQL 2000, fields over 8,000 characters used Text, Image but that involved a different programming style than smaller fields.

Now fields of type char, varchar, binary, varbinary can hold up to 2 GB storage with same programming style as small fields.