 |
|
 |
|
Next: Execute Stored procedure scripts in bulk
|
| Author |
Message |
External

Since: Dec 29, 2008 Posts: 20
|
(Msg. 1) Posted: Tue Jan 13, 2009 5:35 am
Post subject: Problem with Stored Procedure Archived from groups: comp>databases>ms-sqlserver, others (more info?)
|
|
|
I have a stored procedure from SQL 7 that won't run in SQL 2005, and I can't
figure out what the issue is. Can someone take a look at it and see if
anything stands out? The error message I get is: "Invalid column name: 'YR'.
Thanks so much!
@StartYear int,
@OnlyNew bit
As
set nocount on
declare @SQL as nvarchar(2000)
CREATE TABLE #temp1 (SOURCE varchar(255), YR Int, Amt float, CustCount INT)
CREATE UNIQUE INDEX MyIndex ON #temp1(Source, YR)
Set @SQL = 'INSERT INTO #temp1 (Source, YR, Amt)' +
'SELECT CUSTOMER.SOURCE, { fn YEAR(I.INVDATE) } AS YR, SUM(I.SDPRICE) AS Amt
' +
'FROM vwuniInvoiceDet I ' +
'INNER JOIN CUSTOMER ON I.INVCUST = CUSTOMER.[Index] '
If @OnlyNew =1 Set @SQL = @SQL + ' AND { fn YEAR(I.INVDATE) } = { fn
YEAR(dbo.CUSTOMER.MLDATE) } '
Set @SQL = @SQL + 'WHERE Customer.Source<>''XX'' And I.SDINVNUM Is Not Null
' +
'GROUP BY { fn YEAR(I.INVDATE) }, CUSTOMER.SOURCE ' +
'HAVING { fn YEAR(I.INVDATE) } >= ' + Convert(nvarchar, @StartYear) +
'ORDER BY Customer.Source, I.YR '
exec sp_executesql @SQL
.... (sp continues, but error is in the above section) >> Stay informed about: Problem with Stored Procedure |
|
| Back to top |
|
 |  |
External

Since: Aug 24, 2003 Posts: 1744
|
(Msg. 2) Posted: Tue Jan 13, 2009 8:29 am
Post subject: Re: Problem with Stored Procedure [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Neil
It throws an error proba;y on ORDER BY clause becvause I alias contains YR
alias as well
Itis really hard to test this query but what if you add { fn
YEAR(I.INVDATE) } to GROUP BY clause instead of I.YR
http://www.sommarskog.se/dynamic_sql.html
"Neil" wrote in message
>I have a stored procedure from SQL 7 that won't run in SQL 2005, and I
>can't figure out what the issue is. Can someone take a look at it and see
>if anything stands out? The error message I get is: "Invalid column name:
>'YR'. Thanks so much!
>
> @StartYear int,
>
> @OnlyNew bit
>
> As
>
> set nocount on
>
> declare @SQL as nvarchar(2000)
>
> CREATE TABLE #temp1 (SOURCE varchar(255), YR Int, Amt float, CustCount
> INT)
>
> CREATE UNIQUE INDEX MyIndex ON #temp1(Source, YR)
>
> Set @SQL = 'INSERT INTO #temp1 (Source, YR, Amt)' +
>
> 'SELECT CUSTOMER.SOURCE, { fn YEAR(I.INVDATE) } AS YR, SUM(I.SDPRICE) AS
> Amt ' +
>
> 'FROM vwuniInvoiceDet I ' +
>
> 'INNER JOIN CUSTOMER ON I.INVCUST = CUSTOMER.[Index] '
>
> If @OnlyNew =1 Set @SQL = @SQL + ' AND { fn YEAR(I.INVDATE) } = { fn
> YEAR(dbo.CUSTOMER.MLDATE) } '
>
> Set @SQL = @SQL + 'WHERE Customer.Source<>''XX'' And I.SDINVNUM Is Not
> Null ' +
>
> 'GROUP BY { fn YEAR(I.INVDATE) }, CUSTOMER.SOURCE ' +
>
> 'HAVING { fn YEAR(I.INVDATE) } >= ' + Convert(nvarchar, @StartYear) +
>
> 'ORDER BY Customer.Source, I.YR '
>
> exec sp_executesql @SQL
>
> ... (sp continues, but error is in the above section)
>
>
>
>
> >> Stay informed about: Problem with Stored Procedure |
|
| Back to top |
|
 |  |
External

Since: Dec 29, 2008 Posts: 20
|
(Msg. 3) Posted: Tue Jan 13, 2009 8:29 am
Post subject: Re: Problem with Stored Procedure [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Yup, that was it. Strange, though, that the SP worked with I.YR in SQL 7.
Thanks,
Neil
"Uri Dimant" wrote in message
> Neil
> It throws an error proba;y on ORDER BY clause becvause I alias contains YR
> alias as well
> Itis really hard to test this query but what if you add { fn
> YEAR(I.INVDATE) } to GROUP BY clause instead of I.YR
> http://www.sommarskog.se/dynamic_sql.html
>
>
>
>
>
>
>
> "Neil" wrote in message
>
>>I have a stored procedure from SQL 7 that won't run in SQL 2005, and I
>>can't figure out what the issue is. Can someone take a look at it and see
>>if anything stands out? The error message I get is: "Invalid column name:
>>'YR'. Thanks so much!
>>
>> @StartYear int,
>>
>> @OnlyNew bit
>>
>> As
>>
>> set nocount on
>>
>> declare @SQL as nvarchar(2000)
>>
>> CREATE TABLE #temp1 (SOURCE varchar(255), YR Int, Amt float, CustCount
>> INT)
>>
>> CREATE UNIQUE INDEX MyIndex ON #temp1(Source, YR)
>>
>> Set @SQL = 'INSERT INTO #temp1 (Source, YR, Amt)' +
>>
>> 'SELECT CUSTOMER.SOURCE, { fn YEAR(I.INVDATE) } AS YR, SUM(I.SDPRICE) AS
>> Amt ' +
>>
>> 'FROM vwuniInvoiceDet I ' +
>>
>> 'INNER JOIN CUSTOMER ON I.INVCUST = CUSTOMER.[Index] '
>>
>> If @OnlyNew =1 Set @SQL = @SQL + ' AND { fn YEAR(I.INVDATE) } = { fn
>> YEAR(dbo.CUSTOMER.MLDATE) } '
>>
>> Set @SQL = @SQL + 'WHERE Customer.Source<>''XX'' And I.SDINVNUM Is Not
>> Null ' +
>>
>> 'GROUP BY { fn YEAR(I.INVDATE) }, CUSTOMER.SOURCE ' +
>>
>> 'HAVING { fn YEAR(I.INVDATE) } >= ' + Convert(nvarchar, @StartYear) +
>>
>> 'ORDER BY Customer.Source, I.YR '
>>
>> exec sp_executesql @SQL
>>
>> ... (sp continues, but error is in the above section)
>>
>>
>>
>>
>>
>
> >> Stay informed about: Problem with Stored Procedure |
|
| Back to top |
|
 |  |
External

Since: Aug 24, 2003 Posts: 1744
|
(Msg. 4) Posted: Tue Jan 13, 2009 8:29 am
Post subject: Re: Problem with Stored Procedure [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Neil
In SQL Server 2005 that behaviour is changed.
"Neil" wrote in message
> Yup, that was it. Strange, though, that the SP worked with I.YR in SQL 7.
>
> Thanks,
>
> Neil
>
>
> "Uri Dimant" wrote in message
>
>> Neil
>> It throws an error proba;y on ORDER BY clause becvause I alias contains
>> YR alias as well
>> Itis really hard to test this query but what if you add { fn
>> YEAR(I.INVDATE) } to GROUP BY clause instead of I.YR
>> http://www.sommarskog.se/dynamic_sql.html
>>
>>
>>
>>
>>
>>
>>
>> "Neil" wrote in message
>>
>>>I have a stored procedure from SQL 7 that won't run in SQL 2005, and I
>>>can't figure out what the issue is. Can someone take a look at it and see
>>>if anything stands out? The error message I get is: "Invalid column name:
>>>'YR'. Thanks so much!
>>>
>>> @StartYear int,
>>>
>>> @OnlyNew bit
>>>
>>> As
>>>
>>> set nocount on
>>>
>>> declare @SQL as nvarchar(2000)
>>>
>>> CREATE TABLE #temp1 (SOURCE varchar(255), YR Int, Amt float, CustCount
>>> INT)
>>>
>>> CREATE UNIQUE INDEX MyIndex ON #temp1(Source, YR)
>>>
>>> Set @SQL = 'INSERT INTO #temp1 (Source, YR, Amt)' +
>>>
>>> 'SELECT CUSTOMER.SOURCE, { fn YEAR(I.INVDATE) } AS YR, SUM(I.SDPRICE) AS
>>> Amt ' +
>>>
>>> 'FROM vwuniInvoiceDet I ' +
>>>
>>> 'INNER JOIN CUSTOMER ON I.INVCUST = CUSTOMER.[Index] '
>>>
>>> If @OnlyNew =1 Set @SQL = @SQL + ' AND { fn YEAR(I.INVDATE) } = { fn
>>> YEAR(dbo.CUSTOMER.MLDATE) } '
>>>
>>> Set @SQL = @SQL + 'WHERE Customer.Source<>''XX'' And I.SDINVNUM Is Not
>>> Null ' +
>>>
>>> 'GROUP BY { fn YEAR(I.INVDATE) }, CUSTOMER.SOURCE ' +
>>>
>>> 'HAVING { fn YEAR(I.INVDATE) } >= ' + Convert(nvarchar, @StartYear) +
>>>
>>> 'ORDER BY Customer.Source, I.YR '
>>>
>>> exec sp_executesql @SQL
>>>
>>> ... (sp continues, but error is in the above section)
>>>
>>>
>>>
>>>
>>>
>>
>>
>
> >> Stay informed about: Problem with Stored Procedure |
|
| Back to top |
|
 |  |
External

Since: Dec 29, 2008 Posts: 20
|
(Msg. 5) Posted: Tue Jan 13, 2009 8:29 am
Post subject: Re: Problem with Stored Procedure [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Ah. Good to know. Do you know what their rationale was for changing it?
"Uri Dimant" wrote in message
> Neil
> In SQL Server 2005 that behaviour is changed.
>
>
>
> "Neil" wrote in message
>
>> Yup, that was it. Strange, though, that the SP worked with I.YR in SQL 7.
>>
>> Thanks,
>>
>> Neil
>>
>>
>> "Uri Dimant" wrote in message
>>
>>> Neil
>>> It throws an error proba;y on ORDER BY clause becvause I alias contains
>>> YR alias as well
>>> Itis really hard to test this query but what if you add { fn
>>> YEAR(I.INVDATE) } to GROUP BY clause instead of I.YR
>>> http://www.sommarskog.se/dynamic_sql.html
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> "Neil" wrote in message
>>>
>>>>I have a stored procedure from SQL 7 that won't run in SQL 2005, and I
>>>>can't figure out what the issue is. Can someone take a look at it and
>>>>see if anything stands out? The error message I get is: "Invalid column
>>>>name: 'YR'. Thanks so much!
>>>>
>>>> @StartYear int,
>>>>
>>>> @OnlyNew bit
>>>>
>>>> As
>>>>
>>>> set nocount on
>>>>
>>>> declare @SQL as nvarchar(2000)
>>>>
>>>> CREATE TABLE #temp1 (SOURCE varchar(255), YR Int, Amt float, CustCount
>>>> INT)
>>>>
>>>> CREATE UNIQUE INDEX MyIndex ON #temp1(Source, YR)
>>>>
>>>> Set @SQL = 'INSERT INTO #temp1 (Source, YR, Amt)' +
>>>>
>>>> 'SELECT CUSTOMER.SOURCE, { fn YEAR(I.INVDATE) } AS YR, SUM(I.SDPRICE)
>>>> AS Amt ' +
>>>>
>>>> 'FROM vwuniInvoiceDet I ' +
>>>>
>>>> 'INNER JOIN CUSTOMER ON I.INVCUST = CUSTOMER.[Index] '
>>>>
>>>> If @OnlyNew =1 Set @SQL = @SQL + ' AND { fn YEAR(I.INVDATE) } = { fn
>>>> YEAR(dbo.CUSTOMER.MLDATE) } '
>>>>
>>>> Set @SQL = @SQL + 'WHERE Customer.Source<>''XX'' And I.SDINVNUM Is Not
>>>> Null ' +
>>>>
>>>> 'GROUP BY { fn YEAR(I.INVDATE) }, CUSTOMER.SOURCE ' +
>>>>
>>>> 'HAVING { fn YEAR(I.INVDATE) } >= ' + Convert(nvarchar, @StartYear) +
>>>>
>>>> 'ORDER BY Customer.Source, I.YR '
>>>>
>>>> exec sp_executesql @SQL
>>>>
>>>> ... (sp continues, but error is in the above section)
>>>>
>>>>
>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
> >> Stay informed about: Problem with Stored Procedure |
|
| Back to top |
|
 |  |
External

Since: May 30, 2004 Posts: 2061
|
(Msg. 6) Posted: Tue Jan 13, 2009 6:25 pm
Post subject: Re: Problem with Stored Procedure [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
|
|
| Back to top |
|
 |  |
External

Since: Dec 29, 2008 Posts: 20
|
(Msg. 7) Posted: Tue Jan 13, 2009 10:43 pm
Post subject: Re: Problem with Stored Procedure [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
> Looking at your procedure, I suggest that you remove the ORDER BY clause
> entirely. I cannot see that it serves any purpose whatsoever.
Yes, you're right. I do have a sort in the final Select statement that
displays the results. But there's no reason to have it in that other place.
Thanks!
Neil >> Stay informed about: Problem with Stored Procedure |
|
| Back to top |
|
 |  |
| Related Topics: | How to return a Pk value from one stored procedure to anot.. - Dear All, I have one stored procedure like sp_insertEmployee Employee Table Fileds Eno int pk, ename varchar(100), designation varchar In stored Procedure After inserting the ename and designation it has to return the Eno pk Id..
iif stored procedure - Hi all, I have a bunch of queries initially written for MS Access that contain iif function. Since I'm new in SQL Server and I don't like the idea of rewriting all queries using CASE statement, I hope someone has written iif T-SQL stored procedure. I...
Need help writing a stored procedure - How would I write a stored procedure to get * where duedate is less than and not equal to today's date. Is this right? select * from library where duedate < != getdate() Thanks
Date Compare in SQL Server Stored Procedure - Hi, i have "req_date" column of "datetime" type in Database table besides other columns. From my Web page, i am calling the Stored Procedure with variable parameter "Search_Date" of Varchar(60) type. the value, i am pa...
Cleaning unused stored procedure best practice? - I've recently inhereted an environment in which we have over 600 stored procedures. The documentation is either very poor or non-existant and am needing advice on how to determine if a stored procedure has been used. Does SQL Server have any sort of.. |
|
You can post new topics in this forum You can reply to topics in this forum You can edit your posts in this forum You can delete your posts in this forum You can vote in polls in this forum
|
|
|
|
 |
|
|