Thursday, February 5, 2009

Customize Table from string Data in Sqlserver

Hi,
Now let's to discus how we can create a customize Table in Sqlserver.
while my work i face a problem of filterization of employee code.I have to filter some record based on the employee code.
I was passing multiple employeecode to the procedure which datatype is bigint.
so passed the employeecode as a varchar to the procedure by separating ',' .
so let's see how i solve this problem.
for example :
EmployeeCodes : 1,2,3,4

create Procedure USP_Filter_Employees
@EmpCode Varchar(100)
As
DECLARE @idoc int
set @
EmpCode ='< col > < r > '+@EmpCode
select @EmpCode =replace (@EmpCode ,',' ,'</r ><r >' )+'<r></col>'

EXEC sp_xml_preparedocument @idoc OUTPUT, @EmpCode
select * from EmployeeDetails where EmployeeCode in
(
SELECT cast(cast (text as varchar) as bigint)FROM OPENXML (@idoc, '/col/r',3) where text is not null
)
End