Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 stored proc joins

Author  Topic 

srucker
Starting Member

26 Posts

Posted - 2008-06-18 : 12:58:11
I need help from you guru's.
I have this sp which normailzes some data to a temp table. I need to extend the "return results" select statement to join and get more data.
So I add task_charge_type to the select and created 3 joins to get it all together.
I can execute the alter sp command with no errors; however when I try to execute the sp I get the following errors:

Msg 209, Level 16, State 1, Procedure sp_psactualsbyweek, Line 69
Ambiguous column name 'PROJ_NAME'.
Msg 209, Level 16, State 1, Procedure sp_psactualsbyweek, Line 69
Ambiguous column name 'PROJ_NAME'.




set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

-- sp_psactualsbyweek.sql
--
-- This stored procedure generates a report of the actual hours worked by a
-- resource by project and task for a specific date range from the table
-- MSP_WEB_WORK.

ALTER procedure [dbo].[sp_psactualsbyweek]
-- date range variables
@startdate datetime,
@enddate datetime
as

-- cursor variables
declare @res_name nvarchar(510)
declare @proj_name nvarchar(510)
declare @task_name nvarchar(510)
declare @start datetime
declare @finish datetime
declare @value decimal(25,6)
declare @days int
declare @count int
declare @date datetime

-- temp table to hold normalized data
create table #actuals_data (
RES_NAME nvarchar(510),
PROJ_NAME nvarchar(510),
TASK_NAME nvarchar(510),
WORK_DAY datetime,
HOURS decimal(25,6) )

-- cursor to convert denormalized data
declare actuals_csr cursor for

SELECT DISTINCT r.RES_NAME,
p.PROJ_NAME,
a.TASK_NAME,
w.WWORK_START,
w.WWORK_FINISH,
w.WWORK_VALUE
FROM MSP_WEB_RESOURCES AS r
INNER JOIN MSP_WEB_ASSIGNMENTS AS a ON a.WRES_ID = r.WRES_ID
INNER JOIN MSP_WEB_WORK AS w ON w.WASSN_ID = a.WASSN_ID
INNER JOIN MSP_WEB_PROJECTS AS p ON p.WPROJ_ID = a.WPROJ_ID
where w.WWORK_TYPE = 1 -- actual work
and ((w.WWORK_START between @startdate and @enddate) or (w.WWORK_FINISH between @startdate and @enddate))
order by 1, 2, 3, 4

open actuals_csr
fetch next from actuals_csr into @res_name, @proj_name, @task_name, @start, @finish, @value
while @@fetch_status = 0
begin
select @days = DATEDIFF(day, @start, @finish) + 1
select @count = 0
while @count < @days
begin
select @date = DATEADD(day ,@count, @start)
insert into #actuals_data values( @res_name, @proj_name, @task_name, @date, ((@value/1000)/60) )
select @count = @count + 1
end

fetch next from actuals_csr into @res_name, @proj_name, @task_name, @start, @finish, @value
end
close actuals_csr
deallocate actuals_csr

-- return results
select RES_NAME,
PROJ_NAME,
Task_Name,
Task_Charge_Type = case when pe.ProjectEnterpriseOutlineCode30ID = 530 Then 'Expensed'
when pe.ProjectEnterpriseOutlineCode30ID = 529 and te.TaskEnterpriseOutlineCode30ID = 527 Then 'Capital' end,
Work_Day,
sum(HOURS) as 'TOTAL HOURS'
from #actuals_data
INNER JOIN MSP_WEB_PROJECTS AS p on #actuals_data.PROJ_NAME = p.PROJ_NAME
INNER JOIN MSP_VIEW_PROJ_PROJECTS_ENT AS pe ON pe.WPROJ_ID = p.WPROJ_ID
INNER JOIN MSP_VIEW_PROJ_TASKS_ENT AS te ON te.WPROJ_ID = pe.WPROJ_ID
where WORK_DAY between @startdate and @enddate
group by RES_NAME, PROJ_NAME, Task_Name, Work_Day
order by res_name

drop table #actuals_data


--Msg 209, Level 16, State 1, Procedure sp_psactualsbyweek, Line 69
--Ambiguous column name 'PROJ_NAME'.
--Msg 209, Level 16, State 1, Procedure sp_psactualsbyweek, Line 69
--Ambiguous column name 'PROJ_NAME'.

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-06-18 : 13:01:10
You need to qualify the table alias name on that column in the SELECT statement

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-18 : 13:03:47
quote:
Originally posted by srucker

I need help from you guru's.
I have this sp which normailzes some data to a temp table. I need to extend the "return results" select statement to join and get more data.
So I add task_charge_type to the select and created 3 joins to get it all together.
I can execute the alter sp command with no errors; however when I try to execute the sp I get the following errors:

Msg 209, Level 16, State 1, Procedure sp_psactualsbyweek, Line 69
Ambiguous column name 'PROJ_NAME'.
Msg 209, Level 16, State 1, Procedure sp_psactualsbyweek, Line 69
Ambiguous column name 'PROJ_NAME'.




set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

-- sp_psactualsbyweek.sql
--
-- This stored procedure generates a report of the actual hours worked by a
-- resource by project and task for a specific date range from the table
-- MSP_WEB_WORK.

ALTER procedure [dbo].[sp_psactualsbyweek]
-- date range variables
@startdate datetime,
@enddate datetime
as

-- cursor variables
declare @res_name nvarchar(510)
declare @proj_name nvarchar(510)
declare @task_name nvarchar(510)
declare @start datetime
declare @finish datetime
declare @value decimal(25,6)
declare @days int
declare @count int
declare @date datetime

-- temp table to hold normalized data
create table #actuals_data (
RES_NAME nvarchar(510),
PROJ_NAME nvarchar(510),
TASK_NAME nvarchar(510),
WORK_DAY datetime,
HOURS decimal(25,6) )

-- cursor to convert denormalized data
declare actuals_csr cursor for

SELECT DISTINCT r.RES_NAME,
p.PROJ_NAME,
a.TASK_NAME,
w.WWORK_START,
w.WWORK_FINISH,
w.WWORK_VALUE
FROM MSP_WEB_RESOURCES AS r
INNER JOIN MSP_WEB_ASSIGNMENTS AS a ON a.WRES_ID = r.WRES_ID
INNER JOIN MSP_WEB_WORK AS w ON w.WASSN_ID = a.WASSN_ID
INNER JOIN MSP_WEB_PROJECTS AS p ON p.WPROJ_ID = a.WPROJ_ID
where w.WWORK_TYPE = 1 -- actual work
and ((w.WWORK_START between @startdate and @enddate) or (w.WWORK_FINISH between @startdate and @enddate))
order by 1, 2, 3, 4

open actuals_csr
fetch next from actuals_csr into @res_name, @proj_name, @task_name, @start, @finish, @value
while @@fetch_status = 0
begin
select @days = DATEDIFF(day, @start, @finish) + 1
select @count = 0
while @count < @days
begin
select @date = DATEADD(day ,@count, @start)
insert into #actuals_data values( @res_name, @proj_name, @task_name, @date, ((@value/1000)/60) )
select @count = @count + 1
end

fetch next from actuals_csr into @res_name, @proj_name, @task_name, @start, @finish, @value
end
close actuals_csr
deallocate actuals_csr

-- return results
select RES_NAME,
{p/pe/te}.PROJ_NAME,
Task_Name,
Task_Charge_Type = case when pe.ProjectEnterpriseOutlineCode30ID = 530 Then 'Expensed'
when pe.ProjectEnterpriseOutlineCode30ID = 529 and te.TaskEnterpriseOutlineCode30ID = 527 Then 'Capital' end,
Work_Day,
sum(HOURS) as 'TOTAL HOURS'
from #actuals_data
INNER JOIN MSP_WEB_PROJECTS AS p on #actuals_data.PROJ_NAME = p.PROJ_NAME
INNER JOIN MSP_VIEW_PROJ_PROJECTS_ENT AS pe ON pe.WPROJ_ID = p.WPROJ_ID
INNER JOIN MSP_VIEW_PROJ_TASKS_ENT AS te ON te.WPROJ_ID = pe.WPROJ_ID
where WORK_DAY between @startdate and @enddate
group by RES_NAME, {p/pe/te}.PROJ_NAME, Task_Name, Work_Day
order by res_name

drop table #actuals_data


--Msg 209, Level 16, State 1, Procedure sp_psactualsbyweek, Line 69
--Ambiguous column name 'PROJ_NAME'.
--Msg 209, Level 16, State 1, Procedure sp_psactualsbyweek, Line 69
--Ambiguous column name 'PROJ_NAME'.



You have column PROJ_NAME in more than one table so please fully qualify it by giving the table alias where you're using it (i dont know which table has it think its p)
Go to Top of Page

srucker
Starting Member

26 Posts

Posted - 2008-06-18 : 13:18:43
OK Got it! Thanks.
So now I have another issue where I wrote a case staement and now need to see how I can add it to the group by clause since everything in the select has to be in the group by.

Here is the error I get from executing the SP below:

--Msg 329, Level 16, State 1, Procedure sp_psactualsbyweek, Line 69
--Each GROUP BY expression must contain at least one column reference.


set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

-- sp_psactualsbyweek.sql
--
-- This stored procedure generates a report of the actual hours worked by a
-- resource by project and task for a specific date range from the table
-- MSP_WEB_WORK.

ALTER procedure [dbo].[sp_psactualsbyweek]
-- date range variables
@startdate datetime,
@enddate datetime
as

-- cursor variables
declare @res_name nvarchar(510)
declare @proj_name nvarchar(510)
declare @task_name nvarchar(510)
declare @start datetime
declare @finish datetime
declare @value decimal(25,6)
declare @days int
declare @count int
declare @date datetime

-- temp table to hold normalized data
create table #actuals_data (
RES_NAME nvarchar(510),
PROJ_NAME nvarchar(510),
TASK_NAME nvarchar(510),
WORK_DAY datetime,
HOURS decimal(25,6) )

-- cursor to convert denormalized data
declare actuals_csr cursor for

SELECT DISTINCT r.RES_NAME,
p.PROJ_NAME,
a.TASK_NAME,
w.WWORK_START,
w.WWORK_FINISH,
w.WWORK_VALUE
FROM MSP_WEB_RESOURCES AS r
INNER JOIN MSP_WEB_ASSIGNMENTS AS a ON a.WRES_ID = r.WRES_ID
INNER JOIN MSP_WEB_WORK AS w ON w.WASSN_ID = a.WASSN_ID
INNER JOIN MSP_WEB_PROJECTS AS p ON p.WPROJ_ID = a.WPROJ_ID
where w.WWORK_TYPE = 1 -- actual work
and ((w.WWORK_START between @startdate and @enddate) or (w.WWORK_FINISH between @startdate and @enddate))
order by 1, 2, 3, 4

open actuals_csr
fetch next from actuals_csr into @res_name, @proj_name, @task_name, @start, @finish, @value
while @@fetch_status = 0
begin
select @days = DATEDIFF(day, @start, @finish) + 1
select @count = 0
while @count < @days
begin
select @date = DATEADD(day ,@count, @start)
insert into #actuals_data values( @res_name, @proj_name, @task_name, @date, ((@value/1000)/60) )
select @count = @count + 1
end

fetch next from actuals_csr into @res_name, @proj_name, @task_name, @start, @finish, @value
end
close actuals_csr
deallocate actuals_csr

-- return results
select RES_NAME,
p.PROJ_NAME,
Task_Name,
'Task_Charge_Type' = case when pe.ProjectEnterpriseOutlineCode30ID = 530 Then 'Expensed'
when pe.ProjectEnterpriseOutlineCode30ID = 529 and te.TaskEnterpriseOutlineCode30ID = 527 Then 'Capital' end,
a.Work_Day,
sum(a.HOURS) as 'TOTAL HOURS'
from #actuals_data as a,
MSP_WEB_PROJECTS AS p --on #actuals_data.PROJ_NAME = p.PROJ_NAME
INNER JOIN MSP_VIEW_PROJ_PROJECTS_ENT AS pe ON pe.WPROJ_ID = p.WPROJ_ID
INNER JOIN MSP_VIEW_PROJ_TASKS_ENT AS te ON te.WPROJ_ID = pe.WPROJ_ID
where WORK_DAY between @startdate and @enddate
group by RES_NAME, p.PROJ_NAME, Task_Name, 'Task_Charge_Type', Work_Day
order by res_name

drop table #actuals_data

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-18 : 13:50:26
Make it like this & try

select * FROM
(

select RES_NAME,
p.PROJ_NAME,
Task_Name,
'Task_Charge_Type' = case when pe.ProjectEnterpriseOutlineCode30ID = 530 Then 'Expensed'
when pe.ProjectEnterpriseOutlineCode30ID = 529 and te.TaskEnterpriseOutlineCode30ID = 527 Then 'Capital' end,
a.Work_Day,
sum(a.HOURS) as 'TOTAL HOURS'
from #actuals_data as a,
MSP_WEB_PROJECTS AS p --on #actuals_data.PROJ_NAME = p.PROJ_NAME
INNER JOIN MSP_VIEW_PROJ_PROJECTS_ENT AS pe ON pe.WPROJ_ID = p.WPROJ_ID
INNER JOIN MSP_VIEW_PROJ_TASKS_ENT AS te ON te.WPROJ_ID = pe.WPROJ_ID
where WORK_DAY between @startdate and @enddate)t
group by RES_NAME, p.PROJ_NAME, Task_Name, Task_Charge_Type, Work_Day
order by res_name
Go to Top of Page

srucker
Starting Member

26 Posts

Posted - 2008-06-18 : 14:40:27
Here is what I get from executing the updated sp:

--Msg 8120, Level 16, State 1, Procedure sp_psactualsbyweek, Line 70
--Column '#actuals_data.RES_NAME' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
--Msg 4104, Level 16, State 1, Procedure sp_psactualsbyweek, Line 70
--The multi-part identifier "p.PROJ_NAME" could not be bound.


set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go


-- sp_psactualsbyweek.sql
--
-- This stored procedure generates a report of the actual hours worked by a
-- resource by project and task for a specific date range from the table
-- MSP_WEB_WORK.

ALTER procedure [dbo].[sp_psactualsbyweek]
-- date range variables
@startdate datetime,
@enddate datetime
as

-- cursor variables
declare @res_name nvarchar(510)
declare @proj_name nvarchar(510)
declare @task_name nvarchar(510)
declare @start datetime
declare @finish datetime
declare @value decimal(25,6)
declare @days int
declare @count int
declare @date datetime

-- temp table to hold normalized data
create table #actuals_data (
RES_NAME nvarchar(510),
PROJ_NAME nvarchar(510),
TASK_NAME nvarchar(510),
WORK_DAY datetime,
HOURS decimal(25,6) )

-- cursor to convert denormalized data
declare actuals_csr cursor for

SELECT DISTINCT r.RES_NAME,
p.PROJ_NAME,
a.TASK_NAME,
w.WWORK_START,
w.WWORK_FINISH,
w.WWORK_VALUE
FROM MSP_WEB_RESOURCES AS r
INNER JOIN MSP_WEB_ASSIGNMENTS AS a ON a.WRES_ID = r.WRES_ID
INNER JOIN MSP_WEB_WORK AS w ON w.WASSN_ID = a.WASSN_ID
INNER JOIN MSP_WEB_PROJECTS AS p ON p.WPROJ_ID = a.WPROJ_ID
where w.WWORK_TYPE = 1 -- actual work
and ((w.WWORK_START between @startdate and @enddate) or (w.WWORK_FINISH between @startdate and @enddate))
order by 1, 2, 3, 4

open actuals_csr
fetch next from actuals_csr into @res_name, @proj_name, @task_name, @start, @finish, @value
while @@fetch_status = 0
begin
select @days = DATEDIFF(day, @start, @finish) + 1
select @count = 0
while @count < @days
begin
select @date = DATEADD(day ,@count, @start)
insert into #actuals_data values( @res_name, @proj_name, @task_name, @date, ((@value/1000)/60) )
select @count = @count + 1
end

fetch next from actuals_csr into @res_name, @proj_name, @task_name, @start, @finish, @value
end
close actuals_csr
deallocate actuals_csr

-- return results
select * FROM
(
select RES_NAME,
p.PROJ_NAME,
Task_Name,
'Task_Charge_Type' = case when pe.ProjectEnterpriseOutlineCode30ID = 530 Then 'Expensed'
when pe.ProjectEnterpriseOutlineCode30ID = 529 and te.TaskEnterpriseOutlineCode30ID = 527 Then 'Capital' end,
a.Work_Day, sum(a.HOURS) as 'TOTAL HOURS'
from #actuals_data as a,
MSP_WEB_PROJECTS AS p
INNER JOIN MSP_VIEW_PROJ_PROJECTS_ENT AS pe ON pe.WPROJ_ID = p.WPROJ_ID
INNER JOIN MSP_VIEW_PROJ_TASKS_ENT AS te ON te.WPROJ_ID = pe.WPROJ_ID
where WORK_DAY between @startdate and @enddate)t
group by RES_NAME, p.PROJ_NAME, Task_Name, Task_Charge_Type, Work_Day
order by res_name

drop table #actuals_data
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-18 : 14:55:35
Give aliases to all columns. ALso you've a ON conditionfor join missing.
select t.RES_NAME, 
t.PROJ_NAME,
t.Task_Name,
t.Task_Charge_Type,
t.Work_Day, t.TOTAL HOURS

FROM
(
select RES_NAME,
p.PROJ_NAME,
Task_Name,
'Task_Charge_Type' = case when pe.ProjectEnterpriseOutlineCode30ID = 530 Then 'Expensed'
when pe.ProjectEnterpriseOutlineCode30ID = 529 and te.TaskEnterpriseOutlineCode30ID = 527 Then 'Capital' end,
a.Work_Day, sum(a.HOURS) as 'TOTAL HOURS'
from #actuals_data as a
INNER JOIN MSP_WEB_PROJECTS AS p ON condition
INNER JOIN MSP_VIEW_PROJ_PROJECTS_ENT AS pe ON pe.WPROJ_ID = p.WPROJ_ID
INNER JOIN MSP_VIEW_PROJ_TASKS_ENT AS te ON te.WPROJ_ID = pe.WPROJ_ID
where WORK_DAY between @startdate and @enddate)t
group by t.RES_NAME, t.PROJ_NAME, t.Task_Name, t.Task_Charge_Type, t.Work_Day
order by t.res_name
Go to Top of Page
   

- Advertisement -