SQL Server parse cvs

to parse a cvs string and return a table with “Val” as the column name.
=====

ALTER FUNCTION [dbo].[ParseCSV] (
@param varchar(8000),
@Delimeter varchar(10)
)
RETURNS @tblRes TABLE
(
-- Columns returned by the function
Val varchar(100)
)

AS begin

DECLARE @Val varchar(100)
DECLARE @StartPos int, @Length int

WHILE LEN(@param) > 0
BEGIN

SET @StartPos = CHARINDEX(@Delimeter, @param)
IF @StartPos < 0 SET @StartPos = 0 SET @Length = LEN(@param) - @StartPos - 1 IF @Length < 0 SET @Length = 0 IF @StartPos > 0
BEGIN
SET @Val = SUBSTRING(@param, 1, @StartPos - 1)
SET @param = SUBSTRING(@param, @StartPos + 1, LEN(@param) - @StartPos)
END
ELSE
BEGIN
set @param = ltrim(rtrim(@param))
SET @Val = @param
SET @param = ''
END
set @Val = Ltrim(Rtrim(@Val))
INSERT @tblRes (Val) VALUES(@Val)
END

--Show all records in the @tblRes table
--SELECT * FROM @tblRes

RETURN
end

Winows 7 sleep problem

Since I installed Kingston SSD, every time when I press the sleep button, Win 7 goes to sleep and wakes up almost instantly or in a couple of seconds
Solution: Device Manager, Nework Adapter, Properties, Power Management,
Uncheck “Allow this device to wake up…”
Do the same thing for all mouse and keyboard devices under “Human Interface Devices”.
It seems that Win 7 enables those devices to wake up your PC by default.

To see which devices are enabled for waking up your current configuration just run
powercfg -devicequery wake_armed
To see which device prevent win7 from entering sleep mode:
PowerCfg/requests
To see your energy usagae
powercfg/energy
To See what had Waken Up Windows Last
powercfg lastwake
from a command prompt window.

CLR in SQL Server

According to Microsoft, Use Transact-SQL when the code will mostly perform data access with little or no procedural logic. Use managed code for CPU-intensive functions and procedures that feature complex logic, or when you want to make use of the BCL of the .NET Framework.

1. Create SQL Stored Procedure in visual stuido
Project name: “FirstSqlClass.cs”; class name:”FirstSqlClr” and function name:”ListEmployees”

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;   
 
 
public partial class FirstSqlClr
{
    [Microsoft.SqlServer.Server.SqlProcedure]
 
    public static void ListEmployees(SqlString fLetter, SqlString orderBy, out SqlString outputParameter)
    {
        string sqlCmd = "";
        sqlCmd += " select c.LastName, c.FirstName, c.EmailAddress ";
        sqlCmd += " from Person.Contact as c inner join HumanResources.Employee as e on c.ContactID = e.ContactID ";
        sqlCmd += " where left(c.LastName, 1) = @fLetter";
        sqlCmd += " order by " + orderBy.ToString();        
 
        SqlContext.Pipe.Send("Stored Procedure started at: " + DateTime.Now.ToString() + "\n");
        using (SqlConnection conn = new SqlConnection("context connection = true"))
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand(sqlCmd, conn);
            cmd.Parameters.Add(new SqlParameter ("@fLetter", SqlDbType.NVarChar, 1));
            cmd.Parameters[0].Value = fLetter;
 
            SqlDataReader rdr = cmd.ExecuteReader();
            SqlContext.Pipe.Send(rdr);
        }
        outputParameter = "after some manipulation that has to be done by a procedure language.";
        SqlContext.Pipe.Send("Stored Procedure ended at: " + DateTime.Now.ToString() + "\n");
    }    
};

2. Set Target Framework to “.Net 2.0” SQL 2008 only support .Net 2.0. Comply to DLL.
3. Enable Clr in Sql Server:

sp_configure 'clr enabled', 1
go
reconfigure
go

4. Register the assembly in SQL Server Locate the Dll file and set Permission to “SAFE”.

use AdventureWorks
go
create assembly FirstSqlClrClass from
'C:\Users\kwu\Documents\visual studio 2010\projects\FirstSqlClrClass\FirstSqlClrClass\bin\Debug\FirstSqlClrClass.dll'
go

5. Create Stored Procedure

use AdventureWorks
go
create procedure FirstListEmployees(@fLetter Nvarchar(1), @OrderBy NVarchar(20), @outputParameter NVarchar(100) OUT) as
external name FirstSqlClrClass.FirstSqlClr.ListEmployees
go

The the SP can be used as regular T-Sql stored procedure.

use AdventureWorks
go
DECLARE @return_value int,
@outputParameter nvarchar(100)

EXEC @return_value = [dbo].[FirstListEmployees]
@fLetter = 'B',
@OrderBy = 'EmailAddress',
@outputParameter = @outputParameter OUTPUT

SELECT @outputParameter as N'@outputParameter'