Microsoft Dot Net Master

Microsoft Dot Net Master
Microsoft Dot Net Master

Monday, February 28, 2011

Insert Multiple Table Values in a Single Table Using Cursor Sql Server

declare @str1 varchar(50)
declare @tot1 int
declare @tot2 int
declare @tot3 int
DECLARE @USERID INT
CREATE TABLE #report(Name Varchar(50),Account_Count int,Physicisn_Count int,Completed int)

DECLARE getID CURSOR FOR
SELECT ID FROM adminlogin where TypeofUser<>'A'
OPEN getID
FETCH NEXT
FROM getID INTO @USERID
WHILE @@FETCH_STATUS = 0
BEGIN
set @str1=( select Name from adminlogin where id=@USERID)
set @tot1=( select count(*) as Account_Count from View_Rpt_EmpVsHospital where id=@USERID)
set @tot2=( select count(*) as Physician_Count from View_Rpt_PhyVsHospital where Hospitalid in (select Hospitalid from View_Rpt_EmpVsHospital where id=@USERID))
set @tot3=(select count(distinct Physicianid) as Completed from View_SalesEntry where Physicianid in (select Physicianid from View_Rpt_PhyVsHospital where Month(DateFrom)=12 and Month(DateTo)= 12 and Year(DateFrom)=2008 and Year(DateTo)= 2008 and LocationId=24
and Hospitalid in (select Hospitalid from View_Rpt_EmpVsHospital where id=@USERID)))

INSERT INTO #report(Name,Account_Count,Physicisn_Count,Completed) VALUES ((@str1),(@tot1),(@tot2),(@tot3));
FETCH NEXT
FROM getID INTO @USERID

END
CLOSE getID
DEALLOCATE getID
--SELECT * FROM #report
--drop table #report

No comments:

Post a Comment