Tuesday, 27 March 2007
Paging with MS SQL Server 2005
USE Patient_DB;
GO
WITH PatientDetail
AS
(
SELECT
Surname,
Forename,
Age,
ROW_NUMBER() OVER (order by Surname) AS 'RowNumber'
FROM Patient
)
SELECT * FROM
PatientDetail
WHERE
RowNumber between 1 and 10;
GO
The OVER clause is used to determine the partitioning and ordering of the intermediary result set before the ROW_NUMBER function is applied. The SELECT statement can be parameterized to retrieve data for the specified Range as given below.
USE Patient_DB;
GO
WITH PatientDetail
AS
(
SELECT
Surname,
Forename,
Age,
ROW_NUMBER() OVER (order by Surname) AS 'RowNumber'
FROM Patient
)
SELECT * FROM
PatientDetail
WHERE RowNumber between
@RowNumberFrom and @RowNumberTo;
GO
Monday, 26 March 2007
World’s Fastest Optical chip
Measured in another way, the chipset can transmit the equivalent of four million simultaneous telephone conversations. The company said that the optical transceiver can move data at up to eight times more quickly than the fastest existing optical chips -- up to 160 Gbps.
The optical transceiver chipset moves information as light signals, not as electrical signals, and IBM said it could be available by 2010.
the new optical chipset, only one-fifteenth the size of a dime, can be manufactured with high-volume techniques and so could result in low-cost products. They could be integrated into printed circuit boards for PCs or set-top boxes.
More details at
http://www.sci-tech-today.com/story.xhtml?story_id=12200CQ3ZX4E
Thursday, 22 February 2007
SQL Server 2005 Service Pack 2 Features
Microsoft has recently released the service pack 2 for SQL Server 2005.The service pack includes the following key enhancements
- Data Mining Add-ins for the 2007 Microsoft Office system enable data mining functionality from SQL Server Analysis Services (SSAS) to be used directly within Excel® 2007 and Visio® 2007.
- SQL Server Reporting Services (SSRS) compatibility with Microsoft Office Share Point® Server 2007 provides integration with the Report Center in Share Point, enabling the seamless consumption and management of SSRS reports within Share Point.
- SQL Server Analysis Services improvements for Excel 2007 and Excel Services relate to performance and functionality.
- Data compression (varDecimal) is an important feature for data warehousing scenarios, requiring less disk storage of decimal data and increasing overall performance.
- Manageability enhancements, based on customer feedback, provide management capabilities for database administrators such as improvements in database maintenance plans, enhanced management reports and a new copy database wizard.
- Management reports added to SQL Server Express Edition enable customers to get insights into the performance of their Express Edition and SQL Server Compact Edition databases.
- Interoperability improvements including Oracle support in the Report Builder feature enable customers to use its functionality on top of Oracle data sources. Customers also have access to SQL Server Reporting Services to build reports on top of Hyperion’s Essbase cubes.
Thursday, 11 January 2007
Changing Database Collation
- Restrict the Database access to Single User Mode
- Change the Database collation using the following syntax.
ALTER DATABASE dbname COLLATE [Replace Actual Collation]
GO
- Take the backup of all the Indexes and constraints of the tables with Character columns in the Database
- Drop all the Indexes and constraints (Primary Key, Foreign Key, Defaults etc)
- Change the Collation setting for the table columns. The below script can be used to generate the script that identifies the character columns and replace it with the new collation in the Database
SELECT 'ALTER TABLE ' + SYSOBJECTS.Name + ' ALTER COLUMN ' + SYSCOLUMNS.Name + ' ' +
SYSTYPES.name + '(' + RTRIM(CONVERT(CHAR,SYSCOLUMNS.length)) + ') ' + ' COLLATE [Replace Actual Collation]' + CASE ISNULLABLE WHEN 0 THEN 'NOT NULL' ELSE 'NULL' End +
CHAR(13) + ' GO'
FROM SYSCOLUMNS , SYSOBJECTS , SYSTYPES
WHERE SYSCOLUMNS.ID = SYSOBJECTS.ID
AND SYSOBJECTS.TYPE = 'U'
AND SYSTYPES.Xtype = SYSCOLUMNS.xtype
AND SYSCOLUMNS.COLLATION IS NOT NULL
GO
Wednesday, 3 January 2007
SQL Server Locking Modes
SQL Server supports acquires different locking modes depends on the type of operation we perform on the Data. The list of locking modes acquired by SQL Sever for each type of operation is given below.
Shared Lock
- Acquired for reading data.
- Other transactions can acquire shared lock on the same resources.
- No other transaction can modify data.
Exclusive Lock
- Acquired to modify data (INSERT, DELETE and MODIFY).
- No other transaction can modify or read data.
Update Lock
- Acquired to execute a data modification operation but first needs to search the table.
- No other transaction can acquire an update lock or an exclusive lock.
Schema Lock
- Modification lock - Acquired for DDL queries.
- Stability lock – Acquired when compiling queries.
Bulk Update Lock
- Acquired for performing bulk copy of data
Tuesday, 2 January 2007
SQL Server Locking Tips
SQL Server uses locking to support concurrency of data in Multi user environment .The locking can be applied to the Database resources such ROWS, INDEX, PAGE, EXTENT, TABLE or the Database itself. SQL Server automatically escalates row, key, or page locks to table locks as appropriate to protects system resources and increases efficiency. Locking at smaller granularity increases concurrency but create high overhead on the Database on the other hand Locking at larger granularity reduces overhead but expense in terms of concurrency.The best practice is to avoid lock escalation .The following tips can be useful to minimize locking
- Keep all Transact-SQL transactions as short as possible.
- Avoid interleaving reads and database changes in same transaction.
- Do all the conditional logic and variable assignment outside of a Transaction.
- Encapsulate all transactions within stored procedures.
- Avoid Transact-SQL statements inside the transaction that affect large numbers of rows at once.
- Although WHILE nesting transactions is perfectly legal avoid using inside the transactions.
- For lookup tables consider altering the default lock level for the table (Use SP_INDEXOPTION).
- Do not create temporary tables from within a stored procedure that is invoked by the INSERT INTO #temp EXECUTE statement.
Tuesday, 26 December 2006
ALTER TABLE SWITCH PARTITION FAILURE
The SWITCH TO Partition fails when we perform a swith from non-partitioned table to partitioned table even though the same constraint exists on the partitioned column for both the tables
Solution
The solution to the problem can be explained using the below example
/* Partition Function */
CREATE PARTITION FUNCTION [IdentiferPFN](int) AS RANGE LEFT FOR VALUES (100, 200, 300, 400)
/* Partition Scheme */
CREATE PARTITION SCHEME [IdentiferPFN] AS PARTITION [IdentiferPFN] TO ([FILEGROUP1])GO
/* Partition Table */
CREATE TABLE [dbo].[TestPartition]
( [PurchaseOrderID] [int] IDENTITY(1,1) NOT NULL, [EmployeeID] [int] NULL)
ON [IdentiferPFN]([EmployeeID])
GO
ALTER TABLE [dbo].[TestPartition] WITH CHECK ADD CONSTRAINT [CChk] CHECK (([employeeid]>=(0)))
GO
ALTER TABLE [dbo].[TestPartition] WITH CHECK ADD CONSTRAINT [CChk1] CHECK (([employeeid]<=(100)))
GO
/* Non Partition Table */
CREATE TABLE [dbo].[TestPartitionSwitch]
( [PurchaseOrderID] [int] IDENTITY(1,1) NOT NULL, [EmployeeID] [INT] NULL)
ON [FILEGROUP1]
GO
ALTER TABLE [dbo].[TestPartitionSwitch] WITH CHECK ADD CONSTRAINT [Chk] CHECK (([employeeid]>=(0)))
GO
ALTER TABLE [dbo].[TestPartitionSwitch] WITH CHECK ADD CONSTRAINT [Chk1] CHECK (([EMPLOYEEID]<=(100)))
GO
/* Swith the data from Partition table to non Parition table */
ALTER TABLE TestPartitionSwitchSWITCH TO TestPartition PARTITION 1
/* Swith the data back to Partition table */
ALTER TABLE TestPartitionSWITCH PARTITION 1 TO TestPartitionSwitch
This switch back statement will throw an error saying
"ALTER TABLE SWITCH statement failed. Check constraints or partition function of source table 'PARTITIONTEST.dbo.TestPartitionSwitch' allows values that are not allowed by check constraints or partition function on target table'PARTITIONTEST.dbo.TestPartition'."
This problem can be solved by modifying the constriaint to include the NOT NULL check on the partition column "EmployeeID" as below.
ALTER TABLE [dbo].[TestPartitionSwitch] WITH CHECK ADD CONSTRAINT [Chk] CHECK (([employeeid]>=(0) AND [employeeid] IS NOT NULL))
GO
ALTER TABLE [dbo].[TestPartitionSwitch] WITH CHECK ADD CONSTRAINT [Chk1] CHECK (([EMPLOYEEID]<=(100) AND [employeeid] IS NOT NULL))
GO
A NULL in partition column would satisfy the check constraint but fall outside of the target partition.Changing the nullablility of the columns or asserting not null in the constraint resolves the issue thats the trcik.