Archive for the ‘Uncategorized’ Category

h1

Retrieving Unique Values from a DataTable in .NET

March 25, 2009

The easiest way to return a new table that has only the unique values from the original table is as follows:

DataTable distinctTable = originalTable.DefaultView.ToTable(true, “FieldName”);
Thus “distinctTable” will contain unique values!!! :) Have fun!

h1

Using Variables in T-SQL Statements that accept only constant values

December 31, 2008

Developers usually come across a situation wherein they would want to use a variable in a T-SQL Statement which accepts only constant values.

For example, if we try to assign a variable to a Select Top <@variable_name> statement, we would get an error like:

Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ‘@variable_name’

There is a work around this problem though. The below stored procedure  solves it:

CREATE PROCEDURE TEST
@TestInt INT

AS

BEGIN

DECLARE @SqlQuery NVARCHAR(4000);

SET @SqlQuery = ‘SELECT TOP ‘+CAST(@TestInt as CHAR)+’* FROM TABLE_TEST’;

EXEC(@SqlQuery)

END