Thursday, September 15, 2011

Shrink SQL Server 2008 Database Log File Script

I found this script to shrink the database log file in a SQL Server 2008 database. This procedure has changed, since the BACKUP command does not work the same way as previous versions.

Use DatabaseName
GO
Alter Database DatabaseName Set Recovery Simple
GO
Alter Database DatabaseName Set Recovery Full
GO
DBCC SHRINKFILE ('LogFileName', 1)
GO

Monday, July 18, 2011

How to Find Tables without Indexes?

Simply run the following query in the Query Editor.

USE database_name;
GO
SELECT SCHEMA_NAME(schema_id) AS schema_name,name AS table_name
FROM sys.tables
WHERE OBJECTPROPERTY(OBJECT_ID,'IsIndexed') = 0
ORDER BY schema_name, table_name;
GO

Thursday, July 7, 2011

Server Side Paging

In the previous post I wrote about OFFSET and FETCH on SQL Denali.

Today I want to write a sample to use them in a real world:
Normally to do paging on server we do this:

USE AdventureWorks
GO
DECLARE @NumberOfRows INT = 10 -- Number of rows you want on a page
DECLARE @DesiredPageNumber INT = 5 -- Page number
SELECT *
FROM
(
SELECT RowNum = ROW_NUMBER() OVER (
ORDER BY EmployeeID), *
FROM HumanResources.Employee
) AS a
WHERE RowNum > (@NumberOfRows * (@DesiredPageNumber - 1))
AND RowNum <= (@NumberOfRows * (@DesiredPageNumber - 1)) + @NumberOfRows
ORDER BY EmployeeID


which simply by changing @DesiredPageNumber, we can display data from different pages, but using OFFSET and FETCH in SQL-#Denali make it really simpler:

USE AdventureWorks
GO
DECLARE @NumberOfRows INT = 10 -- Number of rows you want on a page
DECLARE @DesiredPageNumber INT = 5 -- Page number

SELECT *
FROM HumanResources.Employee
ORDER BY EmployeeID
OFFSET (@NumberOfRows * (@DesiredPageNumber - 1)) ROWS
FETCH NEXT @NumberOfRows ROWS ONLY;
GO


as you can see it's really simpler and makes the developer's life much easier and has better has better Performance too.




reference

Saturday, April 23, 2011

OFFSET and FETCH on SQL Server Denali

The OFFSET and FETCH clause of SQL Server Denali provides you an option to fetch only a page or a window of the results from the complete result set.

look at some samples:

USE AdventureWorks2008R2;
GO
-- Return all rows sorted by the column DepartmentID.
SELECT DepartmentID, Name, GroupName
FROM HumanResources.Department
ORDER BY DepartmentID;

-- Skip the first 5 rows from the sorted result set and return all remaining rows.
SELECT DepartmentID, Name, GroupName
FROM HumanResources.Department
ORDER BY DepartmentID OFFSET 5 ROWS;

-- Skip 0 rows and return only the first 10 rows from the sorted result set.
SELECT DepartmentID, Name, GroupName
FROM HumanResources.Department
ORDER BY DepartmentID
OFFSET 0 ROWS
FETCH NEXT 10 ROWS ONLY;

Wednesday, April 13, 2011

SQL IntelliSense

Sometime I'm creating a new table or adding a column in SQL Server 2008, but when SELECTing from it I got the red squiggly line

the reason is :There are cases where the local cache used by IntelliSense becomes stale. Refreshing the cache is easy but not necessarily obvious.

There are two ways to refresh the cache:

1) Go to Edit -> IntelliSense -> Refresh Local Cache and
2) Hit Ctrl+Shift+R

Monday, April 11, 2011

Rounded Corners and Shadows – Dialogs with CSS

Great post about making 'Rounded Corners and Shadows' using CSS, do not miss it.

link