Friday, July 3, 2009

Storeprocedure as Simple Query

begin try"+
" begin transaction"+
" IF(1="+intMode.ToString()+")"+
" begin"+
//" --Check Exists or Not"+
" IF (SELECT count(*)FROM Department_Master_Desktop WHERE Department_Name = '" + strDeptName + "')=0" +
" begin"+
//-------Insert Query------------
" insert into Department_Master_Desktop ([Department_Id],[Department_Name],[Company_Id],[IsDeleted])"+
" (select"+
" (select isnull(max(Department_Id),0)+1 from Department_Master_Desktop)"+
" ,'"+strDeptName+"',1,0)" +
" select 'Inserted' as 'Result'"+
" commit transaction"+
" end"+
//Department Already Exists
" else"+
" begin"+
//--Exists
" RAISERROR('Department already Exists!',16,1)"+
" end"+
" end"+
" else if(2="+intMode.ToString()+")"+//-------Update Process----
" begin "+
" IF (SELECT count(*)FROM Department_Master_Desktop WHERE Department_Name = '" + strDeptName + "' and Department_Id<>"+DeptId .ToString()+")=0" +
" begin"+
" update Department_Master_Desktop set Department_Name='"+strDeptName+"' where Department_Id="+DeptId.ToString()+
" commit transaction"+
" select 'Updated' as 'Result'"+
" end"+
" else"+//--Department Already Exists
" begin"+
//--Exists
" RAISERROR('Department already Exists!',16,1)"+

" end"+
" end "+
" else if(3="+intMode.ToString()+")"+//--Delete Operation
" begin"+
" delete Department_Master_Desktop where Department_Id="+DeptId.ToString()+
" commit transaction "+
" select 'Deleted' as 'Result'"+
" end "+
" else if(4="+intMode.ToString()+")"+//--Select Opration
" begin"+
" select Department_Id,Department_Name from Department_Master_Desktop" +
" commit transaction"+
" end"+
" else"+
" begin"+
" RAISERROR('No Operation Found!',16,1)"+
" end"+
" end try"+//--end try
" begin catch"+ //--start catch
" rollback transaction"+
" select ERROR_MESSAGE() as 'Error'"+
" end catch