Solving development problems  |  About this blog

Archive for the ‘ms sql server’ tag

Transact SQL paging Store Procedure – LINQ Skip Take alternative

This Transact-SQL Store Procedure allows you to do paging on your SQL table by providing only two parameters:

  • Current page
  • Record to display per page

It returns chosen records and also total count as output parameter if you need to display this information or calculate how many pages you have in paging control.

CREATE PROCEDURE [dbo].[YourProcedureName]
  @CurrentPage int,
  @RecordsPerPage int,
  @Count int OUTPUT
AS
BEGIN
  -- The number of rows affected by the different commands
  -- does not interest the application, so turn NOCOUNT ON
  SET NOCOUNT ON

  -- Determine the first record and last record
  DECLARE @FirstRecord int, @LastRecord int

  SELECT @FirstRecord = (@CurrentPage - 1) * @RecordsPerPage
  SELECT @LastRecord = (@CurrentPage * @RecordsPerPage + 1)
  SET @Count = (SELECT COUNT(*) FROM YourTable);

  WITH TempResult as
  (
    SELECT	ROW_NUMBER() OVER(ORDER BY Field1 DESC) as RowNumber,
            Field1,
            Field2
    FROM    YourTable
  )
  SELECT  TOP (@LastRecord - 1) *
  FROM    TempResult
  WHERE   RowNumber > @FirstRecord AND
          RowNumber < @LastRecord

  -- Turn NOCOUNT back OFF
  SET NOCOUNT OFF
END

Written by Avivo

April 25th, 2011 at 9:03 am

String or binary data would be truncated. The statement has been terminated.

If this is not a problem for you that some long string will be truncated and you don’t want SQL to send you this error just use at the beginning of your StoreProcedure this statement:

SET ANSI_WARNINGS OFF

Remove items from List using LINQ

Typical scenario: you got your list of items from database but you want to remove some items from it (items that match additional criterion).

It is simple as this (i.e. assuming that your list is named yourItems):

yourItems.RemoveAll(x => x.ItemPropertyId != null);

Written by Avivo

November 24th, 2010 at 7:13 pm

Database cannot be upgraded because it is read-only or has read-only files. Make the database or files writeable, and rerun recovery.

If you have faced this problem when trying to attach your MSSQL database just copy MDF and LDF file to some directory where MS SQL service has full rights. It is usually something like this

c:\Program Files (x86)\Microsoft SQL Server\MSSQL.1\MSSQL\Backup

Regular Expression Replace in SQL 2005 (via the CLR)

Excellent post for achieving great SQL productivity.

I had to do some data clean up the other day, and really needed some regular expression replacements to do the job.

Since .NET has a great RegularExpressions namespace, and since SQL 2005 allows you to integrate .NET CLR functions in your T-SQL code, I thought I’d go ahead and experiment with creating a RegExReplace() function.

I am not so sure that I recommend using a function like this in production (there’s lots of pros and cons of CLR integration in SQL databases), but for data cleaning or quick tasks or just learning how to use new features or technology, it is very interesting and easy to do. All you need is a SQL Server 2005 database (Express is fine) and Visual Studio 2005.

Open up Visual Studio 2005 and create a new SQL Server Project, and after giving it a name and location, you will be prompted to connect to the SQL Server 2005 database in which you’d like to add your code.

Once the project is created, choose Project->Add User Defined Function, and name the .cs file anything you like, such as “RegExFunction.cs”.

Once the file has been added to your project, open it up and paste in the following code (changes made to the original template are in bold):

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Text.RegularExpressions;

public partial class UserDefinedFunctions
{
	[Microsoft.SqlServer.Server.SqlFunction(IsDeterministic=true,IsPrecise=true)]
	public static SqlString RegExReplace(SqlString expression,
                 SqlString pattern, SqlString replace)
	{
	  if (expression.IsNull || pattern.IsNull || replace.IsNull)
	  	return SqlString.Null;

	  Regex r = new Regex(pattern.ToString());

	  return new SqlString(r.Replace(expression.ToString(), replace.ToString()));
	}
};

It’s really quite simple; within the class definition, just define public static methods that accept and return SQLTypes, and if those methods are marked with the SqlFunction attribute, when deployed they become available in your database code as T-SQL User-Defined Functions! Quite cool.

In this example, our function is accepting 3 SQLString parameters, and if any are null, we return null. If they are all legit, we construct a RegEx object from the pattern passed in, do the replace, and return the result. Note that this will not be especially efficient, since the RegEx object is created and destroyed for each call, but it does work and it is interesting at the very least to play around with. You might also want to experiment with other options, such as ignoring whitespace or case sensitivity, provided by the RegEx class. This particular code is very basic, and doesn’t handle error checking or anything like that, you may wish to make improvements or optimizations in your own implementation.

Now that your code is ready to go, choose Build->Deploy Solution. If all goes well, your assembly and new function have been deployed to your SQL database!

There is one final thing you must do before you can use the function, and that is configure your server to allow CLR code to execute, if it hasn’t been configured already. To do this, you must execute the following T-SQL statement:

--ENABLE CLR FUNCTIONS AND PROCEDURES FROM C#
sp_configure 'clr enabled',1
GO
RECONFIGURE
GO

Once that is complete, you can now use your new function like any other User Defined T-SQL function. For example,

select dbo.RegExReplace('Remove1All3Letters7','[a-zA-Z]','')

-------------------------
137

(1 row(s) affected)

Now you can do a standard Regular Expression Replacement within your database directly, for example as an UPDATE:

UPDATE MessyTable
SET MessyColumn = dbo.RegExReplace(MessyColumn, ... , ....)
WHERE ...

Here’s my two cents on using CLR code in a database: If the code is purely a generic function or tool that has nothing specific to do with your data, and it fits and works logically in a database querying language, and there is no way to efficiently implement that code in T-SQL, then it may be worthwhile to implement that function via the CLR. This is a pretty good example. A bad example would be a .NET function that returns a CustomerName when passed a customerID, or something along those lines. That’s just my take on things, for what it’s worth.

So, use wisely and have fun!