Saturday, November 14, 2009

Get all employees have birthday this week

SELECT EmployeeID, EmployeeName, Email, BirthDate
FROM Employee
WHERE DATEDIFF(w, BirthDate, GETDATE()) = 0

Get top 10 customers by revenue

Using TOP keyword.

SELECT TOP 10 CustomerID, CustomerName, Revenue
FROM dbo.Customer
ORDER BY Revenue DESC

Get all customers established this month

SELECT CustomerID, CustomerName, EstablishedDate, Email, Address
FROM dbo.Customer
WHERE DATEDIFF(m, EstablishedDate, GETDATE()) = 0

Try/Catch in T-SQL

Applied from SQL Server 2005

BEGIN TRY
BEGIN TRAN
--Your T-SQL code here
COMMIT TRAN
END TRY
BEGIN CATCH
DECLARE @ErrorMessage NVARCHAR(4000);
SELECT @ErrorMessage = ERROR_MESSAGE();
ROLLBACK TRAN;
RAISERROR(@ErrorMessage, 16, 1);
END CATCH

Using cursor

DECLARE Cur CURSOR FOR
SELECT AccountID, AccountName
FROM Account

OPEN Cur
FETCH FROM Cur
INTO @AccountID, @AccountName

WHILE @@FETCH_STATUS = 0
BEGIN
--Do your tasks here
FETCH FROM Cur
INTO @AccountID, @AcountName
END

CLOSE Cur
DEALLOCATE Cur

Friday, November 13, 2009

A six-part series on getting the most out of SharePoint Designer 2007

As a DBA, I think you also need some knowledge about SharePoint and SharePoint Designer so that you can use it to modify your SharePoint site when necessary. You can read this article for more detail A six-part series on getting the most out of SharePoint Designer 2007.

Monday, November 9, 2009

Delete all foreign keys in a database


DECLARE @Table VARCHAR(255)
DECLARE @ForeignKey VARCHAR(255)
DECLARE @STRSQL VARCHAR(8000)

DECLARE Cur CURSOR FOR
select t1.name as [Table], t2.name as [ForeignKey]
from sysobjects t1 inner join
sysobjects t2 on t1.id = t2.parent_obj
where t1.xtype = 'U' and t2.xtype = 'F'
OPEN Cur
FETCH FROM Cur
INTO @Table, @ForeignKey
WHILE @@FETCH_STATUS = 0
BEGIN
SET @STRSQL = 'ALTER TABLE ' + @Table +
' DROP CONSTRAINT ' + @ForeignKey
EXECUTE(@STRSQL)

FETCH FROM Cur
INTO @Table, @ForeignKey
END
CLOSE Cur
DEALLOCATE Cur

Get random record


SELECT TOP 1 *
FROM YourTable
ORDER BY NEWID()

Friday, October 9, 2009

Get table script

You can use this function dbms_metadata.get_ddl to generate the create script of your table.
SELECT dbms_metadata.get_ddl('TABLE', 'Your table', 'Your owner')
FROM dual;

Tuesday, October 6, 2009

Active Directory Maximum Limits - Scalability

Do you know the limitation of Active Directory? If not, please read this article Active Directory Maximum Limits - Scalability