[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.sqlserver.programming

code printing last record only

shilkhanna

3/20/2007 3:00:00 PM

I am having problem with my code it is just printing the last record,
and skipping everything else. If someone can take a look n help me
out. That will be great help. Thanks in advance.

Declare @Full_Name varchar(5000)
Declare @FinalString varchar(5000)
Declare @Project_ID varchar(3)
declare @Project_Name varchar(5000)
declare @Budget_Code_ID varchar(4)
declare @actual_completion_date datetime, @estimated_completion_date
datetime
Declare @Hours varchar(10)
--declare @cmd varchar(1000)


set @actual_completion_date = '09/30/2006'
set @actual_completion_date = '10/01/2007'
set @estimated_completion_date = '09/30/2006'
set @estimated_completion_date= '10/01/2007'
--Set @cmd = 'select actual_completion_date,
--estimated_completion_date from Tap_project where
actual_completion_date>='+convert
--(varchar(23),@actual_completion_date,121)+' AND
estimated_completion_date>='+convert(varchar(23),@estimated_completion_date,
121)

SET @FinalString = ''
Set ANSI_warnings off
declare cur_first_name cursor for
--declare cur_last_name cursor for
--select first_name, last_name
--from vwTAP_Employee

select
dbo.tap_project.Project_ID,
dbo.tap_project.Project_Name,
dbo.tap_budget_code.Budget_Code_ID ,
dbo.tap_project.actual_completion_date,
dbo.tap_project.estimated_completion_date,
--convert (char(4),actual_completion_date,112)+ convert(char(5),
[actual_completion_date],4),
--convert (char(4),estimated_completion_date,112)+convert(char(5),
[estimated_completion_date],4),
cast (sum(Hours) as int )as Total_Hrs,
dbo.vwTAP_Employee.First_Name + SPACE(1) +
dbo.vwTAP_Employee.Last_Name as Full_Name
from dbo.tap_employee_project_week,
dbo.tap_project,
dbo.tap_budget_code,
dbo.tap_project_budget,
dbo.vwTAP_Employee
where dbo.tap_employee_project_week.project_budget_code_id =
dbo.tap_project_budget.project_budget_code_id
and dbo.tap_project_budget.budget_code_id =
dbo.tap_budget_code.budget_code_id
and dbo.tap_project.project_id = dbo.tap_project_budget.project_id
and dbo.TAP_Employee_Project_Week.iPersonID =
dbo.vwTAP_Employee.iPersonId
and (actual_completion_date between '09/30/2006' and '10/01/2007'
OR estimated_completion_date between '09/30/2006' and
'10/01/2007')
--and hours is not null
group by dbo.tap_project.project_id,
dbo.tap_project.Project_Name,dbo.tap_budget_code.Budget_Code_ID,
Actual_Completion_Date, estimated_completion_date,
dbo.vwTAP_Employee.First_Name, dbo.vwTAP_Employee.Last_Name
order by dbo.tap_budget_code.Budget_code_ID

open cur_first_name
fetch next from cur_first_name
into @Project_ID, @Project_Name, @Budget_Code_ID,
@actual_completion_date, @estimated_completion_date,
@Hours, @Full_Name

while (@@FETCH_STATUS = 0)
begin

SET @FinalString = @FinalString + @Full_Name + ', '
set @actual_completion_date= convert(varchar(15),
MOnth(@actual_completion_date))+'/'+ convert(varchar(15),
day(@actual_completion_date))+ '/' + convert(varchar(15),
Year(@actual_completion_date))
set @estimated_completion_date= convert(varchar(15),
MOnth(@estimated_completion_date))+'/'+ convert(varchar(15),
day(@estimated_completion_date))+ '/' + convert(varchar(15),
Year(@estimated_completion_date))

fetch next from cur_first_name
into @Project_ID, @Project_Name, @Budget_Code_ID,
@actual_completion_date, @estimated_completion_date,
@Hours, @Full_Name

end
--exec @cmd
close cur_first_name
deallocate cur_first_name
print @Project_ID
Print @Project_Name
Print @Budget_Code_ID
Print @actual_completion_date
Print @estimated_completion_date
Print @Hours
Print @full_name

3 Answers

masri999

3/20/2007 3:05:00 PM

0

On Mar 20, 7:59 pm, shilkha...@gmail.com wrote:
> I am having problem with my code it is just printing the last record,
> and skipping everything else. If someone can take a look n help me
> out. That will be great help. Thanks in advance.
>
> Declare @Full_Name varchar(5000)
> Declare @FinalString varchar(5000)
> Declare @Project_ID varchar(3)
> declare @Project_Name varchar(5000)
> declare @Budget_Code_ID varchar(4)
> declare @actual_completion_date datetime, @estimated_completion_date
> datetime
> Declare @Hours varchar(10)
> --declare @cmd varchar(1000)
>
> set @actual_completion_date = '09/30/2006'
> set @actual_completion_date = '10/01/2007'
> set @estimated_completion_date = '09/30/2006'
> set @estimated_completion_date= '10/01/2007'
> --Set @cmd = 'select actual_completion_date,
> --estimated_completion_date from Tap_project where
> actual_completion_date>='+convert
> --(varchar(23),@actual_completion_date,121)+' AND
> estimated_completion_date>='+convert(varchar(23),@estimated_completion_date?,
> 121)
>
> SET @FinalString = ''
> Set ANSI_warnings off
> declare cur_first_name cursor for
> --declare cur_last_name cursor for
> --select first_name, last_name
> --from vwTAP_Employee
>
> select
> dbo.tap_project.Project_ID,
> dbo.tap_project.Project_Name,
> dbo.tap_budget_code.Budget_Code_ID ,
> dbo.tap_project.actual_completion_date,
> dbo.tap_project.estimated_completion_date,
> --convert (char(4),actual_completion_date,112)+ convert(char(5),
> [actual_completion_date],4),
> --convert (char(4),estimated_completion_date,112)+convert(char(5),
> [estimated_completion_date],4),
> cast (sum(Hours) as int )as Total_Hrs,
> dbo.vwTAP_Employee.First_Name + SPACE(1) +
> dbo.vwTAP_Employee.Last_Name as Full_Name
> from dbo.tap_employee_project_week,
> dbo.tap_project,
> dbo.tap_budget_code,
> dbo.tap_project_budget,
> dbo.vwTAP_Employee
> where dbo.tap_employee_project_week.project_budget_code_id =
> dbo.tap_project_budget.project_budget_code_id
> and dbo.tap_project_budget.budget_code_id =
> dbo.tap_budget_code.budget_code_id
> and dbo.tap_project.project_id = dbo.tap_project_budget.project_id
> and dbo.TAP_Employee_Project_Week.iPersonID =
> dbo.vwTAP_Employee.iPersonId
> and (actual_completion_date between '09/30/2006' and '10/01/2007'
> OR estimated_completion_date between '09/30/2006' and
> '10/01/2007')
> --and hours is not null
> group by dbo.tap_project.project_id,
> dbo.tap_project.Project_Name,dbo.tap_budget_code.Budget_Code_ID,
> Actual_Completion_Date, estimated_completion_date,
> dbo.vwTAP_Employee.First_Name, dbo.vwTAP_Employee.Last_Name
> order by dbo.tap_budget_code.Budget_code_ID
>
> open cur_first_name
> fetch next from cur_first_name
> into @Project_ID, @Project_Name, @Budget_Code_ID,
> @actual_completion_date, @estimated_completion_date,
> @Hours, @Full_Name
>
> while (@@FETCH_STATUS = 0)
> begin
>
> SET @FinalString = @FinalString + @Full_Name + ', '
> set @actual_completion_date= convert(varchar(15),
> MOnth(@actual_completion_date))+'/'+ convert(varchar(15),
> day(@actual_completion_date))+ '/' + convert(varchar(15),
> Year(@actual_completion_date))
> set @estimated_completion_date= convert(varchar(15),
> MOnth(@estimated_completion_date))+'/'+ convert(varchar(15),
> day(@estimated_completion_date))+ '/' + convert(varchar(15),
> Year(@estimated_completion_date))
>
> fetch next from cur_first_name
> into @Project_ID, @Project_Name, @Budget_Code_ID,
> @actual_completion_date, @estimated_completion_date,
> @Hours, @Full_Name
>
> end
> --exec @cmd
> close cur_first_name
> deallocate cur_first_name
> print @Project_ID
> Print @Project_Name
> Print @Budget_Code_ID
> Print @actual_completion_date
> Print @estimated_completion_date
> Print @Hours
> Print @full_name


If you are referring to print commnads , push these commands just
after
while (@@FETCH_STATUS = 0)
begin

it will print all the records. By the way avoid using cursors



shilkhanna

3/20/2007 3:15:00 PM

0

On Mar 20, 11:04 am, "M A Srinivas" <masri...@gmail.com> wrote:
> On Mar 20, 7:59 pm, shilkha...@gmail.com wrote:
>
>
>
>
>
> > I am having problem with my code it is just printing the last record,
> > and skipping everything else. If someone can take a look n help me
> > out. That will be great help. Thanks in advance.
>
> > Declare @Full_Name varchar(5000)
> > Declare @FinalString varchar(5000)
> > Declare @Project_ID varchar(3)
> > declare @Project_Name varchar(5000)
> > declare @Budget_Code_ID varchar(4)
> > declare @actual_completion_date datetime, @estimated_completion_date
> > datetime
> > Declare @Hours varchar(10)
> > --declare @cmd varchar(1000)
>
> > set @actual_completion_date = '09/30/2006'
> > set @actual_completion_date = '10/01/2007'
> > set @estimated_completion_date = '09/30/2006'
> > set @estimated_completion_date= '10/01/2007'
> > --Set @cmd = 'select actual_completion_date,
> > --estimated_completion_date from Tap_project where
> > actual_completion_date>='+convert
> > --(varchar(23),@actual_completion_date,121)+' AND
> > estimated_completion_date>='+convert(varchar(23),@estimated_completion_date­­,
> > 121)
>
> > SET @FinalString = ''
> > Set ANSI_warnings off
> > declare cur_first_name cursor for
> > --declare cur_last_name cursor for
> > --select first_name, last_name
> > --from vwTAP_Employee
>
> > select
> > dbo.tap_project.Project_ID,
> > dbo.tap_project.Project_Name,
> > dbo.tap_budget_code.Budget_Code_ID ,
> > dbo.tap_project.actual_completion_date,
> > dbo.tap_project.estimated_completion_date,
> > --convert (char(4),actual_completion_date,112)+ convert(char(5),
> > [actual_completion_date],4),
> > --convert (char(4),estimated_completion_date,112)+convert(char(5),
> > [estimated_completion_date],4),
> > cast (sum(Hours) as int )as Total_Hrs,
> > dbo.vwTAP_Employee.First_Name + SPACE(1) +
> > dbo.vwTAP_Employee.Last_Name as Full_Name
> > from dbo.tap_employee_project_week,
> > dbo.tap_project,
> > dbo.tap_budget_code,
> > dbo.tap_project_budget,
> > dbo.vwTAP_Employee
> > where dbo.tap_employee_project_week.project_budget_code_id =
> > dbo.tap_project_budget.project_budget_code_id
> > and dbo.tap_project_budget.budget_code_id =
> > dbo.tap_budget_code.budget_code_id
> > and dbo.tap_project.project_id = dbo.tap_project_budget.project_id
> > and dbo.TAP_Employee_Project_Week.iPersonID =
> > dbo.vwTAP_Employee.iPersonId
> > and (actual_completion_date between '09/30/2006' and '10/01/2007'
> > OR estimated_completion_date between '09/30/2006' and
> > '10/01/2007')
> > --and hours is not null
> > group by dbo.tap_project.project_id,
> > dbo.tap_project.Project_Name,dbo.tap_budget_code.Budget_Code_ID,
> > Actual_Completion_Date, estimated_completion_date,
> > dbo.vwTAP_Employee.First_Name, dbo.vwTAP_Employee.Last_Name
> > order by dbo.tap_budget_code.Budget_code_ID
>
> > open cur_first_name
> > fetch next from cur_first_name
> > into @Project_ID, @Project_Name, @Budget_Code_ID,
> > @actual_completion_date, @estimated_completion_date,
> > @Hours, @Full_Name
>
> > while (@@FETCH_STATUS = 0)
> > begin
>
> > SET @FinalString = @FinalString + @Full_Name + ', '
> > set @actual_completion_date= convert(varchar(15),
> > MOnth(@actual_completion_date))+'/'+ convert(varchar(15),
> > day(@actual_completion_date))+ '/' + convert(varchar(15),
> > Year(@actual_completion_date))
> > set @estimated_completion_date= convert(varchar(15),
> > MOnth(@estimated_completion_date))+'/'+ convert(varchar(15),
> > day(@estimated_completion_date))+ '/' + convert(varchar(15),
> > Year(@estimated_completion_date))
>
> > fetch next from cur_first_name
> > into @Project_ID, @Project_Name, @Budget_Code_ID,
> > @actual_completion_date, @estimated_completion_date,
> > @Hours, @Full_Name
>
> > end
> > --exec @cmd
> > close cur_first_name
> > deallocate cur_first_name
> > print @Project_ID
> > Print @Project_Name
> > Print @Budget_Code_ID
> > Print @actual_completion_date
> > Print @estimated_completion_date
> > Print @Hours
> > Print @full_name
>
> If you are referring to print commnads , push these commands just
> after
> while (@@FETCH_STATUS = 0)
> begin
>
> it will print all the records. By the way avoid using cursors- Hide quoted text -
>
> - Show quoted text -

yea it is already there, now it is just printing the first record.
where excatly you want me to put the command.
thanks for your help.

shilkhanna

3/20/2007 3:42:00 PM

0

On Mar 20, 11:04 am, "M A Srinivas" <masri...@gmail.com> wrote:
> On Mar 20, 7:59 pm, shilkha...@gmail.com wrote:
>
>
>
>
>
> > I am having problem with my code it is just printing the last record,
> > and skipping everything else. If someone can take a look n help me
> > out. That will be great help. Thanks in advance.
>
> > Declare @Full_Name varchar(5000)
> > Declare @FinalString varchar(5000)
> > Declare @Project_ID varchar(3)
> > declare @Project_Name varchar(5000)
> > declare @Budget_Code_ID varchar(4)
> > declare @actual_completion_date datetime, @estimated_completion_date
> > datetime
> > Declare @Hours varchar(10)
> > --declare @cmd varchar(1000)
>
> > set @actual_completion_date = '09/30/2006'
> > set @actual_completion_date = '10/01/2007'
> > set @estimated_completion_date = '09/30/2006'
> > set @estimated_completion_date= '10/01/2007'
> > --Set @cmd = 'select actual_completion_date,
> > --estimated_completion_date from Tap_project where
> > actual_completion_date>='+convert
> > --(varchar(23),@actual_completion_date,121)+' AND
> > estimated_completion_date>='+convert(varchar(23),@estimated_completion_date­­,
> > 121)
>
> > SET @FinalString = ''
> > Set ANSI_warnings off
> > declare cur_first_name cursor for
> > --declare cur_last_name cursor for
> > --select first_name, last_name
> > --from vwTAP_Employee
>
> > select
> > dbo.tap_project.Project_ID,
> > dbo.tap_project.Project_Name,
> > dbo.tap_budget_code.Budget_Code_ID ,
> > dbo.tap_project.actual_completion_date,
> > dbo.tap_project.estimated_completion_date,
> > --convert (char(4),actual_completion_date,112)+ convert(char(5),
> > [actual_completion_date],4),
> > --convert (char(4),estimated_completion_date,112)+convert(char(5),
> > [estimated_completion_date],4),
> > cast (sum(Hours) as int )as Total_Hrs,
> > dbo.vwTAP_Employee.First_Name + SPACE(1) +
> > dbo.vwTAP_Employee.Last_Name as Full_Name
> > from dbo.tap_employee_project_week,
> > dbo.tap_project,
> > dbo.tap_budget_code,
> > dbo.tap_project_budget,
> > dbo.vwTAP_Employee
> > where dbo.tap_employee_project_week.project_budget_code_id =
> > dbo.tap_project_budget.project_budget_code_id
> > and dbo.tap_project_budget.budget_code_id =
> > dbo.tap_budget_code.budget_code_id
> > and dbo.tap_project.project_id = dbo.tap_project_budget.project_id
> > and dbo.TAP_Employee_Project_Week.iPersonID =
> > dbo.vwTAP_Employee.iPersonId
> > and (actual_completion_date between '09/30/2006' and '10/01/2007'
> > OR estimated_completion_date between '09/30/2006' and
> > '10/01/2007')
> > --and hours is not null
> > group by dbo.tap_project.project_id,
> > dbo.tap_project.Project_Name,dbo.tap_budget_code.Budget_Code_ID,
> > Actual_Completion_Date, estimated_completion_date,
> > dbo.vwTAP_Employee.First_Name, dbo.vwTAP_Employee.Last_Name
> > order by dbo.tap_budget_code.Budget_code_ID
>
> > open cur_first_name
> > fetch next from cur_first_name
> > into @Project_ID, @Project_Name, @Budget_Code_ID,
> > @actual_completion_date, @estimated_completion_date,
> > @Hours, @Full_Name
>
> > while (@@FETCH_STATUS = 0)
> > begin
>
> > SET @FinalString = @FinalString + @Full_Name + ', '
> > set @actual_completion_date= convert(varchar(15),
> > MOnth(@actual_completion_date))+'/'+ convert(varchar(15),
> > day(@actual_completion_date))+ '/' + convert(varchar(15),
> > Year(@actual_completion_date))
> > set @estimated_completion_date= convert(varchar(15),
> > MOnth(@estimated_completion_date))+'/'+ convert(varchar(15),
> > day(@estimated_completion_date))+ '/' + convert(varchar(15),
> > Year(@estimated_completion_date))
>
> > fetch next from cur_first_name
> > into @Project_ID, @Project_Name, @Budget_Code_ID,
> > @actual_completion_date, @estimated_completion_date,
> > @Hours, @Full_Name
>
> > end
> > --exec @cmd
> > close cur_first_name
> > deallocate cur_first_name
> > print @Project_ID
> > Print @Project_Name
> > Print @Budget_Code_ID
> > Print @actual_completion_date
> > Print @estimated_completion_date
> > Print @Hours
> > Print @full_name
>
> If you are referring to print commnads , push these commands just
> after
> while (@@FETCH_STATUS = 0)
> begin
>
> it will print all the records. By the way avoid using cursors- Hide quoted text -
>
> - Show quoted text -

Its working now thanks for you help...yea i knw it was little hard
with cursor but need to use in this one. thanks again