Paging SQL Server 2005 Results

You can use the Row_Number() function. Its used as follows: SELECT Row_Number() OVER(ORDER BY UserName) As RowID, UserFirstName, UserLastName FROM Users From which it will yield a result set with a RowID field which you can use to page between. SELECT * FROM ( SELECT Row_Number() OVER(ORDER BY UserName) As RowID, UserFirstName, UserLastName FROM Users … Read more

Transaction Isolation Level Scopes

Run the following and see for yourself: CREATE PROCEDURE dbo.KeepsIsolation AS BEGIN PRINT ‘Inside sproc that does not change isolation level’; DBCC USEROPTIONS; END GO CREATE PROCEDURE dbo.ChangesIsolation AS BEGIN PRINT ‘Inside sproc that changes isolation level’; SET TRANSACTION ISOLATION LEVEL READ COMMITTED; DBCC USEROPTIONS; END GO SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; DBCC USEROPTIONS; … Read more

SQL Join on Nearest less than date

I believe this subquery will do it (not tested). select *, (select top 1 Discount from table2 where table2.Date <= t.Date order by table2.Date desc) as Discount from Table1 t Perhaps not the most performant however. Edit: Test code: create table #table1 ([date] datetime, val int) create table #table2 ([date] datetime, discount int) insert into … Read more

Reading a text file with SQL Server

Just discovered this: SELECT * FROM OPENROWSET(BULK N'<PATH_TO_FILE>’, SINGLE_CLOB) AS Contents It’ll pull in the contents of the file as varchar(max). Replace SINGLE_CLOB with: SINGLE_NCLOB for nvarchar(max) SINGLE_BLOB for varbinary(max) Thanks to http://www.mssqltips.com/sqlservertip/1643/using-openrowset-to-read-large-files-into-sql-server/ for this!

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)