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.
Author |
Topic |
dalewright24
Starting Member
6 Posts |
Posted - 2007-09-19 : 07:35:41
|
Hi PeopleI have forumalated the following sp.. When i check my syntax, there is no errors. When i execute the sp through Query analyser, i get the 'Invalid Object Name 'U' however u is specified as a tbl.... Please can someone point me in the right direction. Thank you.CREATE PROCEDURE [spYearToDateTotals]@intuserID IntegerASSELECT u.*FROM tblUser AS uLEFT JOIN (SELECT UserID, SUM(CONVERT(int, Miles1)) AS SumOfMiles1, Rate1FROM tblApp AS d LEFT OUTER JOIN u ON d.UserID = u.UserIDWHERE (CONVERT(datetime,AuthorisedHRDate, 103) BETWEEN CONVERT(datetime, '31/03/2007', 103) AND CONVERT(datetime, '31/03/2008', 103))GROUP BY UserID, Rate1, AuthorisedHRStatusHAVING (AuthorisedHRStatus = 1) AND (CONVERT(decimal, Rate1) > 0)) AS d ON d.UserID =u.UserIDLEFT JOIN (SELECT UserID, SUM(CONVERT(int, Miles2)) AS SumOfMiles2, Rate2FROM tblApp AS e LEFT OUTER JOIN u ON e.UserID = u.UserIDWHERE (CONVERT(datetime,AuthorisedHRDate, 103) BETWEEN CONVERT(datetime, '31/03/2007', 103) AND CONVERT(datetime, '31/03/2008', 103))GROUP BY UserID, Rate1, AuthorisedHRStatusHAVING (AuthorisedHRStatus = 1) AND (CONVERT(decimal, Rate2) > 0)) AS e ON e.UserID =u.UserIDWHERE u.UserID = @intuserIDGROUP BY u.UserIDGO |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-09-19 : 07:39:57
|
quote: Originally posted by dalewright24 Hi PeopleI have forumalated the following sp.. When i check my syntax, there is no errors. When i execute the sp through Query analyser, i get the 'Invalid Object Name 'U' however u is specified as a tbl.... Please can someone point me in the right direction. Thank you.CREATE PROCEDURE [spYearToDateTotals]@intuserID IntegerASSELECT u.*FROM tblUser AS uLEFT JOIN (SELECT UserID, SUM(CONVERT(int, Miles1)) AS SumOfMiles1, Rate1FROM tblApp AS d LEFT OUTER JOIN u ON d.UserID = u.UserIDWHERE (CONVERT(datetime,AuthorisedHRDate, 103) BETWEEN CONVERT(datetime, '31/03/2007', 103) AND CONVERT(datetime, '31/03/2008', 103))GROUP BY UserID, Rate1, AuthorisedHRStatusHAVING (AuthorisedHRStatus = 1) AND (CONVERT(decimal, Rate1) > 0)) AS d ON d.UserID =u.UserIDLEFT JOIN (SELECT UserID, SUM(CONVERT(int, Miles2)) AS SumOfMiles2, Rate2FROM tblApp AS e LEFT OUTER JOIN u ON e.UserID = u.UserIDWHERE (CONVERT(datetime,AuthorisedHRDate, 103) BETWEEN CONVERT(datetime, '31/03/2007', 103) AND CONVERT(datetime, '31/03/2008', 103))GROUP BY UserID, Rate1, AuthorisedHRStatusHAVING (AuthorisedHRStatus = 1) AND (CONVERT(decimal, Rate2) > 0)) AS e ON e.UserID =u.UserIDWHERE u.UserID = @intuserIDGROUP BY u.UserIDGO
Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-19 : 08:01:49
|
[code]CREATE PROCEDURE [spYearToDateTotals]( @intuserID Int(ASSET NOCOUNT ONSELECT u.*, d.sumofmiles1, d.rate1, e.sumofmiles2, e.rate2FROM tblUser AS uLEFT JOIN ( SELECT UserID, SUM(CONVERT(int, Miles1)) AS SumOfMiles1, Rate1 FROM tblApp AS d LEFT JOIN u ON d.UserID = u.UserID WHERE AuthorisedHRDate BETWEEN '31/03/2007' AND '31/03/2008' AND AuthorisedHRStatus = 1 AND CONVERT(decimal, Rate1) > 0 GROUP BY UserID, Rate1 ) AS d ON d.UserID =u.UserIDLEFT JOIN ( SELECT UserID, SUM(CONVERT(int, Miles2)) AS SumOfMiles2, Rate2 FROM tblApp AS e LEFT JOIN u ON e.UserID = u.UserID WHERE AuthorisedHRDate '31/03/2007' AND '31/03/2008' AND AuthorisedHRStatus = 1 AND CONVERT(decimal, Rate2) > 0 GROUP BY UserID, Rate1 ) AS e ON e.UserID =u.UserIDWHERE u.UserID = @intuserID[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
dalewright24
Starting Member
6 Posts |
Posted - 2007-09-19 : 10:29:24
|
Hi GuysThanks for the reply, but to be honest i get the same error message. I didnt think there was anything wrong with my SQL statement and after looking through google, it seems as though there is a problem with sql 2k, so i am downloading the service packs to see what if that solves the issues. I will keep you informed. |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-19 : 10:46:30
|
Maybe you should start to prefix all column names with their table names? E 12°55'05.25"N 56°04'39.16" |
 |
|
dalewright24
Starting Member
6 Posts |
Posted - 2007-09-19 : 12:16:15
|
Hi peso...I have tried downloading the update, i have also tried prefix, incase it was to do with an ownership issue. However, still no Joy... I cant understand whats wrong... Back to the drawing board to see what is going on i think. |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-19 : 12:36:29
|
[code]CREATE PROCEDURE [spYearToDateTotals]( @intuserID Int(ASSET NOCOUNT ONSELECT u.*, d.sumofmiles1, d.rate1, e.sumofmiles2, e.rate2FROM tblUser AS uLEFT JOIN ( SELECT UserID, SUM(CONVERT(int, Miles1)) AS SumOfMiles1, Rate1 FROM tblApp WHERE AuthorisedHRDate BETWEEN '31/03/2007' AND '31/03/2008' AND AuthorisedHRStatus = 1 AND CONVERT(decimal, Rate1) > 0 GROUP BY UserID, Rate1 ) AS d ON d.UserID = u.UserIDLEFT JOIN ( SELECT UserID, SUM(CONVERT(int, Miles2)) AS SumOfMiles2, Rate2 FROM tblApp WHERE AuthorisedHRDate '31/03/2007' AND '31/03/2008' AND AuthorisedHRStatus = 1 AND CONVERT(decimal, Rate2) > 0 GROUP BY UserID, Rate1 ) AS e ON e.UserID = u.UserIDWHERE u.UserID = @intuserID[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2007-09-19 : 14:18:10
|
quote: Originally posted by Peso
CREATE PROCEDURE [spYearToDateTotals]( @intuserID Int(ASSET NOCOUNT ONSELECT u.*, d.sumofmiles1, d.rate1, e.sumofmiles2, e.rate2FROM tblUser AS uLEFT JOIN ( SELECT UserID, SUM(CONVERT(int, Miles1)) AS SumOfMiles1, Rate1 FROM tblApp AS d LEFT JOIN tblUser AS u ON d.UserID = u.UserID WHERE AuthorisedHRDate BETWEEN '31/03/2007' AND '31/03/2008' AND AuthorisedHRStatus = 1 AND CONVERT(decimal, Rate1) > 0 GROUP BY UserID, Rate1 ) AS d ON d.UserID =u.UserIDLEFT JOIN ( SELECT UserID, SUM(CONVERT(int, Miles2)) AS SumOfMiles2, Rate2 FROM tblApp AS e LEFT JOIN tblUser AS u ON e.UserID = u.UserID WHERE AuthorisedHRDate '31/03/2007' AND '31/03/2008' AND AuthorisedHRStatus = 1 AND CONVERT(decimal, Rate2) > 0 GROUP BY UserID, Rate1 ) AS e ON e.UserID =u.UserIDWHERE u.UserID = @intuserID E 12°55'05.25"N 56°04'39.16"
Added a fix, not sure if that gets it to 100%. But the basic issue is trying to use a table alias defined outside of the derived table as a joining table in the derived table. |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-19 : 14:28:12
|
The LEFT JOIN is not necessary in the derived table, since you only are grouping by userid an rate. E 12°55'05.25"N 56°04'39.16" |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2007-09-19 : 15:21:09
|
quote: Originally posted by Peso The LEFT JOIN is not necessary in the derived table, since you only are grouping by userid an rate.
Agreed, I was just showing the OP why the original error was happening. |
 |
|
dalewright24
Starting Member
6 Posts |
Posted - 2007-09-20 : 05:26:57
|
Hi Guys... Some good posts, but still getting the same error when executing the SP.... Any more ideas? sorry |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2007-09-20 : 13:56:59
|
Can you post your latest query so we are working from the correct baseline? |
 |
|
|
|
|
|
|