Feb 25, 2015

Important SQL Query

Here are some important and necessary SQL server queries.




1. To insert data from one table to another...
   
 insert into [EmployeeInfo] ( EmpCode,  EmpName, Designation, Division, Department, OfficeBranch, DutyStation, Location, JoiningDate, Mobile, AccNo,          Bank,       PayBy,      IncomeTex)
             Select ID,        Name,    Designation, SBU,      Department, Area,         [Function],  Location, DOJ,         Mobile, BankAccountNo, [Bank Name], PaymentMood,TaxDeduction from TEMP_CurrentEmployees

2. To update one table column with the value from another...
        update  e set joiningdate= CONVERT(VARCHAR(12), cast(DOJ as date), 112) from            Employeeinfo e inner join TEMP_CurrentEmployees t on e.EmpCode=t.ID

3. Most important

   declare @ID as varchar(10)


declare  _cur cursor for
select ID from TEMP_CurrentEmployees where SBU in ('FCL' ,'Food & Life Style')
and id <>'0'

open  _cur

fetch next from _cur into @ID

WHILE @@FETCH_STATUS = 0
begin



insert into EmployeesSalaryStructure( EmpID, EmpCode, StructureID, SalaryHeadID, SalaryHeadType, TaxYearID, Amount, SalaryTypeID, BasedOnID, CreatedDate, ChangeDate, ChangePeriodID, EffectPeriodID, SortOrder, CompanyID, IsPresent, StructMasterId)
Select 1, @id, StructureID, SalaryHeadID, SalaryHeadType, TaxYearID, 0, SalaryTypeID, BasedOnID, CreatedDate, e.ChangeDate, ChangePeriodID, EffectPeriodID, SortOrder, CompanyID, IsPresent, StructMasterId from db_BnB_PayrollManagement.dbo. EmployeesSalaryStructure e
where e.EmpCode='0002'


fetch next from _cur into @ID

end

4.Another most important
 declare @ID as varchar(10)
declare @Basic as decimal (18,2)
declare @HR as decimal (18,2)
declare @Med as decimal (18,2)
declare @Con as decimal (18,2)
declare @spAllow as decimal (18,2)
declare @Tax as decimal (18,2)

declare  _cur cursor for 
select ID, Basic, HouseRent, Medical, Conveyance, Taxdeduction from TEMP_CurrentEmployees

open  _cur

fetch next from _cur into @ID,@Basic , @HR, @Med, @Con, @Tax

WHILE @@FETCH_STATUS = 0
begin
--Update basic
update EmployeesSalaryStructure
set amount=@Basic
where empcode=@ID and salaryheadid=1
--Update HR
update EmployeesSalaryStructure
set amount=@HR
where empcode=@ID and salaryheadid=4
--Update Medical
update EmployeesSalaryStructure
set amount=@Med
where empcode=@ID and salaryheadid=7
--Update Conv
update EmployeesSalaryStructure
set amount=@Con
where empcode=@ID and salaryheadid=6
      --Update spAllow
update EmployeesSalaryStructure
set amount=@spAllow 
where empcode=@ID and salaryheadid=6
--Update Tax
update EmployeesSalaryStructure
set amount=@Tax
where empcode=@ID and salaryheadid=28
fetch next from _cur into @ID,@Basic,@HR, @Med, @Con, @Tax,@spAllow 
end
DEALLOCATE _cur;

  • 0Blogger Comment
  • Facebook Comment

Leave your comment

Post a Comment