| |||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||
|
Friday, June 10, 2011
Miscellaneous Problems and Solutions
Thursday, May 7, 2009
Updating top n records
Sometimes we need to update top n records, try using following synatax:
update top (5) [aspnet_Membership] set IsLockedOut =1
update top (5) [aspnet_Membership] set IsLockedOut =1
Thursday, September 25, 2008
Synonyms
SQL Server 2005's synonyms allow you to give an alias to an already existing object. Realize the benefits of synonyms by using them as a layer of abstraction between the underlying objects and the synonym.Suppose we have a table named 'SalesHistoryOfITDepartmentYear2008'.So instead of writting queries using this long name we can make make a synonym for this table.
CREATE SYNONYM SalesHistoryFOR SalesData.dbo.SalesHistoryOfITDepartmentYear2008;
Lets understand the use of Synonym with example.Due to expanding business and scalability needs, you decide that it's a good idea to remove the SalesHistory table from your current database named Inventory and put it in a separate database named SalesData. There is a lot of code in the Inventory database that references the SalesHistory table. Most of this code is simple queries that only read data from the SalesHistory table. The project deadline is in the very near future, so it will be difficult to change all existing code to reference the table in the new database.So we can use a synonym to drastically reduce your development time.
Assume that you have already copied your SalesHistory table into the new SalesData database and have renamed the current SalesHistory table in the Inventory database. (I always prefer renaming a table that I intend to delete first. This gives me the opportunity to discover any errors on the system caused by removing a table.)
Once you rename the SalesHistory table, you can create a synonym that references to the SalesHistory table that you have placed in the new database. The script is below.
USE InventoryGOCREATE SYNONYM SalesHistoryFOR SalesData.dbo.SalesHistory;
This synonym has created a pointer to the SalesHistory table in the new SalesData database; however, because the synonym is in the Inventory database and is named SalesHistory, any queries or DML statements executed on the SalesHistory synonym are actually executed against the SalesHistory table in the SalesData database
CREATE SYNONYM SalesHistoryFOR SalesData.dbo.SalesHistoryOfITDepartmentYear2008;
Lets understand the use of Synonym with example.Due to expanding business and scalability needs, you decide that it's a good idea to remove the SalesHistory table from your current database named Inventory and put it in a separate database named SalesData. There is a lot of code in the Inventory database that references the SalesHistory table. Most of this code is simple queries that only read data from the SalesHistory table. The project deadline is in the very near future, so it will be difficult to change all existing code to reference the table in the new database.So we can use a synonym to drastically reduce your development time.
Assume that you have already copied your SalesHistory table into the new SalesData database and have renamed the current SalesHistory table in the Inventory database. (I always prefer renaming a table that I intend to delete first. This gives me the opportunity to discover any errors on the system caused by removing a table.)
Once you rename the SalesHistory table, you can create a synonym that references to the SalesHistory table that you have placed in the new database. The script is below.
USE InventoryGOCREATE SYNONYM SalesHistoryFOR SalesData.dbo.SalesHistory;
This synonym has created a pointer to the SalesHistory table in the new SalesData database; however, because the synonym is in the Inventory database and is named SalesHistory, any queries or DML statements executed on the SalesHistory synonym are actually executed against the SalesHistory table in the SalesData database
Synonyms
SQL Server 2005's synonyms allow you to give an alias to an already existing object. Realize the benefits of synonyms by using them as a layer of abstraction between the underlying objects and the synonym.Suppose we have a table named 'SalesHistoryOfITDepartmentYear2008'.So instead of writting queries using this long name we can make make a synonym for this table.
CREATE SYNONYM SalesHistoryFOR SalesData.dbo.SalesHistoryOfITDepartmentYear2008;
Lets understand the use of Synonym with example.Due to expanding business and scalability needs, you decide that it's a good idea to remove the SalesHistory table from your current database named Inventory and put it in a separate database named SalesData. There is a lot of code in the Inventory database that references the SalesHistory table. Most of this code is simple queries that only read data from the SalesHistory table. The project deadline is in the very near future, so it will be difficult to change all existing code to reference the table in the new database.So we can use a synonym to drastically reduce your development time.
Assume that you have already copied your SalesHistory table into the new SalesData database and have renamed the current SalesHistory table in the Inventory database. (I always prefer renaming a table that I intend to delete first. This gives me the opportunity to discover any errors on the system caused by removing a table.)
Once you rename the SalesHistory table, you can create a synonym that references to the SalesHistory table that you have placed in the new database. The script is below.
USE InventoryGOCREATE SYNONYM SalesHistoryFOR SalesData.dbo.SalesHistory;
This synonym has created a pointer to the SalesHistory table in the new SalesData database; however, because the synonym is in the Inventory database and is named SalesHistory, any queries or DML statements executed on the SalesHistory synonym are actually executed against the SalesHistory table in the SalesData database
CREATE SYNONYM SalesHistoryFOR SalesData.dbo.SalesHistoryOfITDepartmentYear2008;
Lets understand the use of Synonym with example.Due to expanding business and scalability needs, you decide that it's a good idea to remove the SalesHistory table from your current database named Inventory and put it in a separate database named SalesData. There is a lot of code in the Inventory database that references the SalesHistory table. Most of this code is simple queries that only read data from the SalesHistory table. The project deadline is in the very near future, so it will be difficult to change all existing code to reference the table in the new database.So we can use a synonym to drastically reduce your development time.
Assume that you have already copied your SalesHistory table into the new SalesData database and have renamed the current SalesHistory table in the Inventory database. (I always prefer renaming a table that I intend to delete first. This gives me the opportunity to discover any errors on the system caused by removing a table.)
Once you rename the SalesHistory table, you can create a synonym that references to the SalesHistory table that you have placed in the new database. The script is below.
USE InventoryGOCREATE SYNONYM SalesHistoryFOR SalesData.dbo.SalesHistory;
This synonym has created a pointer to the SalesHistory table in the new SalesData database; however, because the synonym is in the Inventory database and is named SalesHistory, any queries or DML statements executed on the SalesHistory synonym are actually executed against the SalesHistory table in the SalesData database
multi-criteria filtering without dynamic SQL
Sometimes we're developing applications that require performing searches against database tables in SQL. The search must enable using a set of optional parameters (up to 10 parameters for example) as the main search criteria (a null value indicates not to filter the data, and a non-null value indicate to filter the data by the underlying value). Some developers use complicated T/SQL statements (a handful of if and if/else statements) to implement this requirement, thus making the code illegible. Because I have to deal with this kind of problems, now I'd like to suggest you an alternative technique that I use in my solutions.
Let's supposed that we have the following table:create table CIS_CLIENT( ID int primary key, first_name varchar(50) not null,
last_name varchar(50) null,
case_officer_fk int,
branch_fk int
);
Now, our requirements indicate the option to look up for information by specifying any criteria value (the search parameters are based on the first name , last name, case officer of the client and the the branch office to whom it belongs to).
CREATE PROC st_Search_clients
(
@clientID int = null, @first_name varchar(50) not null,
@last_name null,
@case_officer_fk int,
@branch_fk int
)
as
BEGIN
SELECT * FROM cis_client client
WHERE
(@clientID is null OR @clientID = client.id) AND
(@first_name is null OR @first_name = client.first_name) AND
(@last_name is null OR @last_name = client.last_name ) AND
(@case_officer_fk is null OR @case_officer_fk = client.case_officer_fk) AND
(@branch_fk is null OR @branch_fk = client.branch_fk )
END
The same thing can also be achieved by COALESCE function in where clause :
client.id = COALESCE(@clientID, client.id) AND client.first_name = COALESCE(@first_name, client.first_name) AND client.last_name = COALESCE(@last_name, client.last_name) AND client.case_officer_fk = COALESCE(@case_officer_fk,client.case_officer_fk) AND client.branch_fk = COALESCE(@branch_fk, client.branch_fk)
Let's supposed that we have the following table:create table CIS_CLIENT( ID int primary key, first_name varchar(50) not null,
last_name varchar(50) null,
case_officer_fk int,
branch_fk int
);
Now, our requirements indicate the option to look up for information by specifying any criteria value (the search parameters are based on the first name , last name, case officer of the client and the the branch office to whom it belongs to).
CREATE PROC st_Search_clients
(
@clientID int = null, @first_name varchar(50) not null,
@last_name null,
@case_officer_fk int,
@branch_fk int
)
as
BEGIN
SELECT * FROM cis_client client
WHERE
(@clientID is null OR @clientID = client.id) AND
(@first_name is null OR @first_name = client.first_name) AND
(@last_name is null OR @last_name = client.last_name ) AND
(@case_officer_fk is null OR @case_officer_fk = client.case_officer_fk) AND
(@branch_fk is null OR @branch_fk = client.branch_fk )
END
The same thing can also be achieved by COALESCE function in where clause :
client.id = COALESCE(@clientID, client.id) AND client.first_name = COALESCE(@first_name, client.first_name) AND client.last_name = COALESCE(@last_name, client.last_name) AND client.case_officer_fk = COALESCE(@case_officer_fk,client.case_officer_fk) AND client.branch_fk = COALESCE(@branch_fk, client.branch_fk)
The hierarchical query and SQL Server 2005
We can create hierarchical queries using CTE
Here is the common example :
create table Employees
( id int primary key nonclustered, name varchar(50), boss int constraint FK_boss foreign key references employees(id)
)insert into employees values(1, 'Company CEO', null) insert into employees values(2, 'Manager 1', 1)
insert into employees values(3, 'Manager 2', 1)
insert into employees values(4, 'Manager 3', 1)
insert into employees values(5, 'Asst. Manager', 2)
insert into employees values(6, 'TeamLead1', 5)
insert into employees values(7, 'TeamLead2', 5)
insert into employees values(8, 'TeamMember1', 6)
insert into employees values(9, 'TeamMember2', 6)
insert into employees values(10, 'TeamMember3', 6)
insert into employees values(11, 'TeamMember4', 7)
insert into employees values(12, 'TeamMember5', 7) declare @employee_id as smallint
set @employee_id = 6
;with report(id, name)
AS
( SELECT id, name from employees where boss = @employee_id
union all
SELECT E.id, E.name from employees E JOIN report R on R.id = E.boss
)
Select * from report
Here is the common example :
create table Employees
( id int primary key nonclustered, name varchar(50), boss int constraint FK_boss foreign key references employees(id)
)insert into employees values(1, 'Company CEO', null) insert into employees values(2, 'Manager 1', 1)
insert into employees values(3, 'Manager 2', 1)
insert into employees values(4, 'Manager 3', 1)
insert into employees values(5, 'Asst. Manager', 2)
insert into employees values(6, 'TeamLead1', 5)
insert into employees values(7, 'TeamLead2', 5)
insert into employees values(8, 'TeamMember1', 6)
insert into employees values(9, 'TeamMember2', 6)
insert into employees values(10, 'TeamMember3', 6)
insert into employees values(11, 'TeamMember4', 7)
insert into employees values(12, 'TeamMember5', 7) declare @employee_id as smallint
set @employee_id = 6
;with report(id, name)
AS
( SELECT id, name from employees where boss = @employee_id
union all
SELECT E.id, E.name from employees E JOIN report R on R.id = E.boss
)
Select * from report
Subscribe to:
Comments (Atom)