Showing posts with label following. Show all posts
Showing posts with label following. Show all posts

Friday, March 30, 2012

Receving error with no description

I have designed quite a few reports over the past few weeks, and today I received the following error:

An error occured during local report processing.
The definition of the report /Discharge7.rdl is invalid.
Exception of type: 'Microsoft.ReportingServices.ReportProcessing.ReportProcessingException' was thrown.

I've never seen this error before. I've gotten errors with typos in names of fields in my expressions, but I've never gotten an error on the 40 someodd other forms I have.

Does anyone know what could be causing this?

Did u tried to preview this report before uploading it to the server, the error says that is not definned correctly, so the report must be bad, when you make a preview on the reporting services it will show you the detailed errors.

|||You can also look at the logs for more detailed error messages. These are located here on a default installation: C:\Program Files\Microsoft SQL Server\MSSQL\Reporting Services\LogFiles\

For more information about the various Reporting Services log files:
http://msdn2.microsoft.com/en-us/library/ms157403.aspx|||This was while I was previewing it in Visual Studio in the report designer. I haven't published it to the server yet.

Receiving Error: 'QUOTED_IDENTIFIER' when updating

Hello, I am getting the following message when running a stored proc:
Server: Msg 1934, Level 16, State 1, Procedure SZ_ProcessToCPInvoices, Line
261
UPDATE failed because the following SET options have incorrect settings:
'QUOTED_IDENTIFIER'.
I cannot find any information about this error. This happens with
QUOTED_IDENTIFIER on or off.Ric,
Try going to Query Analyzer and editing the sp. You should be able to see
the settings of "QUOTED_IDENTIFIER" when the sp was created. chnage it to
accomodate your needs.
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
ALTER procedure dbo.SZ_ProcessToCPInvoices
...
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
AMB
"Ric" wrote:

> Hello, I am getting the following message when running a stored proc:
> Server: Msg 1934, Level 16, State 1, Procedure SZ_ProcessToCPInvoices, Lin
e
> 261
> UPDATE failed because the following SET options have incorrect settings:
> 'QUOTED_IDENTIFIER'.
> I cannot find any information about this error. This happens with
> QUOTED_IDENTIFIER on or off.
>

Receiving Error: 'QUOTED_IDENTIFIER' when updating

Hello, I am getting the following message when running a stored proc:
Server: Msg 1934, Level 16, State 1, Procedure SZ_ProcessToCPInvoices, Line
261
UPDATE failed because the following SET options have incorrect settings:
'QUOTED_IDENTIFIER'.
I cannot find any information about this error. This happens with
QUOTED_IDENTIFIER on or off.
Ric,
Try going to Query Analyzer and editing the sp. You should be able to see
the settings of "QUOTED_IDENTIFIER" when the sp was created. chnage it to
accomodate your needs.
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
ALTER procedure dbo.SZ_ProcessToCPInvoices
...
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
AMB
"Ric" wrote:

> Hello, I am getting the following message when running a stored proc:
> Server: Msg 1934, Level 16, State 1, Procedure SZ_ProcessToCPInvoices, Line
> 261
> UPDATE failed because the following SET options have incorrect settings:
> 'QUOTED_IDENTIFIER'.
> I cannot find any information about this error. This happens with
> QUOTED_IDENTIFIER on or off.
>

Receiving error when trying to use try..catch block

I am using SQL Server 2005 and am trying to execute the following statements:

BEGIN TRY SELECT 1/0 END TRY BEGIN CATCH SELECT 'Error Caught' END CATCH I am getting the following errors:

Msg 170, Level 15, State 1, Line 1

Line 1: Incorrect syntax near 'TRY'.

Msg 156, Level 15, State 1, Line 3

Incorrect syntax near the keyword 'END'.

This code should work, can anyone tell me why it it not working?

Thanks in advace.

Hmmm, works fine here.


Is there something else you have coded in the same script?

Are you using SSMS?

|||Sorry, my mistake. I was using our SQL Server 2005 Management Studio, but had accidentally connected to the SQL Server 2000 box to test with. Thanks.|||Been there, done that Smile

Wednesday, March 28, 2012

Receiving Cross Join Result in Error

Hello,

I'm getting a cross join result from the following query;

SELECT DISTINCT RTRIM(DA.AcctCode) AS DaAcctCode, cy.JanRev AS CYJanRev, PY.JanRev AS PYJanRev

FROM SalesCommissions.dbo.DailyAccountsDownload DA

INNER JOIN SalesReporting.dbo.PriorYearSales PY

ON CAST(RTRIM(DA.AcctCode)AS varchar(8)) = RTRIM(PY.AcctCode)

INNER JOIN SalesReporting.dbo.CurrentYearSales CY

ON CAST(RTRIM(DA.AcctCode)AS varchar(8)) = RTRIM(CY.AcctCode)

WHERE

RTRIM(da.acctcode) = 'am940'

ORDER BY

cy.janRev, py.JanRev

--************************************************
The result looks like this;

DaAcctCode CYJanRev PYJanRev
AM940 2.25 0.00
AM940 2.25 12.75
AM940 2.25 745.00
AM940 2.25 1620.50
AM940 511.00 0.00
AM940 511.00 12.75
AM940 511.00 745.00
AM940 511.00 1620.50
AM940 1256.50 0.00
AM940 1256.50 12.75
AM940 1256.50 745.00
AM940 1256.50 1620.50

When I try to SUM(CY.JanRev) and SUM(PY.JanRev), then GROUP BY DA.AcctCode, all of the numbers in this cross join result are summed, and result is incorrect.

The thing I am trying to do is get correct totals for CYJanRev and PYJanRev per each DAAcctCode. In this example, the totals should be 1769.75 and 2378.25 respectively.

Why might I be having this problem? I've tried a number of different aggregate functions and joins to get the correct results, but nothing has worked.

Thank you for your help!

cdun2

I think there is problem with your join condition. Hope this not retrieving the previous years data too? Could you double check pl...|||

Maybe something like:

select da.acctCode as daAcctCode,
( select sum(py.janRev) from dbo.priorYearSales py
where CAST(RTRIM(DA.AcctCode)AS varchar(8)) = RTRIM(PY.AcctCode)
) as pyJanRev,
( select sum(cy.janRev) from dbo.currentYearSales cy
where CAST(RTRIM(DA.AcctCode)AS varchar(8)) = RTRIM(cY.AcctCode)
) as cyJanRev
from ( select distinct rtrim(acctCode)
from salesCommissions.dbo.dailyAccountsDownload
) da

?

|||

I'm not sure I follow you. CY is the current year data (2007) and PY is the previous year data (2006).

|||

This got it;

SELECT RTRIM( DA.AcctCode) AS DaAcctCode, CY.JanRev AS CYJanRev, PY.JanRev PYJanRev
FROM @.DailyAccountsDownload DA
INNER JOIN( SELECT AcctCode, SUM( JanRev) AS JanRev FROM @.CurrentYearSales GROUP BY AcctCode) CY
ON CAST( RTRIM( DA.AcctCode) AS varchar(8)) = RTRIM( CY.AcctCode)
INNER JOIN( SELECT AcctCode, SUM( JanRev) AS JanRev FROM @.PriorYearSales GROUP BY AcctCode) PY
ON CAST( RTRIM( DA.AcctCode) AS varchar(8)) = RTRIM( PY.AcctCode)
WHERE RTRIM( DA.AcctCode) = 'AM940'

Thanks for the assistance!

cdun2

receive top 20

HI

I am trying to set up a stored procedure to retrieve to 20 messages from a queue into a table to implement a batched process. I have the following code in a stored procedure.

WAITFOR (
RECEIVE top (20) -- get batched so that we can process same listid once
message_type_name,
message_body, -- the message contents
conversation_handle -- the identifier of the dialog this message was received on
FROM dbo.target
into @.PayloadData
), TIMEOUT 3000 -- if the queue is empty for three second, give UPDATE and go away

However, the stored procedure is only retrieving 1 message at a time from the queue. Did I miss some other setting

thanks

P

RECEIVE can only return messages on one conversation group. Normally each conversation is its own conversation group. If you sent only one message on each conversation, RECEIVE cannot get more that one message at a time, even if there are more messages in the queue.|||

Hi

so, in your blog on T-SQL RECEIVE. Fast. : Set based Processing.

How are you able to receive the message in bulk? Is it because of the way you send the message in LoadQueueReceivePerfBlog?

P|||Yes. This is also the reason why I recommend reusing dialogs in my other entry at http://blogs.msdn.com/remusrusanu/archive/2007/04/24/reusing-conversations.aspx|||

one follow up,

when I send the message using the same conversation handle, the receive top (20) statement waits until the previous batch is committed before it will start retrieving the next 20. I guess this is because of the its now part of the same conversation group and service broker need to guarantee process order?

This is what my proc looks like

BEGIN TRANSACTION

WAITFOR (

RECEIVE top (20) -- get batched so that we can process same listid once

message_type_name,

message_body, -- the message contents

conversation_handle -- the identifier of the dialog this message was received on

FROM dbo.target

into @.PayloadData

), TIMEOUT 3000 -- if the queue is empty for three second, give UPDATE and go away

-- do some processing of the records in @.PayloadData

COMMIT TRANSACTION

if "-- do some processing of the records in @.PayloadData" is taking a long time, its going to block the messages in the queue.

If I remove the begin and commit transaction block, it only wait for the 3 seconds I specified.

Question: is the transaction block necessary in the activated procedure.

thanks

Paul

Monday, March 26, 2012

rebuilt option for master database

I was testing Disaster Recovery plann for master database recovery , While
rebuliding master file on test setup , I am getting following error , can u
pls let me know why does it give this error ..I am already in a single user
mode .
Rebuilt master is failed due to following error no : 266 , copy function
cannot be used
There has been sharing violations in source or destination file .
Regards,
swati
Seems like your SQL Server is still started so that rebuildm.exe can't create the database files...
Also, note that rebuildm has a bug where it can't run the source database files from the CD, as it
doesn't remove the read-only attribute.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"swati" <swati.zingade@.ugamsolutions.com> wrote in message
news:%23gE5zkwwEHA.3528@.TK2MSFTNGP10.phx.gbl...
> I was testing Disaster Recovery plann for master database recovery , While
> rebuliding master file on test setup , I am getting following error , can u
> pls let me know why does it give this error ..I am already in a single user
> mode .
> Rebuilt master is failed due to following error no : 266 , copy function
> cannot be used
> There has been sharing violations in source or destination file .
>
> Regards,
> swati
>
>
|||Hi !
sql server is in single user mode , and then i tried to rebulid the same
using rebuild option ,i am accessing the files which are present in
c:\programme files \ ..ie. a default directory . there is no read only
option for data /log files . Pls tell me what should be done ?
Regards,
Swati
"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in
message news:#$zD9pwwEHA.3824@.TK2MSFTNGP15.phx.gbl...
> Seems like your SQL Server is still started so that rebuildm.exe can't
create the database files...
> Also, note that rebuildm has a bug where it can't run the source database
files from the CD, as it[vbcol=seagreen]
> doesn't remove the read-only attribute.
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "swati" <swati.zingade@.ugamsolutions.com> wrote in message
> news:%23gE5zkwwEHA.3528@.TK2MSFTNGP10.phx.gbl...
While[vbcol=seagreen]
can u[vbcol=seagreen]
user
>
|||Seems you are using the rebildm.exe program in a completely wrong way, I'm afraid. I recommend that
you start by reading in Books Online how this program work, no need to re-phrase all the information
from Books Online as you already have the information there. And then you can of course post back
questions here is you still don't get it working.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"swati" <swati.zingade@.ugamsolutions.com> wrote in message
news:OaroX0wwEHA.2572@.tk2msftngp13.phx.gbl...
> Hi !
> sql server is in single user mode , and then i tried to rebulid the same
> using rebuild option ,i am accessing the files which are present in
> c:\programme files \ ..ie. a default directory . there is no read only
> option for data /log files . Pls tell me what should be done ?
>
> Regards,
> Swati
> "Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in
> message news:#$zD9pwwEHA.3824@.TK2MSFTNGP15.phx.gbl...
> create the database files...
> files from the CD, as it
> While
> can u
> user
>

rebuilt option for master database

I was testing Disaster Recovery plann for master database recovery , While
rebuliding master file on test setup , I am getting following error , can u
pls let me know why does it give this error ..I am already in a single user
mode .
Rebuilt master is failed due to following error no : 266 , copy function
cannot be used
There has been sharing violations in source or destination file .
Regards,
swatiSeems like your SQL Server is still started so that rebuildm.exe can't creat
e the database files...
Also, note that rebuildm has a bug where it can't run the source database fi
les from the CD, as it
doesn't remove the read-only attribute.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"swati" <swati.zingade@.ugamsolutions.com> wrote in message
news:%23gE5zkwwEHA.3528@.TK2MSFTNGP10.phx.gbl...
> I was testing Disaster Recovery plann for master database recovery , Whi
le
> rebuliding master file on test setup , I am getting following error , can
u
> pls let me know why does it give this error ..I am already in a single use
r
> mode .
> Rebuilt master is failed due to following error no : 266 , copy function
> cannot be used
> There has been sharing violations in source or destination file .
>
> Regards,
> swati
>
>|||Hi !
sql server is in single user mode , and then i tried to rebulid the same
using rebuild option ,i am accessing the files which are present in
c:\programme files \ ..ie. a default directory . there is no read only
option for data /log files . Pls tell me what should be done ?
Regards,
Swati
"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in
message news:#$zD9pwwEHA.3824@.TK2MSFTNGP15.phx.gbl...
> Seems like your SQL Server is still started so that rebuildm.exe can't
create the database files...
> Also, note that rebuildm has a bug where it can't run the source database
files from the CD, as it
> doesn't remove the read-only attribute.
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "swati" <swati.zingade@.ugamsolutions.com> wrote in message
> news:%23gE5zkwwEHA.3528@.TK2MSFTNGP10.phx.gbl...
While[vbcol=seagreen]
can u[vbcol=seagreen]
user[vbcol=seagreen]
>|||Seems you are using the rebildm.exe program in a completely wrong way, I'm a
fraid. I recommend that
you start by reading in Books Online how this program work, no need to re-ph
rase all the information
from Books Online as you already have the information there. And then you ca
n of course post back
questions here is you still don't get it working.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"swati" <swati.zingade@.ugamsolutions.com> wrote in message
news:OaroX0wwEHA.2572@.tk2msftngp13.phx.gbl...
> Hi !
> sql server is in single user mode , and then i tried to rebulid the same
> using rebuild option ,i am accessing the files which are present in
> c:\programme files \ ..ie. a default directory . there is no read only
> option for data /log files . Pls tell me what should be done ?
>
> Regards,
> Swati
> "Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote i
n
> message news:#$zD9pwwEHA.3824@.TK2MSFTNGP15.phx.gbl...
> create the database files...
> files from the CD, as it
> While
> can u
> user
>sql

rebuilt option for master database

I was testing Disaster Recovery plann for master database recovery , While
rebuliding master file on test setup , I am getting following error , can u
pls let me know why does it give this error ..I am already in a single user
mode .
Rebuilt master is failed due to following error no : 266 , copy function
cannot be used
There has been sharing violations in source or destination file .
Regards,
swatiSeems like your SQL Server is still started so that rebuildm.exe can't create the database files...
Also, note that rebuildm has a bug where it can't run the source database files from the CD, as it
doesn't remove the read-only attribute.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"swati" <swati.zingade@.ugamsolutions.com> wrote in message
news:%23gE5zkwwEHA.3528@.TK2MSFTNGP10.phx.gbl...
> I was testing Disaster Recovery plann for master database recovery , While
> rebuliding master file on test setup , I am getting following error , can u
> pls let me know why does it give this error ..I am already in a single user
> mode .
> Rebuilt master is failed due to following error no : 266 , copy function
> cannot be used
> There has been sharing violations in source or destination file .
>
> Regards,
> swati
>
>|||Hi !
sql server is in single user mode , and then i tried to rebulid the same
using rebuild option ,i am accessing the files which are present in
c:\programme files \ ..ie. a default directory . there is no read only
option for data /log files . Pls tell me what should be done ?
Regards,
Swati
"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in
message news:#$zD9pwwEHA.3824@.TK2MSFTNGP15.phx.gbl...
> Seems like your SQL Server is still started so that rebuildm.exe can't
create the database files...
> Also, note that rebuildm has a bug where it can't run the source database
files from the CD, as it
> doesn't remove the read-only attribute.
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "swati" <swati.zingade@.ugamsolutions.com> wrote in message
> news:%23gE5zkwwEHA.3528@.TK2MSFTNGP10.phx.gbl...
> > I was testing Disaster Recovery plann for master database recovery ,
While
> > rebuliding master file on test setup , I am getting following error ,
can u
> > pls let me know why does it give this error ..I am already in a single
user
> > mode .
> >
> > Rebuilt master is failed due to following error no : 266 , copy function
> > cannot be used
> > There has been sharing violations in source or destination file .
> >
> >
> > Regards,
> > swati
> >
> >
> >
>|||Seems you are using the rebildm.exe program in a completely wrong way, I'm afraid. I recommend that
you start by reading in Books Online how this program work, no need to re-phrase all the information
from Books Online as you already have the information there. And then you can of course post back
questions here is you still don't get it working.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"swati" <swati.zingade@.ugamsolutions.com> wrote in message
news:OaroX0wwEHA.2572@.tk2msftngp13.phx.gbl...
> Hi !
> sql server is in single user mode , and then i tried to rebulid the same
> using rebuild option ,i am accessing the files which are present in
> c:\programme files \ ..ie. a default directory . there is no read only
> option for data /log files . Pls tell me what should be done ?
>
> Regards,
> Swati
> "Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in
> message news:#$zD9pwwEHA.3824@.TK2MSFTNGP15.phx.gbl...
> > Seems like your SQL Server is still started so that rebuildm.exe can't
> create the database files...
> >
> > Also, note that rebuildm has a bug where it can't run the source database
> files from the CD, as it
> > doesn't remove the read-only attribute.
> >
> > --
> > Tibor Karaszi, SQL Server MVP
> > http://www.karaszi.com/sqlserver/default.asp
> > http://www.solidqualitylearning.com/
> >
> >
> > "swati" <swati.zingade@.ugamsolutions.com> wrote in message
> > news:%23gE5zkwwEHA.3528@.TK2MSFTNGP10.phx.gbl...
> > > I was testing Disaster Recovery plann for master database recovery ,
> While
> > > rebuliding master file on test setup , I am getting following error ,
> can u
> > > pls let me know why does it give this error ..I am already in a single
> user
> > > mode .
> > >
> > > Rebuilt master is failed due to following error no : 266 , copy function
> > > cannot be used
> > > There has been sharing violations in source or destination file .
> > >
> > >
> > > Regards,
> > > swati
> > >
> > >
> > >
> >
> >
>

Rebuilding the master Database

I have a copy of SQL Server Express on a clients computer.

When i try & restart the services it fails with the following error:-

Error: 9003, Severity: 20, State1.

It looks like the master database has become corrupt & needs rebuilding.

I have found a utility rebuildm.exe that repairs the master db but unfortunately it is not supported in sql 2005.

Does anyone have any other suggestions on how to repair the master db.

Thanks.

i think this will help u

http://geekswithblogs.net/mskoolaid/archive/2005/12/17/63413.aspx

Madhu

|||

Rebuildm.exe is used in sql 2000 .....i think you can rebuild as below,

copy the sql server express exe file to your local hard disk and then in cmd go to relevant path where the setup file resides and run the below code in command prompt.........

start /wait <CD or DVD Drive>\setup.exe /qn INSTANCENAME=<InstanceName> REINSTALL=SQL_Engine REBUILDDATABASE=1 SAPWD=<NewStrongPassword>

This works with sql 2005 and i think it will work with sql express also.........pls correct me if am wrong

Thanxx

Deepak

|||

Thanks for that.

Silly question but will the instance name be master & will the SAPWD be sa also do you think i need to use < > between instancename & sa

Does this look good to you guys.

setup.exe /qn INSTANCENAME=<master> REINSTALL=SQL_Engine REBUILDDATABASE=1 SAPWD=<sa>

|||

Go to the appropriate path where your sql server setup.exe exist in the command prompt........I assume its in C:\test folder .......then command will be like this,

C:\Test> start /wait \setup.exe /qn INSTANCENAME="SQLEXPRESS" REINSTALL=SQL_Engine REBUILDDATABASE=1 SAPWD="XXXXX"

SQL Server Express always installs a named instance (SQLExpress) unless you select a default instance.

http://technet.microsoft.com/en-us/library/ms143744.aspx

I am not sure if this is correct just try|||

Thanks guys.

Got it to work.

Respect.

Rebuilding the master Database

I have a copy of SQL Server Express on a clients computer.

When i try & restart the services it fails with the following error:-

Error: 9003, Severity: 20, State1.

It looks like the master database has become corrupt & needs rebuilding.

I have found a utility rebuildm.exe that repairs the master db but unfortunately it is not supported in sql 2005.

Does anyone have any other suggestions on how to repair the master db.

Thanks.

i think this will help u

http://geekswithblogs.net/mskoolaid/archive/2005/12/17/63413.aspx

Madhu

|||

Rebuildm.exe is used in sql 2000 .....i think you can rebuild as below,

copy the sql server express exe file to your local hard disk and then in cmd go to relevant path where the setup file resides and run the below code in command prompt.........

start /wait <CD or DVD Drive>\setup.exe /qn INSTANCENAME=<InstanceName> REINSTALL=SQL_Engine REBUILDDATABASE=1 SAPWD=<NewStrongPassword>

This works with sql 2005 and i think it will work with sql express also.........pls correct me if am wrong

Thanxx

Deepak

|||

Thanks for that.

Silly question but will the instance name be master & will the SAPWD be sa also do you think i need to use < > between instancename & sa

Does this look good to you guys.

setup.exe /qn INSTANCENAME=<master> REINSTALL=SQL_Engine REBUILDDATABASE=1 SAPWD=<sa>

|||

Go to the appropriate path where your sql server setup.exe exist in the command prompt........I assume its in C:\test folder .......then command will be like this,

C:\Test> start /wait \setup.exe /qn INSTANCENAME="SQLEXPRESS" REINSTALL=SQL_Engine REBUILDDATABASE=1 SAPWD="XXXXX"

SQL Server Express always installs a named instance (SQLExpress) unless you select a default instance.

http://technet.microsoft.com/en-us/library/ms143744.aspx

I am not sure if this is correct just try|||

Thanks guys.

Got it to work.

Respect.

Friday, March 23, 2012

Rebuilding a merge publication & adding a subscriber

Assume the following scenario: I have an existing publication using merge
replication with ServerA as the distributor and publisher and ServerB as a
push subscriber. I need to add ServerC as a push subscriber. In addition, the
publication has been in service for several years, and I understand that it
is desirable to start from scratch by deleting and rebuilding it. My plan is
to create an entirely new publication involving a few minor schema changes
from the existing one, again with ServerA as the distributor and publisher
and with ServerC as the sole subscriber. Once this setup proves functional, I
then plan to delete the original publication and add ServerB as a subscriber
to the new one. Here are my questions.
1. First of all, is this a workable scenario? Are there practical gotchas
that I will run into? Is there any problem with having two publications –
running side-by-side for just a few days – that are nearly identical?
2. Is there any concern that during this interim period of a few days – with
ServerA hosting both publications – the processing load on ServerA would be
excessive?
3. Can the existing distribution database serve for both publications, or is
it necessary or desirable to create a new distribution database for the new
publication?
4. All the publication articles already exist on ServerB and ServerC, but I
want to remove them from these subscribers and push them back out from
ServerA when the new subscription is established, so that a number of minor
schema changes can be included. What is the best way to remove the articles
from the subscriber servers?
Sorry for the monster posting, but I thought it best to get all this out
there at once. TIA...
You cannot publish the same table more than once. But if I understand
correctly, You are going to create a new set of articles that have
slight changes so that should not be a problem. The distributor can
handle multiple publications without any problems and many publications
can run on the same server, so long as the same tables are not
published more than once. The load would mostly be IO and network
traffic but without knowing the kind of data and server details it's
hard to say how it will impact your server. If you have all the data at
the publisher you can drop the subscriptions at the subscribers, then
drop the tables. Script the tables form the publisher and create them
on the subscriber. The merge snapshot will sync the tables and you
should be good.

Tuesday, March 20, 2012

Rebooting SQL server

We have a SQL server 2000 server residing on an iSeries intel card which is being rebooted once per week. The server log has the following two entries:

When shutting down:
1. SQL Server terminating because of system shutdown.

During startup:
1. Recovery of database "ERPDB" is complete
2. Two transactions rolled back in "ERPDB".
3. Recovery is checkpointing "ERPDB"
4. Recovery complete.

The database is 70GB and is a copy of an iSeries database. The recovery model is set to 'Simple' but I am still concerned if the above messages indicate that the server is shut down without properly waiting for replication tasks to complete?

Rgds

Bertrand

Is there any reason for the reboot every week?

The message on error log is by default as the services for SQL Server has been stopped when there are connections on SQL Server are idle or performing any activity.

The messages during the startup are also by default as it need to bring up the database and those rolledback transactions are due to when the SQL was stopping these were initiated. It is better to stop the SQL Server services when there is no such activity on server.

Wednesday, March 7, 2012

Rearrange String

I have a database where dates are stored in the following format yyyymmdd. I want to write a query where this is broken up and returned in the format mm-dd-yyyy ... Is this possible in SQL server?try this

SELECT CONVERT (datetime,, <DateFIELD> , 110)
FROM <TABLE>

Or

SELECT SUBSTR(<DateFIELD>, 5, 2) + "-" + SUBSTR(<DateFIELD>, 7, 2) + "-" + SUBSTR(<DateFIELD>, 1, 4) AS YOurField
FROM <TABLE>

<DateFIELD> - The name of your date field|||if you want mm-dd-yyyy then

select convert(varchar,getdate(),110)
select substring('20030201',5,2) + '-' + right('20030201',2) + '-' + left('20030201',4)

substitute your db field for getdate() or '20030227'|||Pardon my ignorance, but just for future reference, what is the "110" about?|||CONVERT (data_type[(length)], expression [, style])

it is the date style

0 or 100 (*) Default mon dd yyyy hh:miAM (or PM)
1 or 101 USA mm/dd/yy
2 or 102 ANSI yy.mm.dd
3 or 103 British/French dd/mm/yy
4 or 104 German dd.mm.yy
5 or 105 Italian dd-mm-yy
6 or 106 - dd mon yy
7 or 107 - mon dd, yy
8 or 108 - hh:mm:ss
9 or 109 (*) Default + milliseconds mon dd yyyy hh:mi:ss:mmmAM (or PM)
10 or 110 USA mm-dd-yy
11 or 111 JAPAN yy/mm/dd
12 or 112 ISO yymmdd
-13 or 113 (*) Europe default + milliseconds dd mon yyyy hh:mm:ss:mmm(24h)
14 or 114 - hh:mi:ss:mmm(24h)
20 or 120 (*) ODBC canonical yyyy-mm-dd hh:mi:ss(24h)
21 or 121 (*) ODBC canonical (with milliseconds) yyyy-mm-dd hh:mi:ss.mmm(24h)|||Look in bol under convert - it gives all the styles.
They affect converting to and from character format.|||Thanks guys... thats been really helpful... I have one other similar problem that ye might be able to help me with... In the same table I have a field for time and it's stored in the following format:

- 9:20am is stored as 920
- 3:30pm is stored as 1530
- 12:20am is stored as 20
- 12:05 is stored as 5

Do ye have any suggestion as to how I could call this back in the correct format?|||try:

if object_id('tempdb..#Tmp') is not null drop table #Tmp
create table #tmp(dt1 int, tm1 int,dt2 varchar(8), tm2 varchar(4))
insert into #tmp values(20030228,920,'20030228','920')
insert into #tmp values(20030228,1530,'20030228','1530')
insert into #tmp values(20030228,20,'20030228','20')
insert into #tmp values(20030228,5,'20030228','5')

select * From #tmp

select convert(varchar,cast(cast(dt1 as varchar) as datetime),110)
, substring(dt2,5,2) + '-' + right(dt2,2) + '-' + left(dt2,4)
from #tmp

select right(convert(varchar,cast('01-Jan-1753 ' +
left(right('0000' + cast(tm1 as varchar),4),2) +
':' +
right('0000' + cast(tm1 as varchar),2) as datetime)),7)
, right(convert(varchar,cast('01-Jan-1753 ' +
left(right('0000' + tm2,4),2) +
':' +
right('0000' + tm2,2) as datetime)),7)
from #tmp

and in anticipation of your next question...

select convert(varchar,cast(cast(dt1 as varchar) +
' ' +
left(right('0000' + cast(tm1 as varchar),4),2) +
':' +
right('0000' + cast(tm1 as varchar),2) as datetime), 100)
, convert(varchar,cast(substring(dt2,5,2) + '-' + right(dt2,2) + '-' + left(dt2,4) + ' ' +
left(right('0000' + cast(tm1 as varchar),4),2) +
':' +
right('0000' + cast(tm1 as varchar),2) as datetime), 100)
from #tmp|||Is there any easier way of doing this... this way seems very complicated !

Re-architect a Stored Procedure...

Hi,
I apologize if this is the wrong forum for this kind of thing.
I have inherited the following stored procedure (of some 1000 lines) and
would like to rationalize, tune, optimize or just completely re-architect
it.
Some thoughts or guidance would be appreciated.
What I'm looking for is suggestions like... Chop bits like blah, blah into
smaller procs. and stuff like that.
Cheers, Simon.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
CREATE procedure [dbo].[spNmlFMS]
as
set nocount on
set ansi_warnings off
set arithabort off
set arithignore on
declare @.PrevPeriod as tinyint
declare @.PrevPeriod_Month as tinyint
declare @.PrevPeriod_Year as smallint
declare @.PrevPeriod_Season as smallint
declare @.ThisPeriod as tinyint
declare @.ThisPeriod_Month as tinyint
declare @.ThisPeriod_Year as smallint
declare @.NextPeriod as tinyint
declare @.NextPeriod_Month as tinyint
declare @.NextPeriod_Year as smallint
declare @.ThisSeason as smallint
----
----
---
select @.ThisPeriod = (select distinct Period from drvFMS)
select @.ThisSeason = (select distinct Season from drvFMS)
select @.ThisPeriod_Month =
case @.ThisPeriod
when 1 then 6
when 2 then 7
when 3 then 8
when 4 then 9
when 5 then 10
when 6 then 11
when 7 then 12
when 8 then 1
when 9 then 2
when 10 then 3
when 11 then 4
when 12 then 5
end
select @.ThisPeriod_Year =
case @.ThisPeriod
when 1 then (@.ThisSeason - 1)
when 2 then (@.ThisSeason - 1)
when 3 then (@.ThisSeason - 1)
when 4 then (@.ThisSeason - 1)
when 5 then (@.ThisSeason - 1)
when 6 then (@.ThisSeason - 1)
when 7 then (@.ThisSeason - 1)
when 8 then @.ThisSeason
when 9 then @.ThisSeason
when 10 then @.ThisSeason
when 11 then @.ThisSeason
when 12 then @.ThisSeason
end
set @.PrevPeriod = @.ThisPeriod - 1
set @.PrevPeriod_Month = @.ThisPeriod_Month - 1
if @.PrevPeriod < 1
begin
set @.PrevPeriod = 12
end
if @.PrevPeriod_Month < 1
begin
set @.PrevPeriod_Month = 12
end
select @.PrevPeriod_Year =
case @.PrevPeriod
when 1 then (@.ThisSeason - 1)
when 2 then (@.ThisSeason - 1)
when 3 then (@.ThisSeason - 1)
when 4 then (@.ThisSeason - 1)
when 5 then (@.ThisSeason - 1)
when 6 then (@.ThisSeason - 1)
when 7 then (@.ThisSeason - 1)
when 8 then @.ThisSeason
when 9 then @.ThisSeason
when 10 then @.ThisSeason
when 11 then @.ThisSeason
when 12 then (@.ThisSeason - 1)
end
select @.PrevPeriod_Season =
case @.PrevPeriod
when 1 then @.ThisSeason
when 2 then @.ThisSeason
when 3 then @.ThisSeason
when 4 then @.ThisSeason
when 5 then @.ThisSeason
when 6 then @.ThisSeason
when 7 then @.ThisSeason
when 8 then @.ThisSeason
when 9 then @.ThisSeason
when 10 then @.ThisSeason
when 11 then @.ThisSeason
when 12 then (@.ThisSeason - 1)
end
select @.NextPeriod =
case @.ThisPeriod
when 1 then 2
when 2 then 3
when 3 then 4
when 4 then 5
when 5 then 6
when 6 then 7
when 7 then 8
when 8 then 9
when 9 then 10
when 10 then 11
when 11 then 12
when 12 then 1
end
select @.NextPeriod_Month =
case @.NextPeriod
when 1 then 6
when 2 then 7
when 3 then 8
when 4 then 9
when 5 then 10
when 6 then 11
when 7 then 12
when 8 then 1
when 9 then 2
when 10 then 3
when 11 then 4
when 12 then 5
end
----
----
---
/* aMthEndAvgCover, aPastureGrowth */
select drvFMS.FarmId, drvFMS.Season, drvFMS.Period,
tblActualPastureData.Cover as ActualCover, tblForecastPastureData.Cover as
ForecastCover
into #A1
from drvFMS
inner join tblActualPastureData on (tblActualPastureData.FarmId =
drvFMS.FarmId) and (tblActualPastureData.Season = drvFMS.Season) and
(tblActualPastureData.Period = drvFMS.Period) and
(month(tblActualPastureData.[Date]) = @.ThisPeriod_Month) and
(day(tblActualPastureData.[Date]) = 21)
inner join tblForecastPastureData on (tblForecastPastureData.FarmId =
drvFMS.FarmId) and (tblForecastPastureData.Season = drvFMS.Season) and
(tblForecastPastureData.Period = drvFMS.Period) and
(month(tblForecastPastureData.[Date]) = @.ThisPeriod_Month) and
(day(tblForecastPastureData.[Date]) = 21)
select #A1.*, tblForecastPastureData.Growth
into #A2
from #A1
inner join tblForecastPastureData on (tblForecastPastureData.FarmId =
#A1.FarmId) and (tblForecastPastureData.Season = #A1.Season) and
(tblForecastPastureData.Period = #A1.Period) and
(month(tblForecastPastureData.[Date]) = @.ThisPeriod_Month)
select #A2.FarmId, #A2.Season, #A2.Period, #A2.ActualCover,
#A2.ForecastCover, avg(cast(#A2.Growth as decimal(9,3))) as aPastureGrowth
into #A3
from #A2
group by #A2.FarmId, #A2.Season, #A2.Period, #A2.ActualCover,
#A2.ForecastCover
declare @.F as varchar(7)
declare @.S as smallint
declare @.P as tinyint
declare @.ActualCover as smallint
declare @.ForecastCover as smallint
declare @.Cover as smallint
declare @.aPastureGrowth as decimal(9,3)
declare a_Cursor cursor for
select FarmId, Season, Period, ActualCover, ForecastCover, aPastureGrowth
from #A3
open a_Cursor
fetch next from a_Cursor
into @.F, @.S, @.P, @.ActualCover, @.ForecastCover, @.aPastureGrowth
while @.@.fetch_status = 0
begin
if @.ActualCover >= 1000
Set @.Cover = @.ActualCover
else
if @.ForecastCover >= 1000
Set @.Cover = @.ForecastCover
else
Set @.Cover = 0
if exists (select FarmId, Season, Period from nmlFMS where (FarmId = @.F)
and (Season = @.S) and (Period = @.P))
begin
update nmlFMS set aMthEndAvgCover = @.Cover, aPastureGrowth =
@.aPastureGrowth
where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
end
else
begin
insert into nmlFMS (FarmId, Season, Period, aMthEndAvgCover,
aPastureGrowth) values (@.F, @.S, @.P, @.Cover, @.aPastureGrowth)
end
fetch next from a_Cursor
into @.F, @.S, @.P, @.ActualCover, @.ForecastCover, @.aPastureGrowth
end
close a_Cursor
deallocate a_Cursor
----
----
---
/* aAvgCowsMilked, aDmc1, aDmc2, aDmc3, aDmc4, aDmc5, aDmc6, aDmc7, aCrops,
aTotalDMConsumed, dmc1Description, dmc2Description, dmc3Description,
dmc4Description, dmc5Description, dmc6Description, dmc7Description */
select drvFMS.FarmId, drvFMS.Season, drvFMS.Period,
avg(cast(tblActualMilkingCowData.NoOfCows as decimal(9,3))) as
aAvgCowsMilked, avg(cast(tblActualMilkingCowData.dmc1 as decimal(9,3))) as
aDmc1avg, avg(cast(tblActualMilkingCowData.dmc2 as decimal(9,3))) as
aDmc2avg, avg(cast(tblActualMilkingCowData.dmc3 as decimal(9,3))) as
aDmc3avg, avg(cast(tblActualMilkingCowData.dmc4 as decimal(9,3))) as
aDmc4avg, avg(cast(tblActualMilkingCowData.dmc5 as decimal(9,3))) as
aDmc5avg, avg(cast(tblActualMilkingCowData.dmc6 as decimal(9,3))) as
aDmc6avg, avg(cast(tblActualMilkingCowData.dmc7 as decimal(9,3))) as
aDmc7avg, avg(cast(tblActualMilkingCowData.Crop1 as decimal(9,3))) as
aCrop1avg, avg(cast(tblActualMilkingCowData.Crop2 as decimal(9,3))) as
aCrop2avg, avg(cast(tblActualMilkingCowData.Crop3 as decimal(9,3))) as
aCrop3avg, avg(cast(tblActualMilkingCowData.Crop4 as decimal(9,3))) as
aCrop4avg, avg(cast(tblActualMilkingCowData.Crop5 as decimal(9,3))) as
aCrop5avg
into #B1
from tblActualMilkingCowData
inner join drvFMS on (tblActualMilkingCowData.FarmId = drvFMS.FarmId) and
(tblActualMilkingCowData.Season = drvFMS.Season) and
(tblActualMilkingCowData.Period = drvFMS.Period)
where (month(tblActualMilkingCowData.[Date]) = @.ThisPeriod_Month)
group by drvFMS.FarmId, drvFMS.Season, drvFMS.Period
select #B1.FarmId, #B1.Season, #B1.Period, aAvgCowsMilked, aDmc1avg,
aDmc2avg, aDmc3avg, aDmc4avg, aDmc5avg, aDmc6avg, aDmc7avg, (aCrop1avg +
aCrop2avg + aCrop3avg + aCrop4avg + aCrop5avg) as aCrops, (round(aDmc1avg,1)
+ round(aDmc2avg,1) + round(aDmc3avg,1) + round(aDmc4avg,1) +
round(aDmc5avg,1) + round(aDmc6avg,1) + round(aDmc7avg,1) +
round(aCrop1avg,1) + round(aCrop2avg,1) + round(aCrop3avg,1) +
round(aCrop4avg,1) + round(aCrop5avg,1)) as aTotalDMConsumed
into #B2
from #B1
select #B2.*, vwFarmAdditionalInfo.dmc1Description,
vwFarmAdditionalInfo.dmc2Description, vwFarmAdditionalInfo.dmc3Description,
vwFarmAdditionalInfo.dmc4Description, vwFarmAdditionalInfo.dmc5Description,
vwFarmAdditionalInfo.dmc6Description, vwFarmAdditionalInfo.dmc7Description
into #B3
from #B2
inner join vwFarmAdditionalInfo
on (vwFarmAdditionalInfo.FarmId = #B2.FarmId)
declare @.aAvgCowsMilked as smallint
declare @.aDmc1 as decimal(9,3)
declare @.aDmc2 as decimal(9,3)
declare @.aDmc3 as decimal(9,3)
declare @.aDmc4 as decimal(9,3)
declare @.aDmc5 as decimal(9,3)
declare @.aDmc6 as decimal(9,3)
declare @.aDmc7 as decimal(9,3)
declare @.aCrops as decimal(9,3)
declare @.aTotalDMConsumed as decimal(9,3)
declare @.dmc1Desc as varchar(80)
declare @.dmc2Desc as varchar(80)
declare @.dmc3Desc as varchar(80)
declare @.dmc4Desc as varchar(80)
declare @.dmc5Desc as varchar(80)
declare @.dmc6Desc as varchar(80)
declare @.dmc7Desc as varchar(80)
declare a_Cursor cursor for
select * from #B3
open a_Cursor
fetch next from a_Cursor
into @.F, @.S, @.P, @.aAvgCowsMilked, @.aDmc1, @.aDmc2, @.aDmc3, @.aDmc4, @.aDmc5,
@.aDmc6, @.aDmc7, @.aCrops, @.aTotalDMConsumed, @.dmc1Desc, @.dmc2Desc, @.dmc3Desc,
@.dmc4Desc, @.dmc5Desc, @.dmc6Desc, @.dmc7Desc
while @.@.fetch_status = 0
begin
update nmlFMS set aAvgCowsMilked = @.aAvgCowsMilked, aDmc1 = @.aDmc1, aDmc2 =
@.aDmc2, aDmc3 = @.aDmc3, aDmc4 = @.aDmc4, aDmc5 = @.aDmc5, aDmc6 = @.aDmc6,
aDmc7 = @.aDmc7, aCrops = @.aCrops, aTotalDMConsumed = @.aTotalDMConsumed,
lblDmc1 = @.dmc1Desc, lblDmc2 = @.dmc2Desc, lblDmc3 = @.dmc3Desc, lblDmc4 =
@.dmc4Desc, lblDmc5 = @.dmc5Desc, lblDmc6 = @.dmc6Desc, lblDmc7 = @.dmc7Desc
where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
fetch next from a_Cursor
into @.F, @.S, @.P, @.aAvgCowsMilked, @.aDmc1, @.aDmc2, @.aDmc3, @.aDmc4, @.aDmc5,
@.aDmc6, @.aDmc7, @.aCrops, @.aTotalDMConsumed, @.dmc1Desc, @.dmc2Desc, @.dmc3Desc,
@.dmc4Desc, @.dmc5Desc, @.dmc6Desc, @.dmc7Desc
end
close a_Cursor
deallocate a_Cursor
----
----
---
/* aSeasonToDateMS, aMonthTotalMS */
select drvFMS.FarmId, drvFMS.Season, drvFMS.Period,
month(tblActualProductionData.[Date]) as [Month],
(tblActualProductionData.Fat + tblActualProductionData.Protein) as
MilkSolids
into #C1
from tblActualProductionData
inner join drvFMS on (tblActualProductionData.FarmId = drvFMS.FarmId) and
(tblActualProductionData.Season = drvFMS.Season) and
(tblActualProductionData.Period = drvFMS.Period)
select #C1.FarmId, #C1.Season, #C1.Period, sum(#C1.MilkSolids) as
aMonthTotalMS
into #C2
from #C1
where (#C1.[Month] = @.ThisPeriod_Month)
group by FarmId, Season, Period
select #C1.FarmId, #C1.Season, #C1.Period, sum(#C1.MilkSolids) as
aSeasonToDateMS
into #C3
from #C1
where (#C1.[Month] <= @.ThisPeriod_Month)
group by FarmId, Season, Period
select #C2.FarmId, #C2.Season, #C2.Period, #C2.aMonthTotalMS,
#C3.aSeasonToDateMS
into #C4
from #C2
inner join #C3 on (#C3.FarmId = #C2.FarmId)
declare @.aMonthTotalMS as decimal(9,3)
declare @.aSeasonToDateMS as decimal(9,3)
declare a_Cursor cursor for
select * from #C4
open a_Cursor
fetch next from a_Cursor
into @.F, @.S, @.P, @.aMonthTotalMS, @.aSeasonToDateMS
while @.@.fetch_status = 0
begin
update nmlFMS set aMonthTotalMS = @.aMonthTotalMS, aSeasonToDateMS =
@.aSeasonToDateMS
where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
fetch next from a_Cursor
into @.F, @.S, @.P, @.aMonthTotalMS, @.aSeasonToDateMS
end
close a_Cursor
deallocate a_Cursor
----
----
---
/* aFeedConvEff, [aFeedAlloc/LiveWeight], [aMS/Cow/Day], [aMthMS/Ha] */
select #B3.FarmId, #B3.Season, #B3.Period, #B3.aTotalDMConsumed,
sum(tblActualProductionData.Days) as [Days]
into #D1
from #B3
inner join tblActualProductionData on (tblActualProductionData.FarmId =
#B3.FarmId) and (tblActualProductionData.Season = #B3.Season) and
(tblActualProductionData.Period = #B3.Period) and
(month(tblActualProductionData.[Date]) = @.ThisPeriod_Month)
group by #B3.FarmId, #B3.Season, #B3.Period, #B3.aTotalDMConsumed
select #D1.FarmId, #D1.Season, #D1.Period, #D1.aTotalDMConsumed, #D1.[Days],
avg(cast(tblActualPastureData.FarmSize as decimal(9,3))) as aAverageArea
into #D2
from #D1
inner join tblActualPastureData on (tblActualPastureData.FarmId =
#D1.FarmId) and (tblActualPastureData.Season = #D1.Season) and
(tblActualPastureData.Period = #D1.Period) and
(month(tblActualPastureData.[Date]) = @.ThisPeriod_Month)
group by #D1.FarmId, #D1.Season, #D1.Period, #D1.aTotalDMConsumed,
#D1.[Days]
select #D2.*, #B1.aAvgCowsMilked, #C4.aMonthTotalMS,
tblActualFarmData.FarmSize as 'aAvailableArea',
tblFarmAdditionalInfo.Liveweight
into #D3
from #D2
inner join #B1 on (#B1.FarmId = #D2.FarmId) and (#B1.Season = #D2.Season)
and (#B1.Period = #D2.Period)
inner join #C4 on (#C4.FarmId = #D2.FarmId) and (#C4.Season = #D2.Season)
and (#C4.Period = #D2.Period)
inner join tblActualFarmData on (tblActualFarmData.FarmId = #D2.FarmId) and
(tblActualFarmData.Season = #D2.Season) and (tblActualFarmData.Period =
#D2.Period)
inner join tblFarmAdditionalInfo on (tblFarmAdditionalInfo.FarmId =
#D2.FarmId)
select #D3.FarmId, #D3.Season, #D3.Period, ((#D3.aTotalDMConsumed * #D3.Days
* (#D3.aAvgCowsMilked / #D3.aAverageArea)) / (#D3.aMonthTotalMS /
#D3.aAvailableArea)) as aFeedConvEff, (#D3.aTotalDMConsumed / #D3.Liveweight
* 100) as [aFeedAlloc/LiveWeight], (#D3.aMonthTotalMS / #D3.aAvgCowsMilked /
#D3.Days) as [aMS/Cow/Day], (#D3.aMonthTotalMS / #D3.aAvailableArea) as
[aMthMS/Ha]
into #D4
from #D3
declare @.aFeedConvEff as decimal(9,3)
declare @.aFeedAllocLiveWeight as decimal(9,3)
declare @.aMSCowDay as decimal (9,3)
declare @.aMthMSHa as decimal(9,3)
declare a_Cursor cursor for
select * from #D4
open a_Cursor
fetch next from a_Cursor
into @.F, @.S, @.P, @.aFeedConvEff, @.aFeedAllocLiveWeight, @.aMSCowDay, @.aMthMSHa
while @.@.fetch_status = 0
begin
update nmlFMS set aFeedConvEff = cast(@.aFeedConvEff as decimal(6,4)),
[aFeedAlloc/LiveWeight] = cast(@.aFeedAllocLiveWeight as decimal(6,4)),
[aMS/Cow/Day] = cast(@.aMSCowDay as decimal(6,4)), [aMthMS/Ha] =
cast(@.aMthMSHa as decimal(9,3))
where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
fetch next from a_Cursor
into @.F, @.S, @.P, @.aFeedConvEff, @.aFeedAllocLiveWeight, @.aMSCowDay,
@.aMthMSHa
end
close a_Cursor
deallocate a_Cursor
----
----
---
/* [aMthFeedCosts/KgMS], rSeasonMS, rSeasonSuppUsed, rSeasonFeedCosts,
rSeasonGrossMargin */
select #D1.FarmId, #D1.Season, #D1.Period, #D1.Days,
cast(avg(tblActualMilkingCowData.NoOfCows) as smallint) as aAvgCowsMilked,
avg(tblActualMilkingCowData.dmc2 * tblFarmAdditionalInfo.dmc2$) as aDmc2$,
avg(tblActualMilkingCowData.dmc3 * tblFarmAdditionalInfo.dmc3$) as aDmc3$,
avg(tblActualMilkingCowData.dmc4 * tblFarmAdditionalInfo.dmc4$) as aDmc4$,
avg(tblActualMilkingCowData.dmc5 * tblFarmAdditionalInfo.dmc5$) as aDmc5$,
avg(tblActualMilkingCowData.dmc6 * tblFarmAdditionalInfo.dmc6$) as aDmc6$,
avg(tblActualMilkingCowData.dmc7 * tblFarmAdditionalInfo.dmc7$) as aDmc7$,
avg(tblActualMilkingCowData.Crop1 * tblFarmAdditionalInfo.Crop1$) as
aCrop1$, avg(tblActualMilkingCowData.Crop2 * tblFarmAdditionalInfo.Crop2$)
as aCrop2$, avg(tblActualMilkingCowData.Crop3 *
tblFarmAdditionalInfo.Crop3$) as aCrop3$, avg(tblActualMilkingCowData.Crop4
* tblFarmAdditionalInfo.Crop4$) as aCrop4$,
avg(tblActualMilkingCowData.Crop5 * tblFarmAdditionalInfo.Crop5$) as aCrop5$
into #E1
from #D1
inner join tblActualMilkingCowData on (tblActualMilkingCowData.FarmId =
#D1.FarmId) and (tblActualMilkingCowData.Season = #D1.Season) and
(tblActualMilkingCowData.Period = #D1.Period) and
(month(tblActualMilkingCowData.[Date]) = @.ThisPeriod_Month)
inner join tblFarmAdditionalInfo on (tblFarmAdditionalInfo.FarmId =
#D1.FarmId)
group by #D1.FarmId, #D1.Season, #D1.Period, #D1.Days
create index C2_ndx on #C2 (FarmId, Season, Period)
select #E1.FarmId, #E1.Season, #E1.Period, #E1.Days, aAvgCowsMilked,
#C2.aMonthTotalMS, (aDmc2$ + aDmc3$ + aDmc4$ + aDmc5$ + aDmc6$ + aDmc7$ +
aCrop1$ + aCrop2$ + aCrop3$ + aCrop4$ + aCrop5$) as aMthFeedCosts,
tblActualFarmData.Adjistment$ as aGrazingCosts$, (tblForecastFarmData.Fat +
tblForecastFarmData.Protein) as rSeasonMS, (tblForecastFarmData.IOFC$ -
tblForecastFarmData.CowCosts$ - tblForecastFarmData.GrossMargin$) as
aIrrigationCosts$, (tblForecastFarmData.dmc2fed +
tblForecastFarmData.dmc3fed + tblForecastFarmData.dmc4fed +
tblForecastFarmData.dmc5fed + tblForecastFarmData.dmc6fed +
tblForecastFarmData.dmc7fed) as rSeasonSuppUsed,
(tblForecastFarmData.Concentrates$ + tblForecastFarmData.Fodder$ +
tblForecastFarmData.Nitrogen$ + tblForecastFarmData.Adjistment$ +
tblForecastFarmData.Crop1$ + tblForecastFarmData.Crop2$ +
tblForecastFarmData.Crop3$ + tblForecastFarmData.Crop4$ +
tblForecastFarmData.Crop5$) as rSeasonFeedCosts,
cast(tblForecastFarmData.GrossMargin$ as decimal(11,3)) as
rSeasonGrossMargin
into #E2
from #E1
inner join #C2 on (#C2.FarmId = #E1.FarmId) and (#C2.Season = #E1.Season)
and (#C2.Period = #E1.Period)
inner join tblActualFarmData on (tblActualFarmData.FarmId = #E1.FarmId) and
(tblActualFarmData.Season = #E1.Season) and (tblActualFarmData.Period =
#E1.Period)
inner join tblForecastFarmData on (tblForecastFarmData.FarmId = #E1.FarmId)
and (tblForecastFarmData.Season = #E1.Season) and
(tblForecastFarmData.Period = #E1.Period)
select #E2.*, (((#E2.aMthFeedCosts * #E2.aAvgCowsMilked * #E2.Days) /
#E2.aMonthTotalMS) + (#E2.aGrazingCosts$ / #E2.rSeasonMS) +
(#E2.aIrrigationCosts$ / #E2.rSeasonMS)) as aMthFeedCostsKgMS
into #E3
from #E2
declare @.rSeasonMS as integer
declare @.aMthFeedCostsKgMS as decimal(9,4)
declare @.rSeasonSuppUsed as decimal(6,2)
declare @.rSeasonFeedCosts as decimal(8,2)
declare @.rSeasonGrossMargin as decimal(11,3)
declare a_Cursor cursor for
select FarmId, Season, Period, rSeasonMS, aMthFeedCostsKgMS,
rSeasonSuppUsed, rSeasonFeedCosts, rSeasonGrossMargin from #E3
open a_Cursor
fetch next from a_Cursor
into @.F, @.S, @.P, @.rSeasonMS, @.aMthFeedCostsKgMS, @.rSeasonSuppUsed,
@.rSeasonFeedCosts, @.rSeasonGrossMargin
while @.@.fetch_status = 0
begin
update nmlFMS set rSeasonMS = @.rSeasonMS, [aMthFeedCosts/KgMS] =
@.aMthFeedCostsKgMS, rSeasonSuppUsed = @.rSeasonSuppUsed, rSeasonFeedCosts =
@.rSeasonFeedCosts, rSeasonGrossMargin = @.rSeasonGrossMargin
where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
fetch next from a_Cursor
into @.F, @.S, @.P, @.rSeasonMS, @.aMthFeedCostsKgMS, @.rSeasonSuppUsed,
@.rSeasonFeedCosts, @.rSeasonGrossMargin
end
close a_Cursor
deallocate a_Cursor
----
----
---
/* tSeasonMS, tSeasonSuppUsed, tSeasonFeedCosts, tSeasonGrossMargin */
select drvFMS.FarmId, drvFMS.Season, drvFMS.Period,
tblOriginalFarmData.Adjistment$ as oGrazingCosts$, (tblOriginalFarmData.Fat
+ tblOriginalFarmData.Protein) as oSeasonMS, (tblOriginalFarmData.IOFC$ -
tblOriginalFarmData.CowCosts$ - tblOriginalFarmData.GrossMargin$) as
oIrrigationCosts$, (tblOriginalFarmData.dmc2fed +
tblOriginalFarmData.dmc3fed + tblOriginalFarmData.dmc4fed +
tblOriginalFarmData.dmc5fed + tblOriginalFarmData.dmc6fed +
tblOriginalFarmData.dmc7fed) as oSeasonSuppUsed,
(tblOriginalFarmData.Concentrates$ + tblOriginalFarmData.Fodder$ +
tblOriginalFarmData.Nitrogen$ + tblOriginalFarmData.Crop1$ +
tblOriginalFarmData.Crop2$ + tblOriginalFarmData.Crop3$ +
tblOriginalFarmData.Crop4$ + tblOriginalFarmData.Crop5$) as
oSeasonFeedCosts, (cast(tblOriginalFarmData.GrossMargin$ as decimal(11,3)) -
(cast(tblOriginalFarmData.IOFC$ as decimal(11,3)) -
cast(tblOriginalFarmData.CowCosts$ as decimal(11,3)) -
cast(tblOriginalFarmData.GrossMargin$ as decimal(11,3)))) as
oSeasonGrossMargin
into #F1
from drvFMS
inner join tblOriginalFarmData on (tblOriginalFarmData.FarmId =
drvFMS.FarmId) and (tblOriginalFarmData.Season = drvFMS.Season)
select #F1.FarmId, #F1.Season, #F1.Period, #F1.oSeasonMS as tSeasonMS,
#F1.oSeasonSuppUsed as tSeasonSuppUsed, (#F1.oSeasonFeedCosts +
#F1.oGrazingCosts$ + #F1.oIrrigationCosts$) as tSeasonFeedCosts,
#F1.oSeasonGrossMargin as tSeasonGrossMargin
into #F2
from #F1
declare @.tSeasonMS as integer
declare @.tSeasonSuppUsed as decimal(6,2)
declare @.tSeasonFeedCosts as decimal(8,2)
declare @.tSeasonGrossMargin as decimal(11,3)
declare a_Cursor cursor for
select FarmId, Season, Period, tSeasonMS, tSeasonSuppUsed, tSeasonFeedCosts,
tSeasonGrossMargin from #F2
open a_Cursor
fetch next from a_Cursor
into @.F, @.S, @.P, @.tSeasonMS, @.tSeasonSuppUsed, @.tSeasonFeedCosts,
@.tSeasonGrossMargin
while @.@.fetch_status = 0
begin
update nmlFMS set tSeasonMS = @.tSeasonMS, tSeasonSuppUsed =
@.tSeasonSuppUsed, tSeasonFeedCosts = @.tSeasonFeedCosts, tSeasonGrossMargin =
@.tSeasonGrossMargin
where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
fetch next from a_Cursor
into @.F, @.S, @.P, @.tSeasonMS, @.tSeasonSuppUsed, @.tSeasonFeedCosts,
@.tSeasonGrossMargin
end
close a_Cursor
deallocate a_Cursor
----
----
---
/* tMthEndAvgCover, tPastureGrowth */
select drvFMS.FarmId, drvFMS.Season, drvFMS.Period,
tblForecastPastureData.Cover as tMthEndAvgCover
into #G1
from drvFMS
inner join tblForecastPastureData on (tblForecastPastureData.FarmId =
drvFMS.FarmId) and (tblForecastPastureData.Season = drvFMS.Season) and
(tblForecastPastureData.Period = @.PrevPeriod) and
(month(tblForecastPastureData.[Date]) = @.ThisPeriod_Month) and
(day(tblForecastPastureData.[Date]) = 21)
select #G1.*, tblForecastPastureData.Growth
into #G2
from #G1
inner join tblForecastPastureData on (tblForecastPastureData.FarmId =
#G1.FarmId) and (tblForecastPastureData.Season = #G1.Season) and
(tblForecastPastureData.Period = @.PrevPeriod) and
(month(tblForecastPastureData.[Date]) = @.ThisPeriod_Month)
select #G2.FarmId, #G2.Season, #G2.Period, #G2.tMthEndAvgCover,
avg(cast(#G2.Growth as decimal(9,3))) as tPastureGrowth
into #G3
from #G2
group by #G2.FarmId, #G2.Season, #G2.Period, #G2.tMthEndAvgCover
declare @.tMthEndAvgCover as smallint
declare @.tPastureGrowth as decimal(9,3)
declare a_Cursor cursor for
select FarmId, Season, Period, tMthEndAvgCover, tPastureGrowth from #G3
open a_Cursor
fetch next from a_Cursor
into @.F, @.S, @.P, @.tMthEndAvgCover, @.tPastureGrowth
while @.@.fetch_status = 0
begin
update nmlFMS set tMthEndAvgCover = @.tMthEndAvgCover, tPastureGrowth =
@.tPastureGrowth
where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
fetch next from a_Cursor
into @.F, @.S, @.P, @.tMthEndAvgCover, @.tPastureGrowth
end
close a_Cursor
deallocate a_Cursor
----
----
---
/* tAvgCowsMilked, tDmc1, tDmc2, tDmc3, tDmc4, tDmc5, tDmc6, tDmc7, tCrops,
tTotalDMConsumed */
select drvFMS.FarmId, drvFMS.Season, drvFMS.Period,
avg(cast(tblForecastMilkingCowData.NoOfCows as decimal(9,3))) as
tAvgCowsMilked, avg(cast(tblForecastMilkingCowData.dmc1 as decimal(9,3))) as
tDmc1avg, avg(cast(tblForecastMilkingCowData.dmc2 as decimal(9,3))) as
tDmc2avg, avg(cast(tblForecastMilkingCowData.dmc3 as decimal(9,3))) as
tDmc3avg, avg(cast(tblForecastMilkingCowData.dmc4 as decimal(9,3))) as
tDmc4avg, avg(cast(tblForecastMilkingCowData.dmc5 as decimal(9,3))) as
tDmc5avg, avg(cast(tblForecastMilkingCowData.dmc6 as decimal(9,3))) as
tDmc6avg, avg(cast(tblForecastMilkingCowData.dmc7 as decimal(9,3))) as
tDmc7avg, avg(cast(tblForecastMilkingCowData.Crop1 as decimal(9,3))) as
tCrop1avg, avg(cast(tblForecastMilkingCowData.Crop2 as decimal(9,3))) as
tCrop2avg, avg(cast(tblForecastMilkingCowData.Crop3 as decimal(9,3))) as
tCrop3avg, avg(cast(tblForecastMilkingCowData.Crop4 as decimal(9,3))) as
tCrop4avg, avg(cast(tblForecastMilkingCowData.Crop5 as decimal(9,3))) as
tCrop5avg
into #H1
from tblForecastMilkingCowData
inner join drvFMS on (tblForecastMilkingCowData.FarmId = drvFMS.FarmId) and
(tblForecastMilkingCowData.Season = drvFMS.Season) and
(tblForecastMilkingCowData.Period = @.PrevPeriod)
where (month(tblForecastMilkingCowData.[Date]) = @.ThisPeriod_Month)
group by drvFMS.FarmId, drvFMS.Season, drvFMS.Period
select #H1.*, (tCrop1avg + tCrop2avg + tCrop3avg + tCrop4avg + tCrop5avg) as
tCrops, (round(tDmc1avg,1) + round(tDmc2avg,1) + round(tDmc3avg,1) +
round(tDmc4avg,1) + round(tDmc5avg,1) + round(tDmc6avg,1) +
round(tDmc7avg,1) + round(tCrop1avg,1) + round(tCrop2avg,1) +
round(tCrop3avg,1) + round(tCrop4avg,1) + round(tCrop5avg,1)) as
tTotalDMConsumed
into #H2
from #H1
declare @.tAvgCowsMilked as smallint
declare @.tDmc1 as decimal(9,3)
declare @.tDmc2 as decimal(9,3)
declare @.tDmc3 as decimal(9,3)
declare @.tDmc4 as decimal(9,3)
declare @.tDmc5 as decimal(9,3)
declare @.tDmc6 as decimal(9,3)
declare @.tDmc7 as decimal(9,3)
declare @.tCrops as decimal(9,3)
declare @.tTotalDMConsumed as decimal(9,3)
declare a_Cursor cursor for
select FarmId, Season, Period, tAvgCowsMilked, tDmc1avg, tDmc2avg, tDmc3avg,
tDmc4avg, tDmc5avg, tDmc6avg, tDmc7avg, tCrops, tTotalDMConsumed from #H2
open a_Cursor
fetch next from a_Cursor
into @.F, @.S, @.P, @.tAvgCowsMilked, @.tDmc1, @.tDmc2, @.tDmc3, @.tDmc4, @.tDmc5,
@.tDmc6, @.tDmc7, @.tCrops, @.tTotalDMConsumed
while @.@.fetch_status = 0
begin
update nmlFMS set tAvgCowsMilked = @.tAvgCowsMilked, tDmc1 = @.tDmc1, tDmc2 =
@.tDmc2, tDmc3 = @.tDmc3, tDmc4 = @.tDmc4, tDmc5 = @.tDmc5, tDmc6 = @.tDmc6,
tDmc7 = @.tDmc7, tCrops = @.tCrops, tTotalDMConsumed = @.tTotalDMConsumed
where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
fetch next from a_Cursor
into @.F, @.S, @.P, @.tAvgCowsMilked, @.tDmc1, @.tDmc2, @.tDmc3, @.tDmc4, @.tDmc5,
@.tDmc6, @.tDmc7, @.tCrops, @.tTotalDMConsumed
end
close a_Cursor
deallocate a_Cursor
----
----
---
/* tSeasonToDateMS, tMonthTotalMS, [tMS/Cow/Day] */
select drvFMS.FarmId, drvFMS.Season, drvFMS.Period,
month(tblForecastProductionData.[Date]) as [Month],
(tblForecastProductionData.Fat + tblForecastProductionData.Protein) as
MilkSolids, tblForecastProductionData.Days
into #I1
from tblForecastProductionData
inner join drvFMS on (tblForecastProductionData.FarmId = drvFMS.FarmId) and
(tblForecastProductionData.Season = drvFMS.Season) and
(tblForecastProductionData.Period = @.PrevPeriod)
select #I1.FarmId, #I1.Season, #I1.Period, sum(#I1.MilkSolids) as
tMonthTotalMS, sum(#I1.Days) as [Days]
into #I2
from #I1
where (#I1.[Month] = @.ThisPeriod_Month)
group by FarmId, Season, Period
create index I2_ndx on #I2 (FarmId, Season, Period)
create index H2_ndx on #H2 (FarmId, Season, Period)
select #I1.*, dbo.fnMonthToPeriod([Month]) as MonthPeriod,
#I2.tMonthTotalMS, cast((#I2.tMonthTotalMS / #H2.tAvgCowsMilked / #I2.Days)
as decimal(6,4)) as [tMS/Cow/Day]
into #I3
from #I1
inner join #I2 on #I2.FarmId = #I1.FarmId and #I2.Season = #I1.Season and
#I2.Period = #I1.Period
inner join #H2 on #H2.FarmId = #I1.FarmId and #H2.Season = #I1.Season and
#H2.Period = #I1.Period
select #I3.FarmId, #I3.Season, #I3.Period, #I3.tMonthTotalMS, [tMS/Cow/Day],
sum(#I3.MilkSolids) as tSeasonToDateMS
into #I4
from #I3
where #I3.MonthPeriod <= #I3.Period
group by FarmId, Season, Period, tMonthTotalMS, [tMS/Cow/Day]
declare @.tMonthTotalMS as decimal(9,3)
declare @.tSeasonToDateMS as decimal(9,3)
declare @.tMSCowDay as decimal(6,4)
declare a_Cursor cursor for
select * from #I4
open a_Cursor
fetch next from a_Cursor
into @.F, @.S, @.P, @.tMonthTotalMS, @.tMSCowDay, @.tSeasonToDateMS
while @.@.fetch_status = 0
begin
update nmlFMS set tMonthTotalMS = @.tMonthTotalMS, [tMS/Cow/Day] =
@.tMSCowDay, tSeasonToDateMS = @.tSeasonToDateMS
where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
fetch next from a_Cursor
into @.F, @.S, @.P, @.tMonthTotalMS, @.tMSCowDay, @.tSeasonToDateMS
end
close a_Cursor
deallocate a_Cursor
----
----
---
/* tFeedConvEff, [tFeedAlloc/LiveWeight], [tMthMS/Ha] */
select #H2.FarmId, #H2.Season, #H2.Period, #H2.tTotalDMConsumed,
sum(tblForecastProductionData.Days) as [Days]
into #J1
from #H2
inner join tblForecastProductionData on (tblForecastProductionData.FarmId =
#H2.FarmId) and (tblForecastProductionData.Season = #H2.Season) and
(tblForecastProductionData.Period = @.PrevPeriod) and
(month(tblForecastProductionData.[Date]) = @.ThisPeriod_Month)
group by #H2.FarmId, #H2.Season, #H2.Period, #H2.tTotalDMConsumed
select #J1.FarmId, #J1.Season, #J1.Period, #J1.tTotalDMConsumed, #J1.Days,
avg(cast(tblForecastPastureData.FarmSize as decimal(9,3))) as tAverageArea
into #J2
from #J1
inner join tblForecastPastureData on (tblForecastPastureData.FarmId =
#J1.FarmId) and (tblForecastPastureData.Season = #J1.Season) and
(tblForecastPastureData.Period = @.PrevPeriod) and
(month(tblForecastPastureData.[Date]) = @.ThisPeriod_Month)
group by #J1.FarmId, #J1.Season, #J1.Period, #J1.tTotalDMConsumed,
#J1.[Days]
select #J2.*, #H2.tAvgCowsMilked, #I4.tMonthTotalMS,
tblForecastFarmData.FarmSize as 'tAvailableArea',
tblFarmAdditionalInfo.Liveweight
into #J3
from #J2
inner join #H2 on (#H2.FarmId = #J2.FarmId) and (#H2.Season = #J2.Season)
and (#H2.Period = #J2.Period)
inner join #I4 on (#I4.FarmId = #J2.FarmId) and (#I4.Season = #J2.Season)
and (#I4.Period = #J2.Period)
inner join tblForecastFarmData on (tblForecastFarmData.FarmId = #J2.FarmId)
and (tblForecastFarmData.Season = #J2.Season) and
(tblForecastFarmData.Period = #J2.Period)
inner join tblFarmAdditionalInfo on (tblFarmAdditionalInfo.FarmId =
#J2.FarmId)
select #J3.FarmId, #J3.Season, #J3.Period, ((#J3.tTotalDMConsumed * #J3.Days
* (#J3.tAvgCowsMilked / #J3.tAverageArea)) / (#J3.tMonthTotalMS /
#J3.tAvailableArea)) as tFeedConvEff, (#J3.tTotalDMConsumed / #J3.Liveweight
* 100) as [tFeedAlloc/LiveWeight], (#J3.tMonthTotalMS / #J3.tAvailableArea)
as [tMthMS/Ha]
into #J4
from #J3
declare @.tFeedConvEff as decimal(9,3)
declare @.tFeedAllocLiveWeight as decimal(9,3)
declare @.tMthMSHa as decimal(9,3)
declare a_Cursor cursor for
select * from #J4
open a_Cursor
fetch next from a_Cursor
into @.F, @.S, @.P, @.tFeedConvEff, @.tFeedAllocLiveWeight, @.tMthMSHa
while @.@.fetch_status = 0
begin
update nmlFMS set tFeedConvEff = cast(@.tFeedConvEff as decimal(6,4)),
[tFeedAlloc/LiveWeight] = cast(@.tFeedAllocLiveWeight as decimal(6,4)),
[tMthMS/Ha] = @.tMthMSHa
where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
fetch next from a_Cursor
into @.F, @.S, @.P, @.tFeedConvEff, @.tFeedAllocLiveWeight, @.tMthMSHa
end
close a_Cursor
deallocate a_Cursor
----
----
---
/* [tMthFeedCosts/KgMS] */
select #J1.FarmId, #J1.Season, #J1.Period, #J1.Days,
cast(avg(tblForecastMilkingCowData.NoOfCows) as smallint) as tAvgCowsMilked,
avg(tblForecastMilkingCowData.dmc2 * tblFarmAdditionalInfo.dmc2$) as tDmc2$,
avg(tblForecastMilkingCowData.dmc3 * tblFarmAdditionalInfo.dmc3$) as tDmc3$,
avg(tblForecastMilkingCowData.dmc4 * tblFarmAdditionalInfo.dmc4$) as tDmc4$,
avg(tblForecastMilkingCowData.dmc5 * tblFarmAdditionalInfo.dmc5$) as tDmc5$,
avg(tblForecastMilkingCowData.dmc6 * tblFarmAdditionalInfo.dmc6$) as tDmc6$,
avg(tblForecastMilkingCowData.dmc7 * tblFarmAdditionalInfo.dmc7$) as tDmc7$,
avg(tblForecastMilkingCowData.Crop1 * tblFarmAdditionalInfo.Crop1$) as
tCrop1$, avg(tblForecastMilkingCowData.Crop2 * tblFarmAdditionalInfo.Crop2$)
as tCrop2$, avg(tblForecastMilkingCowData.Crop3 *
tblFarmAdditionalInfo.Crop3$) as tCrop3$,
avg(tblForecastMilkingCowData.Crop4 * tblFarmAdditionalInfo.Crop4$) as
tCrop4$, avg(tblForecastMilkingCowData.Crop5 * tblFarmAdditionalInfo.Crop5$)
as tCrop5$
into #K1
from #J1
inner join tblForecastMilkingCowData on (tblForecastMilkingCowData.FarmId =
#J1.FarmId) and (tblForecastMilkingCowData.Season = #J1.Season) and
(tblForecastMilkingCowData.Period = @.PrevPeriod) and
(month(tblForecastMilkingCowData.[Date]) = @.ThisPeriod_Month)
inner join tblFarmAdditionalInfo on (tblFarmAdditionalInfo.FarmId =
#J1.FarmId)
group by #J1.FarmId, #J1.Season, #J1.Period, #J1.Days
select #K1.FarmId, #K1.Season, #K1.Period, #K1.Days, #K1.tAvgCowsMilked,
#I2.tMonthTotalMS, (#K1.tDmc2$ + #K1.tDmc3$ + #K1.tDmc4$ + #K1.tDmc5$ +
#K1.tDmc6$ + #K1.tDmc7$ + #K1.tCrop1$ + #K1.tCrop2$ + #K1.tCrop3$ +
#K1.tCrop4$ + #K1.tCrop5$) as tMthFeedCosts, tblForecastFarmData.Adjistment$
as tGrazingCosts$, (tblForecastFarmData.Fat + tblForecastFarmData.Protein)
as tSeasonMS, (tblForecastFarmData.IOFC$ - tblForecastFarmData.CowCosts$ -
tblForecastFarmData.GrossMargin$) as tIrrigationCosts$,
nmlFMS.rSeasonSuppUsed, nmlFMS.rSeasonFeedCosts
into #K2
from #K1
inner join #I2 on (#I2.FarmId = #K1.FarmId) and (#I2.Season = #K1.Season)
and (#I2.Period = #K1.Period)
inner join tblForecastFarmData on (tblForecastFarmData.FarmId = #K1.FarmId)
and (tblForecastFarmData.Season = #K1.Season) and
(tblForecastFarmData.Period = @.PrevPeriod)
inner join nmlFMS on (nmlFMS.FarmId = #K1.FarmId) and (nmlFMS.Season =
#K1.Season) and (nmlFMS.Period = #K1.Period)
select #K2.*, (((#K2.tMthFeedCosts * #K2.tAvgCowsMilked * #K2.Days) /
#K2.tMonthTotalMS) + (#K2.tGrazingCosts$ / #K2.tSeasonMS) +
(#K2.tIrrigationCosts$ / #K2.tSeasonMS)) as tMthFeedCostsKgMS
into #K3
from #K2
declare @.tMthFeedCostsKgMS as decimal(9,4)
declare a_Cursor cursor for
select FarmId, Season, Period, tMthFeedCostsKgMS from #K3
open a_Cursor
fetch next from a_Cursor
into @.F, @.S, @.P, @.tMthFeedCostsKgMS
while @.@.fetch_status = 0
begin
update nmlFMS set [tMthFeedCosts/KgMS] = @.tMthFeedCostsKgMS
where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
fetch next from a_Cursor
into @.F, @.S, @.P, @.tMthFeedCostsKgMS
end
close a_Cursor
deallocate a_Cursor
----
----
---
/* [tSeasonMS/Ha], [rSeasonMS/Ha], [tSeasonPastureHarvest/Ha],
[rSeasonPastureHarvest/Ha], [tSeasonSuppUsed/Ha], [rSeasonSuppUsed/Ha],
[tSeasonFeedCosts/Ha], [rSeasonFeedCosts/Ha], [tSeasonGrossMargin/Ha],
[rSeasonGrossMargin/Ha] */
select drvFMS.FarmId, drvFMS.Season, drvFMS.Period, (nmlFMS.tSeasonMS /
tblOriginalFarmData.FarmSize) as [tSeasonMS/Ha], (nmlFMS.rSeasonMS /
tblForecastFarmData.FarmSize) as [rSeasonMS/Ha],
tblOriginalFarmData.Dmc1Used as [tSeasonPastureHarvest/Ha],
tblForecastFarmData.Dmc1Used as [rSeasonPastureHarvest/Ha],
(nmlFMS.tSeasonSuppUsed / tblOriginalFarmData.FarmSize) as
[tSeasonSuppUsed/Ha], (nmlFMS.rSeasonSuppUsed /
tblForecastFarmData.FarmSize) as [rSeasonSuppUsed/Ha],
(nmlFMS.tSeasonFeedCosts / tblOriginalFarmData.FarmSize) as
[tSeasonFeedCosts/Ha], (nmlFMS.rSeasonFeedCosts /
tblForecastFarmData.FarmSize) as [rSeasonFeedCosts/Ha],
(nmlFMS.tSeasonGrossMargin / tblOriginalFarmData.FarmSize) as
[tSeasonGrossMargin/Ha], (nmlFMS.rSeasonGrossMargin /
tblForecastFarmData.FarmSize) as [rSeasonGrossMargin/Ha]
into #L1
from drvFMS
inner join nmlFMS on (nmlFMS.FarmId = drvFMS.FarmId) and (nmlFMS.Season =
drvFMS.Season) and (nmlFMS.Period = drvFMS.Period)
inner join tblOriginalFarmData on (tblOriginalFarmData.FarmId =
drvFMS.FarmId) and (tblOriginalFarmData.Season = drvFMS.Season)
inner join tblForecastFarmData on (tblForecastFarmData.FarmId =
drvFMS.FarmId) and (tblForecastFarmData.Season = drvFMS.Season) and
(tblForecastFarmData.Period = drvFMS.Period)
declare @.tSeasonMSHa as integer
declare @.rSeasonMSHa as integer
declare @.tSeasonPastureHarvestHa as decimal(9,3)
declare @.rSeasonPastureHarvestHa as decimal(9,3)
declare @.tSeasonSuppUsedHa as decimal(9,3)
declare @.rSeasonSuppUsedHa as decimal(9,3)
declare @.tSeasonFeedCostsHa as decimal(9,3)
declare @.rSeasonFeedCostsHa as decimal(9,3)
declare @.tSeasonGrossMarginHa as decimal(9,3)
declare @.rSeasonGrossMarginHa as decimal(9,3)
declare a_Cursor cursor for select * from #L1
open a_Cursor
fetch next from a_Cursor
into @.F, @.S, @.P, @.tSeasonMSHa, @.rSeasonMSHa, @.tSeasonPastureHarvestHa,
@.rSeasonPastureHarvestHa, @.tSeasonSuppUsedHa, @.rSeasonSuppUsedHa,
@.tSeasonFeedCostsHa, @.rSeasonFeedCostsHa, @.tSeasonGrossMarginHa,
@.rSeasonGrossMarginHa
while @.@.fetch_status = 0
begin
update nmlFMS set [tSeasonMS/Ha] = @.tSeasonMSHa, [rSeasonMS/Ha] =
@.rSeasonMSHa, [tSeasonPastureHarvest/Ha] = @.tSeasonPastureHarvestHa,
[rSeasonPastureHarvest/Ha] = @.rSeasonPastureHarvestHa, [tSeasonSuppUsed/Ha]
= @.tSeasonSuppUsedHa, [rSeasonSuppUsed/Ha] = @.rSeasonSuppUsedHa,
[tSeasonFeedCosts/Ha] = @.tSeasonFeedCostsHa, [rSeasonFeedCosts/Ha] =
@.rSeasonFeedCostsHa, [tSeasonGrossMargin/Ha] = @.tSeasonGrossMarginHa,
[rSeasonGrossMargin/Ha] = @.rSeasonGrossMarginHa
where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
fetch next from a_Cursor
into @.F, @.S, @.P, @.tSeasonMSHa, @.rSeasonMSHa, @.tSeasonPastureHarvestHa,
@.rSeasonPastureHarvestHa, @.tSeasonSuppUsedHa, @.rSeasonSuppUsedHa,
@.tSeasonFeedCostsHa, @.rSeasonFeedCostsHa, @.tSeasonGrossMarginHa,
@.rSeasonGrossMarginHa
end
close a_Cursor
deallocate a_Cursor
----
----
---
/* [tSeasonMS/Cow], [rSeasonMS/Cow], [tSeasonPastureHarvest/Cow],
[rSeasonPastureHarvest/Cow], [tSeasonSuppUsed/Cow], [rSeasonSuppUsed/Cow],
[tSeasonFeedCosts/Cow], [rSeasonFeedCosts/Cow], [tSeasonGrossMargin/Cow],
[rSeasonGrossMargin/Cow] */
select drvFMS.FarmId, drvFMS.Season, drvFMS.Period,
max(tblForecastMilkingCowData.NoOfCows) as PeakCows
into #M1
from drvFMS
inner join tblForecastMilkingCowData on (tblForecastMilkingCowData.FarmId =
drvFMS.FarmId) and (tblForecastMilkingCowData.Season = drvFMS.Season) and
(tblForecastMilkingCowData.Period = drvFMS.Period)
group by drvFMS.FarmId, drvFMS.Season, drvFMS.Period
order by drvFMS.FarmId, drvFMS.Season, drvFMS.Period
select #M1.*, tblOriginalFarmData.FarmSize
into #M2
from #M1
inner join tblOriginalFarmData on (tblOriginalFarmData.FarmId = #M1.FarmId)
and (tblOriginalFarmData.Season = #M1.Season)
select #M2.FarmId, #M2.Season, #M2.Period, cast(round((cast(nmlFMS.tSeasonMS
as decimal(11,3)) / cast(#M2.PeakCows as decimal(11,3))),0) as integer) as
'tSeasonMS/Cow', cast(round((cast(nmlFMS.rSeasonMS as decimal(11,3)) /
cast(#M2.PeakCows as decimal(11,3))),0) as integer) as 'rSeasonMS/Cow',
(nmlFMS.[tSeasonPastureHarvest/Ha] / (#M2.PeakCows / #M2.FarmSize)) as
'tSeasonPastureHarvest/Cow', (nmlFMS.[rSeasonPastureHarvest/Ha] /
(#M2.PeakCows / #M2.FarmSize)) as 'rSeasonPastureHarvest/Cow',
(nmlFMS.tSeasonSuppUsed / #M2.PeakCows) as 'tSeasonSuppUsed/Cow',
(nmlFMS.rSeasonSuppUsed / #M2.PeakCows) as 'rSeasonSuppUsed/Cow',
(nmlFMS.tSeasonFeedCosts / #M2.PeakCows) as 'tSeasonFeedCosts/Cow',
(nmlFMS.rSeasonFeedCosts / #M2.PeakCows) as 'rSeasonFeedCosts/Cow',
(nmlFMS.tSeasonGrossMargin / #M2.PeakCows) as 'tSeasonGrossMargin/Cow',
(nmlFMS.rSeasonGrossMargin / #M2.PeakCows) as 'rSeasonGrossMargin/Cow'
into #M3
from #M2
inner join nmlFMS on (nmlFMS.FarmId = #M2.FarmId) and (nmlFMS.Season =
#M2.Season) and (nmlFMS.Period = #M2.Period)
declare @.tSeasonMSCow as integer
declare @.rSeasonMSCow as integer
declare @.tSeasonPastureHarvestCow as decimal(9,3)
declare @.rSeasonPastureHarvestCow as decimal(9,3)
declare @.tSeasonSuppUsedCow as decimal(9,3)
declare @.rSeasonSuppUsedCow as decimal(9,3)
declare @.tSeasonFeedCostsCow as decimal(9,3)
declare @.rSeasonFeedCostsCow as decimal(9,3)
declare @.tSeasonGrossMarginCow as decimal(9,3)
declare @.rSeasonGrossMarginCow as decimal(9,3)
declare a_Cursor cursor for select * from #M3
open a_Cursor
fetch next from a_Cursor
into @.F, @.S, @.P, @.tSeasonMSCow, @.rSeasonMSCow, @.tSeasonPastureHarvestCow,
@.rSeasonPastureHarvestCow, @.tSeasonSuppUsedCow, @.rSeasonSuppUsedCow,
@.tSeasonFeedCostsCow, @.rSeasonFeedCostsCow, @.tSeasonGrossMarginCow,
@.rSeasonGrossMarginCow
while @.@.fetch_status = 0
begin
update nmlFMS set [tSeasonMS/Cow] = @.tSeasonMSCow, [rSeasonMS/Cow] =
@.rSeasonMSCow, [tSeasonPastureHarvest/Cow] = @.tSeasonPastureHarvestCow,
[rSeasonPastureHarvest/Cow] = @.rSeasonPastureHarvestCow,
[tSeasonSuppUsed/Cow] = @.tSeasonSuppUsedCow, [rSeasonSuppUsed/Cow] =
@.rSeasonSuppUsedCow, [tSeasonFeedCosts/Cow] = @.tSeasonFeedCostsCow,
[rSeasonFeedCosts/Cow] = @.rSeasonFeedCostsCow, [tSeasonGrossMargin/Cow] =
@.tSeasonGrossMarginCow, [rSeasonGrossMargin/Cow] = @.rSeasonGrossMarginCow
where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
fetch next from a_Cursor
into @.F, @.S, @.P, @.tSeasonMSCow, @.rSeasonMSCow, @.tSeasonPastureHarvestCow,
@.rSeasonPastureHarvestCow, @.tSeasonSuppUsedCow, @.rSeasonSuppUsedCow,
@.tSeasonFeedCostsCow, @.rSeasonFeedCostsCow, @.tSeasonGrossMarginCow,
@.rSeasonGrossMarginCow
end
close a_Cursor
deallocate a_Cursor
----
----
---
/* [tSeasonPastureHarvest/KgMS], [rSeasonPastureHarvest/KgMS],
[tSeasonSuppUsed/KgMS], [rSeasonSuppUsed/KgMS], [tSeasonFeedCosts/KgMS],
[rSeasonFeedCosts/KgMS], [tSeasonGrossMargin/KgMS],
[rSeasonGrossMargin/KgMS] */
select drvFMS.FarmId, drvFMS.Season, drvFMS.Period,
((nmlFMS.[tSeasonPastureHarvest/Ha] * 1000) / nmlFMS.[tSeasonMS/Ha]) as
'tSeasonPastureHarvest/KgMS', ((nmlFMS.[rSeasonPastureHarvest/Ha] * 1000) /
nmlFMS.[rSeasonMS/Ha]) as 'rSeasonPastureHarvest/KgMS',
((nmlFMS.tSeasonSuppUsed * 1000 / tblOriginalFarmData.FarmSize) /
nmlFMS.[tSeasonMS/Ha]) as 'tSeasonSuppUsed/KgMS', ((nmlFMS.rSeasonSuppUsed *
1000 / tblOriginalFarmData.FarmSize) / nmlFMS.[rSeasonMS/Ha]) as
'rSeasonSuppUsed/KgMS', (nmlFMS.tSeasonFeedCosts / nmlFMS.tSeasonMS) as
'tSeasonFeedCosts/KgMS', (nmlFMS.rSeasonFeedCosts / nmlFMS.rSeasonMS) as
'rSeasonFeedCosts/KgMS', (nmlFMS.tSeasonGrossMargin / nmlFMS.tSeasonMS) as
'tSeasonGrossMargin/KgMS', (nmlFMS.rSeasonGrossMargin / nmlFMS.rSeasonMS) as
'rSeasonGrossMargin/KgMS'
into #N1
from drvFMS
inner join nmlFMS on (nmlFMS.FarmId = drvFMS.FarmId) and (nmlFMS.Season =
drvFMS.Season) and (nmlFMS.Period = drvFMS.Period)
inner join tblOriginalFarmData on (tblOriginalFarmData.FarmId =
drvFMS.FarmId) and (tblOriginalFarmData.Season = drvFMS.Season)
declare @.tSeasonPastureHarvestKgMS as decimal(9,3)
declare @.rSeasonPastureHarvestKgMS as decimal(9,3)
declare @.tSeasonSuppUsedKgMS as decimal(9,3)
declare @.rSeasonSuppUsedKgMS as decimal(9,3)
declare @.tSeasonFeedCostsKgMS as decimal(9,3)
declare @.rSeasonFeedCostsKgMS as decimal(9,3)
declare @.tSeasonGrossMarginKgMS as decimal(9,3)
declare @.rSeasonGrossMarginKgMS as decimal(9,3)
declare a_Cursor cursor for select * from #N1
open a_Cursor
fetch next from a_Cursor
into @.F, @.S, @.P, @.tSeasonPastureHarvestKgMS, @.rSeasonPastureHarvestKgMS,
@.tSeasonSuppUsedKgMS, @.rSeasonSuppUsedKgMS, @.tSeasonFeedCostsKgMS,
@.rSeasonFeedCostsKgMS, @.tSeasonGrossMarginKgMS, @.rSeasonGrossMarginKgMS
while @.@.fetch_status = 0
begin
update nmlFMS set [tSeasonPastureHarvest/KgMS] =
@.tSeasonPastureHarvestKgMS, [rSeasonPastureHarvest/KgMS] =
@.rSeasonPastureHarvestKgMS, [tSeasonSuppUsed/KgMS] = @.tSeasonSuppUsedKgMS,
[rSeasonSuppUsed/KgMS] = @.rSeasonSuppUsedKgMS, [tSeasonFeedCosts/KgMS] =
@.tSeasonFeedCostsKgMS, [rSeasonFeedCosts/KgMS] = @.rSeasonFeedCostsKgMS,
[tSeasonGrossMargin/KgMS] = @.tSeasonGrossMarginKgMS,
[rSeasonGrossMargin/KgMS] = @.rSeasonGrossMarginKgMS
where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
fetch next from a_Cursor
into @.F, @.S, @.P, @.tSeasonPastureHarvestKgMS, @.rSeasonPastureHarvestKgMS,
@.tSeasonSuppUsedKgMS, @.rSeasonSuppUsedKgMS, @.tSeasonFeedCostsKgMS,
@.rSeasonFeedCostsKgMS, @.tSeasonGrossMarginKgMS, @.rSeasonGrossMarginKgMS
end
close a_Cursor
deallocate a_Cursor
----
----
---
/* nmMthEndAvgCover, nmPastureGrowth */
select drvFMS.FarmId, drvFMS.Season, drvFMS.Period,
tblForecastPastureData.Cover as nmMthEndAvgCover
into #O1
from drvFMS
inner join tblForecastPastureData on (tblForecastPastureData.FarmId =
drvFMS.FarmId) and (tblForecastPastureData.Season = drvFMS.Season) and
(tblForecastPastureData.Period = drvFMS.Period) and
(month(tblForecastPastureData.[Date]) = @.NextPeriod_Month) and
(day(tblForecastPastureData.[Date]) = 21)
select #O1.*, tblForecastPastureData.Growth
into #O2
from #O1
inner join tblForecastPastureData on (tblForecastPastureData.FarmId =
#O1.FarmId) and (tblForecastPastureData.Season = #O1.Season) and
(tblForecastPastureData.Period = #O1.Period) and
(month(tblForecastPastureData.[Date]) = @.NextPeriod_Month)
select #O2.FarmId, #O2.Season, #O2.Period, #O2.nmMthEndAvgCover,
avg(cast(#O2.Growth as decimal(9,3))) as nmPastureGrowth
into #O3
from #O2
group by #O2.FarmId, #O2.Season, #O2.Period, #O2.nmMthEndAvgCover
declare @.nmMthEndAvgCover as smallint
declare @.nmPastureGrowth as decimal(9,3)
declare a_Cursor cursor for select * from #O3
open a_Cursor
fetch next from a_Cursor
into @.F, @.S, @.P, @.nmMthEndAvgCover, @.nmPastureGrowth
while @.@.fetch_status = 0
begin
update nmlFMS set nmMthEndAvgCover = @.nmMthEndAvgCover, nmPastureGrowth =
@.nmPastureGrowth
where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
fetch next from a_Cursor
into @.F, @.S, @.P, @.nmMthEndAvgCover, @.nmPastureGrowth
end
close a_Cursor
deallocate a_Cursor
----
----
---
/* nmAvgCowsMilked, nmDmc1, nmDmc2, nmDmc3, nmDmc4, nmDmc5, nmDmc6, nmDmc7,
nmCrops, nmTotalDMConsumed */
select drvFMS.FarmId, drvFMS.Season, drvFMS.Period,
avg(cast(tblForecastMilkingCowData.NoOfCows as decimal(9,3))) as
nmAvgCowsMilked, avg(cast(tblForecastMilkingCowData.dmc1 as decimal(9,3)))
as nmDmc1avg, avg(cast(tblForecastMilkingCowData.dmc2 as decimal(9,3))) as
nmDmc2avg, avg(cast(tblForecastMilkingCowData.dmc3 as decimal(9,3))) as
nmDmc3avg, avg(cast(tblForecastMilkingCowData.dmc4 as decimal(9,3))) as
nmDmc4avg, avg(cast(tblForecastMilkingCowData.dmc5 as decimal(9,3))) as
nmDmc5avg, avg(cast(tblForecastMilkingCowData.dmc6 as decimal(9,3))) as
nmDmc6avg, avg(cast(tblForecastMilkingCowData.dmc7 as decimal(9,3))) as
nmDmc7avg, avg(cast(tblForecastMilkingCowData.Crop1 as decimal(9,3))) as
nmCrop1avg, avg(cast(tblForecastMilkingCowData.Crop2 as decimal(9,3))) as
nmCrop2avg, avg(cast(tblForecastMilkingCowData.Crop3 as decimal(9,3))) as
nmCrop3avg, avg(cast(tblForecastMilkingCowData.Crop4 as decimal(9,3))) as
nmCrop4avg, avg(cast(tblForecastMilkingCowData.Crop5 as decimal(9,3))) as
nmCrop5avg
into #P1
from tblForecastMilkingCowData
inner join drvFMS on (tblForecastMilkingCowData.FarmId = drvFMS.FarmId) and
(tblForecastMilkingCowData.Season = drvFMS.Season) and
(tblForecastMilkingCowData.Period = @.ThisPeriod) and
(month(tblForecastMilkingCowData.[Date]) = @.NextPeriod_Month)
group by drvFMS.FarmId, drvFMS.Season, drvFMS.Period
select #P1.*, (nmCrop1avg + nmCrop2avg + nmCrop3avg + nmCrop4avg +
nmCrop5avg) as nmCrops, (round(nmDmc1avg,1) + round(nmDmc2avg,1) +
round(nmDmc3avg,1) + round(nmDmc4avg,1) + round(nmDmc5avg,1) +
round(nmDmc6avg,1) + round(nmDmc7avg,1) + round(nmCrop1avg,1) +
round(nmCrop2avg,1) + round(nmCrop3avg,1) + round(nmCrop4avg,1) +
round(nmCrop5avg,1)) as nmTotalDMConsumed
into #P2
from #P1
declare @.nmAvgCowsMilked as smallint
declare @.nmDmc1 as decimal(9,3)
declare @.nmDmc2 as decimal(9,3)
declare @.nmDmc3 as decimal(9,3)
declare @.nmDmc4 as decimal(9,3)
declare @.nmDmc5 as decimal(9,3)
declare @.nmDmc6 as decimal(9,3)
declare @.nmDmc7 as decimal(9,3)
declare @.nmCrops as decimal(9,3)
declare @.nmTotalDMConsumed as decimal(9,3)
declare a_Cursor cursor for
select FarmId, Season, Period, nmAvgCowsMilked, nmDmc1avg, nmDmc2avg,
nmDmc3avg, nmDmc4avg, nmDmc5avg, nmDmc6avg, nmDmc7avg, nmCrops,
nmTotalDMConsumed from #P2
open a_Cursor
fetch next from a_Cursor
into @.F, @.S, @.P, @.nmAvgCowsMilked, @.nmDmc1, @.nmDmc2, @.nmDmc3, @.nmDmc4,
@.nmDmc5, @.nmDmc6, @.nmDmc7, @.nmCrops, @.nmTotalDMConsumed
while @.@.fetch_status = 0
begin
update nmlFMS set nmAvgCowsMilked = @.nmAvgCowsMilked, nmDmc1 = @.nmDmc1,
nmDmc2 = @.nmDmc2, nmDmc3 = @.nmDmc3, nmDmc4 = @.nmDmc4, nmDmc5 = @.nmDmc5,
nmDmc6 = @.nmDmc6, nmDmc7 = @.nmDmc7, nmCrops = @.nmCrops, nmTotalDMConsumed =
@.nmTotalDMConsumed
where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
fetch next from a_Cursor
into @.F, @.S, @.P, @.nmAvgCowsMilked, @.nmDmc1, @.nmDmc2, @.nmDmc3, @.nmDmc4,
@.nmDmc5, @.nmDmc6, @.nmDmc7, @.nmCrops, @.nmTotalDMConsumed
end
close a_Cursor
deallocate a_Cursor
----
----
---
/* nmSeasonToDateMS, nmMonthTotalMS, [nmMS/Cow/Day] */
select drvFMS.FarmId, drvFMS.Season, drvFMS.Period,
month(tblForecastProductionData.[Date]) as [Month],
(tblForecastProductionData.Fat + tblForecastProductionData.Protein) as
MilkSolids, tblForecastProductionData.Days
into #Q1
from drvFMS
inner join tblForecastProductionData on (tblForecastProductionData.FarmId =
drvFMS.FarmId) and (tblForecastProductionData.Season = drvFMS.Season) and
(tblForecastProductionData.Period = @.ThisPeriod)
select #Q1.FarmId, #Q1.Season, #Q1.Period, sum(#Q1.MilkSolids) as
nmMonthTotalMS, sum(#Q1.Days) as [Days]
into #Q2
from #Q1
where (#Q1.[Month] = @.NextPeriod_Month)
group by FarmId, Season, Period
create index Q2_ndx on #Q2 (FarmId, Season, Period)
create index P2_ndx on #P2 (FarmId, Season, Period)
select #Q1.*, dbo.fnMonthToPeriod([Month]) as MonthPeriod,
#Q2.nmMonthTotalMS, cast((#Q2.nmMonthTotalMS / #P2.nmAvgCowsMilked /
#Q2.Days) as decimal(9,3)) as [nmMS/Cow/Day]
into #Q3
from #Q1
inner join #Q2 on (#Q2.FarmId = #Q1.FarmId) and (#Q2.Season = #Q1.Season)
and (#Q2.Period = #Q1.Period)
inner join #P2 on (#P2.FarmId = #Q1.FarmId) and (#P2.Season = #Q1.Season)
and (#P2.Period = #Q1.Period)
select #Q3.FarmId, #Q3.Season, #Q3.Period, #Q3.nmMonthTotalMS,
[nmMS/Cow/Day], sum(#Q3.MilkSolids) as nmSeasonToDateMS
into #Q4
from #Q3
where #Q3.MonthPeriod <= @.NextPeriod
group by FarmId, Season, Period, nmMonthTotalMS, [nmMS/Cow/Day]
declare @.nmMonthTotalMS as decimal(9,3)
declare @.nmMSCowDay as decimal(9,3)
declare @.nmSeasonToDateMS as decimal(9,3)
declare a_Cursor cursor for select * from #Q4
open a_Cursor
fetch next from a_Cursor
into @.F, @.S, @.P, @.nmMonthTotalMS, @.nmMSCowDay, @.nmSeasonToDateMS
while @.@.fetch_status = 0
begin
update nmlFMS set nmMonthTotalMS = @.nmMonthTotalMS, [nmMS/Cow/Day] =
@.nmMSCowDay, nmSeasonToDateMS = @.nmSeasonToDateMS
where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
fetch next from a_Cursor
into @.F, @.S, @.P, @.nmMonthTotalMS, @.nmMSCowDay, @.nmSeasonToDateMS
end
close a_Cursor
deallocate a_Cursor
----
----
---
/* nmFeedConvEff, [nmFeedAlloc/LiveWeight], [nmMthMS/Ha] */
select #P2.FarmId, #P2.Season, #P2.Period, #P2.nmTotalDMConsumed,
sum(tblForecastProductionData.Days) as [Days]
into #R1
from #P2
inner join tblForecastProductionData on (tblForecastProductionData.FarmId =
#P2.FarmId) and (tblForecastProductionData.Season = #P2.Season) and
(tblForecastProductionData.Period = @.ThisPeriod) and
(month(tblForecastProductionData.[Date]) = @.NextPeriod_Month)
group by #P2.FarmId, #P2.Season, #P2.Period, #P2.nmTotalDMConsumed
select #R1.FarmId, #R1.Season, #R1.Period, #R1.nmTotalDMConsumed, #R1.Days,
avg(cast(tblForecastPastureData.FarmSize as decimal(9,3))) as nmAverageArea
into #R2
from #R1
inner join tblForecastPastureData on (tblForecastPastureData.FarmId =
#R1.FarmId) and (tblForecastPastureData.Season = #R1.Season) and
(tblForecastPastureData.Period = @.ThisPeriod) and
(month(tblForecastPastureData.[Date]) = @.NextPeriod_Month)
group by #R1.FarmId, #R1.Season, #R1.Period, #R1.nmTotalDMConsumed,
#R1.[Days]
select #R2.*, #P2.nmAvgCowsMilked, #Q4.nmMonthTotalMS,
tblForecastFarmData.FarmSize as 'nmAvailableArea',
tblFarmAdditionalInfo.Liveweight
into #R3
from #R2
inner join #P2 on (#P2.FarmId = #R2.FarmId) and (#P2.Season = #R2.Season)
and (#P2.Period = #R2.Period)
inner join #Q4 on (#Q4.FarmId = #R2.FarmId) and (#Q4.Season = #R2.Season)
and (#Q4.Period = #R2.Period)
inner join tblForecastFarmData on (tblForecastFarmData.FarmId = #R2.FarmId)
and (tblForecastFarmData.Season = #R2.Season) and
(tblForecastFarmData.Period = #R2.Period)
inner join tblFarmAdditionalInfo on (tblFarmAdditionalInfo.FarmId =
#R2.FarmId)
select #R3.FarmId, #R3.Season, #R3.Period, ((#R3.nmTotalDMConsumed *
#R3.Days * (#R3.nmAvgCowsMilked / #R3.nmAverageArea)) / (#R3.nmMonthTotalMS
/ #R3.nmAvailableArea)) as nmFeedConvEff, (#R3.nmTotalDMConsumed /
#R3.Liveweight * 100) as [nmFeedAlloc/LiveWeight], (#R3.nmMonthTotalMS /
#R3.nmAvailableArea) as [nmMthMS/Ha]
into #R4
from #R3
declare @.nmFeedConvEff as decimal(9,3)
declare @.nmFeedAllocLiveWeight as decimal(9,3)
declare @.nmMthMSHa as decimal(9,3)
declare a_Cursor cursor for select * from #R4
open a_Cursor
fetch next from a_Cursor
into @.F, @.S, @.P, @.nmFeedConvEff, @.nmFeedAllocLiveWeight, @.nmMthMSHa
while @.@.fetch_status = 0
begin
update nmlFMS set nmFeedConvEff = cast(@.nmFeedConvEff as decimal(6,4)),
[nmFeedAlloc/LiveWeight] = cast(@.nmFeedAllocLiveWeight as decimal(6,4)),
[nmMthMS/Ha] = @.nmMthMSHa
where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
fetch next from a_Cursor
into @.F, @.S, @.P, @.nmFeedConvEff, @.nmFeedAllocLiveWeight, @.nmMthMSHa
end
close a_Cursor
deallocate a_Cursor
----
----
---
/* [nmMthFeedCosts/KgMS] */
select #R1.FarmId, #R1.Season, #R1.Period, #R1.Days,
cast(avg(tblForecastMilkingCowData.NoOfCows) as smallint) as
nmAvgCowsMilked, avg(tblForecastMilkingCowData.dmc2 *
tblFarmAdditionalInfo.dmc2$) as nmDmc2$, avg(tblForecastMilkingCowData.dmc3
* tblFarmAdditionalInfo.dmc3$) as nmDmc3$,
avg(tblForecastMilkingCowData.dmc4 * tblFarmAdditionalInfo.dmc4$) as
nmDmc4$, avg(tblForecastMilkingCowData.dmc5 * tblFarmAdditionalInfo.dmc5$)
as nmDmc5$, avg(tblForecastMilkingCowData.dmc6 *
tblFarmAdditionalInfo.dmc6$) as nmDmc6$, avg(tblForecastMilkingCowData.dmc7
* tblFarmAdditionalInfo.dmc7$) as nmDmc7$,
avg(tblForecastMilkingCowData.Crop1 * tblFarmAdditionalInfo.Crop1$) as
nmCrop1$, avg(tblForecastMilkingCowData.Crop2 *
tblFarmAdditionalInfo.Crop2$) as nmCrop2$,
avg(tblForecastMilkingCowData.Crop3 * tblFarmAdditionalInfo.Crop3$) as
nmCrop3$, avg(tblForecastMilkingCowData.Crop4 *
tblFarmAdditionalInfo.Crop4$) as nmCrop4$,
avg(tblForecastMilkingCowData.Crop5 * tblFarmAdditionalInfo.Crop5$) as
nmCrop5$
into #S1
from #R1
inner join tblForecastMilkingCowData on (tblForecastMilkingCowData.FarmId =
#R1.FarmId) and (tblForecastMilkingCowData.Season = #R1.Season) and
(tblForecastMilkingCowData.Period = @.ThisPeriod) and
(month(tblForecastMilkingCowData.[Date]) = @.NextPeriod_Month)
inner join tblFarmAdditionalInfo on (tblFarmAdditionalInfo.FarmId =
#R1.FarmId)
group by #R1.FarmId, #R1.Season, #R1.Period, #R1.Days
select #S1.FarmId, #S1.Season, #S1.Period, #S1.Days, #S1.nmAvgCowsMilked,
#Q2.nmMonthTotalMS, (#S1.nmDmc2$ + #S1.nmDmc3$ + #S1.nmDmc4$ + #S1.nmDmc5$ +
#S1.nmDmc6$ + #S1.nmDmc7$ + #S1.nmCrop1$ + #S1.nmCrop2$ + #S1.nmCrop3$ +
#S1.nmCrop4$ + #S1.nmCrop5$) as nmMthFeedCosts,
tblForecastFarmData.Adjistment$ as nmGrazingCosts$, (tblForecastFarmData.Fat
+ tblForecastFarmData.Protein) as nmSeasonMS, (tblForecastFarmData.IOFC$ -
tblForecastFarmData.CowCosts$ - tblForecastFarmData.GrossMargin$) as
nmIrrigationCosts$, nmlFMS.rSeasonSuppUsed, nmlFMS.rSeasonFeedCosts
into #S2
from #S1
inner join #Q2 on (#Q2.FarmId = #S1.FarmId) and (#Q2.Season = #S1.Season)
and (#Q2.Period = #S1.Period)
inner join tblForecastFarmData on (tblForecastFarmData.FarmId = #S1.FarmId)
and (tblForecastFarmData.Season = #S1.Season) and
(tblForecastFarmData.Period = @.ThisPeriod)
inner join nmlFMS on (nmlFMS.FarmId = #S1.FarmId) and (nmlFMS.Season =
#S1.Season) and (nmlFMS.Period = #S1.Period)
select #S2.*, (((#S2.nmMthFeedCosts * #S2.nmAvgCowsMilked * #S2.Days) /
#S2.nmMonthTotalMS) + (#S2.nmGrazingCosts$ / #S2.nmSeasonMS) +
(#S2.nmIrrigationCosts$ / #S2.nmSeasonMS)) as nmMthFeedCostsKgMS
into #S3
from #S2
declare @.nmMthFeedCostsKgMS as decimal(9,4)
declare a_Cursor cursor for select FarmId, Season, Period,
nmMthFeedCostsKgMS from #S3
open a_Cursor
fetch next from a_Cursor
into @.F, @.S, @.P, @.nmMthFeedCostsKgMS
while @.@.fetch_status = 0
begin
update nmlFMS set [nmMthFeedCosts/KgMS] = @.nmMthFeedCostsKgMS
where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
fetch next from a_Cursor
into @.F, @.S, @.P, @.nmMthFeedCostsKgMS
end
close a_Cursor
deallocate a_Cursor
----
----
---
EndOf:
Return
GOTrash the lot! is the most constructive advice I can give. Useless
cursor code (actually I gave up, incredulous, after examining the first
10 cursors!) and all those temp tables are just a nightmare. I honestly
don't think the person who wrote this had a clue what he/she was doing.
David Portas
SQL Server MVP
--|||In news:O9XQLNyHFHA.3760@.TK2MSFTNGP12.phx.gbl,
SBeetham <sbeetham@.xtra.co.nz> said:

> I apologize if this is the wrong forum for this kind of thing.
You needn't appologize, although you may wish to s counselling having
witnessed the stored procedure you just posted. Of course you'll now be
liable for other usenet readers' damages, suffered whilst reading your post.
;-)

> I have inherited the following stored procedure (of some 1000 lines)
> and would like to rationalize, tune, optimize or just completely
> re-architect it.
> Some thoughts or guidance would be appreciated.
> What I'm looking for is suggestions like... Chop bits like blah, blah
> into smaller procs. and stuff like that.
Chop bits, yes, chop lots and lots of bits. Until there's nothing left.
Then write it properly from a spec of what the proc is supposed to do.
Changing small parts of such a monstrous routine can only lead to heartache.
Steve|||I'd go with Dave on this one; trash it. You have temp tables,
home-grown temporal data types, names that begin with "tbl-", cursors
and procedure over 50 lines long. The only classic bad programming
stunt he missed was dynamic SQL.
Start by putting the reporting periods into a calendar table and
getting the base table to use DATETIME data types. That should remove
hundreds of lines of code.|||Simon,
As frightening as this looks, I think you could begin by working
on pieces. After the first few pages, it's just a bunch of updates to
one table - updates that are written as cursors that can be rewritten
as single queries. For example,
-- This simple update
update nmlFMS set
[tSeasonPastureHarvest/KgMS] = #N2.[tSeasonPastureHarvest/KgMS]
[rSeasonPastureHarvest/KgMS] = #N2.[rSeasonPastureHarvest/KgMS]
[tSeasonSuppUsed/KgMS] = #N2.[tSeasonSuppUsed/KgMS]
[tSeasonSuppUsed/KgMS] = #N2.[tSeasonSuppUsed/KgMS]
[tSeasonFeedCosts/KgMS] = #N2.[tSeasonFeedCosts/KgMS]
[rSeasonFeedCosts/KgMS] = #N2.[rSeasonFeedCosts/KgMS]
[tSeasonGrossMargin/KgMS] = #N2.[tSeasonGrossMargin/KgMS]
[rSeasonGrossMargin/KgMS] = #N2.[rSeasonGrossMargin/KgMS]
from nmlFMS join #N2
on (nmlFMS.FarmId = #N2.FarmId)
and (nmlFMS.Season = #N2.Season)
and (nmlFMS.Period = #N2.Period)
-- should replace this cursor:
declare a_Cursor cursor for select * from #N1
open a_Cursor
fetch next from a_Cursor
into @.F, @.S, @.P, @.tSeasonPastureHarvestKgMS, @.rSeasonPastureHarvestKgMS,
@.tSeasonSuppUsedKgMS, @.rSeasonSuppUsedKgMS, @.tSeasonFeedCostsKgMS,
@.rSeasonFeedCostsKgMS, @.tSeasonGrossMarginKgMS, @.rSeasonGrossMarginKgMS
while @.@.fetch_status = 0
begin
update nmlFMS set [tSeasonPastureHarvest/KgMS] =
@.tSeasonPastureHarvestKgMS, [rSeasonPastureHarvest/KgMS] =
@.rSeasonPastureHarvestKgMS, [tSeasonSuppUsed/KgMS] = @.tSeasonSuppUsedKgMS,
[rSeasonSuppUsed/KgMS] = @.rSeasonSuppUsedKgMS, [tSeasonFeedCosts/KgMS] =
@.tSeasonFeedCostsKgMS, [rSeasonFeedCosts/KgMS] = @.rSeasonFeedCostsKgMS,
[tSeasonGrossMargin/KgMS] = @.tSeasonGrossMarginKgMS,
[rSeasonGrossMargin/KgMS] = @.rSeasonGrossMarginKgMS
where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
fetch next from a_Cursor
into @.F, @.S, @.P, @.tSeasonPastureHarvestKgMS, @.rSeasonPastureHarvestKgMS,
@.tSeasonSuppUsedKgMS, @.rSeasonSuppUsedKgMS, @.tSeasonFeedCostsKgMS,
@.rSeasonFeedCostsKgMS, @.tSeasonGrossMarginKgMS, @.rSeasonGrossMarginKgMS
end
close a_Cursor
deallocate a_Cursor
The very beginning can be simplified also. For example:
-- This
select @.ThisPeriod_Month = 1 + (@.ThisPeriod + 4) % 12
-- Is a simple replacement for this:
select @.ThisPeriod_Month =
case @.ThisPeriod
when 1 then 6
when 2 then 7
when 3 then 8
when 4 then 9
when 5 then 10
when 6 then 11
when 7 then 12
when 8 then 1
when 9 then 2
when 10 then 3
when 11 then 4
when 12 then 5
end
Once you do all this, it should be easier to picture the entire process
more clearly. While the overuse of cursors and temp tables isn't
efficient, I've seen worse - it's at least readable,
and as far as the population of the temp tables goes, maybe more
readable than a really slick set-based query for someone unfamiliar
with the business, since it takes things slowly and stepwise.
Steve Kass
Drew University
SBeetham wrote:
>Hi,
>
>I apologize if this is the wrong forum for this kind of thing.
>
>I have inherited the following stored procedure (of some 1000 lines) and
>would like to rationalize, tune, optimize or just completely re-architect
>it.
>
>Some thoughts or guidance would be appreciated.
>What I'm looking for is suggestions like... Chop bits like blah, blah into
>smaller procs. and stuff like that.
>
>
>Cheers, Simon.
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
>CREATE procedure [dbo].[spNmlFMS]
>as
>set nocount on
>set ansi_warnings off
>set arithabort off
>set arithignore on
>declare @.PrevPeriod as tinyint
>declare @.PrevPeriod_Month as tinyint
>declare @.PrevPeriod_Year as smallint
>declare @.PrevPeriod_Season as smallint
>declare @.ThisPeriod as tinyint
>declare @.ThisPeriod_Month as tinyint
>declare @.ThisPeriod_Year as smallint
>declare @.NextPeriod as tinyint
>declare @.NextPeriod_Month as tinyint
>declare @.NextPeriod_Year as smallint
>declare @.ThisSeason as smallint
>----
-
>----
-
>---
>select @.ThisPeriod = (select distinct Period from drvFMS)
>select @.ThisSeason = (select distinct Season from drvFMS)
>select @.ThisPeriod_Month =
> case @.ThisPeriod
> when 1 then 6
> when 2 then 7
> when 3 then 8
> when 4 then 9
> when 5 then 10
> when 6 then 11
> when 7 then 12
> when 8 then 1
> when 9 then 2
> when 10 then 3
> when 11 then 4
> when 12 then 5
> end
>select @.ThisPeriod_Year =
> case @.ThisPeriod
> when 1 then (@.ThisSeason - 1)
> when 2 then (@.ThisSeason - 1)
> when 3 then (@.ThisSeason - 1)
> when 4 then (@.ThisSeason - 1)
> when 5 then (@.ThisSeason - 1)
> when 6 then (@.ThisSeason - 1)
> when 7 then (@.ThisSeason - 1)
> when 8 then @.ThisSeason
> when 9 then @.ThisSeason
> when 10 then @.ThisSeason
> when 11 then @.ThisSeason
> when 12 then @.ThisSeason
> end
>set @.PrevPeriod = @.ThisPeriod - 1
>set @.PrevPeriod_Month = @.ThisPeriod_Month - 1
>if @.PrevPeriod < 1
> begin
> set @.PrevPeriod = 12
> end
>if @.PrevPeriod_Month < 1
> begin
> set @.PrevPeriod_Month = 12
> end
>select @.PrevPeriod_Year =
> case @.PrevPeriod
> when 1 then (@.ThisSeason - 1)
> when 2 then (@.ThisSeason - 1)
> when 3 then (@.ThisSeason - 1)
> when 4 then (@.ThisSeason - 1)
> when 5 then (@.ThisSeason - 1)
> when 6 then (@.ThisSeason - 1)
> when 7 then (@.ThisSeason - 1)
> when 8 then @.ThisSeason
> when 9 then @.ThisSeason
> when 10 then @.ThisSeason
> when 11 then @.ThisSeason
> when 12 then (@.ThisSeason - 1)
> end
>select @.PrevPeriod_Season =
> case @.PrevPeriod
> when 1 then @.ThisSeason
> when 2 then @.ThisSeason
> when 3 then @.ThisSeason
> when 4 then @.ThisSeason
> when 5 then @.ThisSeason
> when 6 then @.ThisSeason
> when 7 then @.ThisSeason
> when 8 then @.ThisSeason
> when 9 then @.ThisSeason
> when 10 then @.ThisSeason
> when 11 then @.ThisSeason
> when 12 then (@.ThisSeason - 1)
> end
>select @.NextPeriod =
> case @.ThisPeriod
> when 1 then 2
> when 2 then 3
> when 3 then 4
> when 4 then 5
> when 5 then 6
> when 6 then 7
> when 7 then 8
> when 8 then 9
> when 9 then 10
> when 10 then 11
> when 11 then 12
> when 12 then 1
> end
>select @.NextPeriod_Month =
> case @.NextPeriod
> when 1 then 6
> when 2 then 7
> when 3 then 8
> when 4 then 9
> when 5 then 10
> when 6 then 11
> when 7 then 12
> when 8 then 1
> when 9 then 2
> when 10 then 3
> when 11 then 4
> when 12 then 5
> end
>----
-
>----
-
>---
>/* aMthEndAvgCover, aPastureGrowth */
>select drvFMS.FarmId, drvFMS.Season, drvFMS.Period,
>tblActualPastureData.Cover as ActualCover, tblForecastPastureData.Cover as
>ForecastCover
>into #A1
>from drvFMS
>inner join tblActualPastureData on (tblActualPastureData.FarmId =
>drvFMS.FarmId) and (tblActualPastureData.Season = drvFMS.Season) and
>(tblActualPastureData.Period = drvFMS.Period) and
>(month(tblActualPastureData.[Date]) = @.ThisPeriod_Month) and
>(day(tblActualPastureData.[Date]) = 21)
>inner join tblForecastPastureData on (tblForecastPastureData.FarmId =
>drvFMS.FarmId) and (tblForecastPastureData.Season = drvFMS.Season) and
>(tblForecastPastureData.Period = drvFMS.Period) and
>(month(tblForecastPastureData.[Date]) = @.ThisPeriod_Month) and
>(day(tblForecastPastureData.[Date]) = 21)
>select #A1.*, tblForecastPastureData.Growth
>into #A2
>from #A1
>inner join tblForecastPastureData on (tblForecastPastureData.FarmId =
>#A1.FarmId) and (tblForecastPastureData.Season = #A1.Season) and
>(tblForecastPastureData.Period = #A1.Period) and
>(month(tblForecastPastureData.[Date]) = @.ThisPeriod_Month)
>select #A2.FarmId, #A2.Season, #A2.Period, #A2.ActualCover,
>#A2.ForecastCover, avg(cast(#A2.Growth as decimal(9,3))) as aPastureGrowth
>into #A3
>from #A2
>group by #A2.FarmId, #A2.Season, #A2.Period, #A2.ActualCover,
>#A2.ForecastCover
>declare @.F as varchar(7)
>declare @.S as smallint
>declare @.P as tinyint
>declare @.ActualCover as smallint
>declare @.ForecastCover as smallint
>declare @.Cover as smallint
>declare @.aPastureGrowth as decimal(9,3)
>declare a_Cursor cursor for
>select FarmId, Season, Period, ActualCover, ForecastCover, aPastureGrowth
>from #A3
>open a_Cursor
>fetch next from a_Cursor
>into @.F, @.S, @.P, @.ActualCover, @.ForecastCover, @.aPastureGrowth
>while @.@.fetch_status = 0
>begin
> if @.ActualCover >= 1000
> Set @.Cover = @.ActualCover
> else
> if @.ForecastCover >= 1000
> Set @.Cover = @.ForecastCover
> else
> Set @.Cover = 0
> if exists (select FarmId, Season, Period from nmlFMS where (FarmId = @.F)
>and (Season = @.S) and (Period = @.P))
> begin
> update nmlFMS set aMthEndAvgCover = @.Cover, aPastureGrowth =
>@.aPastureGrowth
> where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
> end
> else
> begin
> insert into nmlFMS (FarmId, Season, Period, aMthEndAvgCover,
>aPastureGrowth) values (@.F, @.S, @.P, @.Cover, @.aPastureGrowth)
> end
> fetch next from a_Cursor
> into @.F, @.S, @.P, @.ActualCover, @.ForecastCover, @.aPastureGrowth
>end
>close a_Cursor
>deallocate a_Cursor
>----
-
>----
-
>---
>/* aAvgCowsMilked, aDmc1, aDmc2, aDmc3, aDmc4, aDmc5, aDmc6, aDmc7, aCrops,
>aTotalDMConsumed, dmc1Description, dmc2Description, dmc3Description,
>dmc4Description, dmc5Description, dmc6Description, dmc7Description */
>select drvFMS.FarmId, drvFMS.Season, drvFMS.Period,
>avg(cast(tblActualMilkingCowData.NoOfCows as decimal(9,3))) as
>aAvgCowsMilked, avg(cast(tblActualMilkingCowData.dmc1 as decimal(9,3))) as
>aDmc1avg, avg(cast(tblActualMilkingCowData.dmc2 as decimal(9,3))) as
>aDmc2avg, avg(cast(tblActualMilkingCowData.dmc3 as decimal(9,3))) as
>aDmc3avg, avg(cast(tblActualMilkingCowData.dmc4 as decimal(9,3))) as
>aDmc4avg, avg(cast(tblActualMilkingCowData.dmc5 as decimal(9,3))) as
>aDmc5avg, avg(cast(tblActualMilkingCowData.dmc6 as decimal(9,3))) as
>aDmc6avg, avg(cast(tblActualMilkingCowData.dmc7 as decimal(9,3))) as
>aDmc7avg, avg(cast(tblActualMilkingCowData.Crop1 as decimal(9,3))) as
>aCrop1avg, avg(cast(tblActualMilkingCowData.Crop2 as decimal(9,3))) as
>aCrop2avg, avg(cast(tblActualMilkingCowData.Crop3 as decimal(9,3))) as
>aCrop3avg, avg(cast(tblActualMilkingCowData.Crop4 as decimal(9,3))) as
>aCrop4avg, avg(cast(tblActualMilkingCowData.Crop5 as decimal(9,3))) as
>aCrop5avg
>into #B1
>from tblActualMilkingCowData
>inner join drvFMS on (tblActualMilkingCowData.FarmId = drvFMS.FarmId) and
>(tblActualMilkingCowData.Season = drvFMS.Season) and
>(tblActualMilkingCowData.Period = drvFMS.Period)
>where (month(tblActualMilkingCowData.[Date]) = @.ThisPeriod_Month)
>group by drvFMS.FarmId, drvFMS.Season, drvFMS.Period
>select #B1.FarmId, #B1.Season, #B1.Period, aAvgCowsMilked, aDmc1avg,
>aDmc2avg, aDmc3avg, aDmc4avg, aDmc5avg, aDmc6avg, aDmc7avg, (aCrop1avg +
>aCrop2avg + aCrop3avg + aCrop4avg + aCrop5avg) as aCrops, (round(aDmc1avg,1
)
>+ round(aDmc2avg,1) + round(aDmc3avg,1) + round(aDmc4avg,1) +
>round(aDmc5avg,1) + round(aDmc6avg,1) + round(aDmc7avg,1) +
>round(aCrop1avg,1) + round(aCrop2avg,1) + round(aCrop3avg,1) +
>round(aCrop4avg,1) + round(aCrop5avg,1)) as aTotalDMConsumed
>into #B2
>from #B1
>select #B2.*, vwFarmAdditionalInfo.dmc1Description,
>vwFarmAdditionalInfo.dmc2Description, vwFarmAdditionalInfo.dmc3Description,
>vwFarmAdditionalInfo.dmc4Description, vwFarmAdditionalInfo.dmc5Description,
>vwFarmAdditionalInfo.dmc6Description, vwFarmAdditionalInfo.dmc7Description
>into #B3
>from #B2
>inner join vwFarmAdditionalInfo
>on (vwFarmAdditionalInfo.FarmId = #B2.FarmId)
>declare @.aAvgCowsMilked as smallint
>declare @.aDmc1 as decimal(9,3)
>declare @.aDmc2 as decimal(9,3)
>declare @.aDmc3 as decimal(9,3)
>declare @.aDmc4 as decimal(9,3)
>declare @.aDmc5 as decimal(9,3)
>declare @.aDmc6 as decimal(9,3)
>declare @.aDmc7 as decimal(9,3)
>declare @.aCrops as decimal(9,3)
>declare @.aTotalDMConsumed as decimal(9,3)
>declare @.dmc1Desc as varchar(80)
>declare @.dmc2Desc as varchar(80)
>declare @.dmc3Desc as varchar(80)
>declare @.dmc4Desc as varchar(80)
>declare @.dmc5Desc as varchar(80)
>declare @.dmc6Desc as varchar(80)
>declare @.dmc7Desc as varchar(80)
>declare a_Cursor cursor for
>select * from #B3
>open a_Cursor
>fetch next from a_Cursor
>into @.F, @.S, @.P, @.aAvgCowsMilked, @.aDmc1, @.aDmc2, @.aDmc3, @.aDmc4, @.aDmc5,
>@.aDmc6, @.aDmc7, @.aCrops, @.aTotalDMConsumed, @.dmc1Desc, @.dmc2Desc, @.dmc3Desc
,
>@.dmc4Desc, @.dmc5Desc, @.dmc6Desc, @.dmc7Desc
>while @.@.fetch_status = 0
>begin
> update nmlFMS set aAvgCowsMilked = @.aAvgCowsMilked, aDmc1 = @.aDmc1, aDmc2
=
>@.aDmc2, aDmc3 = @.aDmc3, aDmc4 = @.aDmc4, aDmc5 = @.aDmc5, aDmc6 = @.aDmc6,
>aDmc7 = @.aDmc7, aCrops = @.aCrops, aTotalDMConsumed = @.aTotalDMConsumed,
>lblDmc1 = @.dmc1Desc, lblDmc2 = @.dmc2Desc, lblDmc3 = @.dmc3Desc, lblDmc4 =
>@.dmc4Desc, lblDmc5 = @.dmc5Desc, lblDmc6 = @.dmc6Desc, lblDmc7 = @.dmc7Desc
> where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
> fetch next from a_Cursor
> into @.F, @.S, @.P, @.aAvgCowsMilked, @.aDmc1, @.aDmc2, @.aDmc3, @.aDmc4, @.aDmc5,
>@.aDmc6, @.aDmc7, @.aCrops, @.aTotalDMConsumed, @.dmc1Desc, @.dmc2Desc, @.dmc3Desc
,
>@.dmc4Desc, @.dmc5Desc, @.dmc6Desc, @.dmc7Desc
>end
>close a_Cursor
>deallocate a_Cursor
>----
-
>----
-
>---
>/* aSeasonToDateMS, aMonthTotalMS */
>select drvFMS.FarmId, drvFMS.Season, drvFMS.Period,
>month(tblActualProductionData.[Date]) as [Month],
>(tblActualProductionData.Fat + tblActualProductionData.Protein) as
>MilkSolids
>into #C1
>from tblActualProductionData
>inner join drvFMS on (tblActualProductionData.FarmId = drvFMS.FarmId) and
>(tblActualProductionData.Season = drvFMS.Season) and
>(tblActualProductionData.Period = drvFMS.Period)
>select #C1.FarmId, #C1.Season, #C1.Period, sum(#C1.MilkSolids) as
>aMonthTotalMS
>into #C2
>from #C1
>where (#C1.[Month] = @.ThisPeriod_Month)
>group by FarmId, Season, Period
>select #C1.FarmId, #C1.Season, #C1.Period, sum(#C1.MilkSolids) as
>aSeasonToDateMS
>into #C3
>from #C1
>where (#C1.[Month] <= @.ThisPeriod_Month)
>group by FarmId, Season, Period
>select #C2.FarmId, #C2.Season, #C2.Period, #C2.aMonthTotalMS,
>#C3.aSeasonToDateMS
>into #C4
>from #C2
>inner join #C3 on (#C3.FarmId = #C2.FarmId)
>declare @.aMonthTotalMS as decimal(9,3)
>declare @.aSeasonToDateMS as decimal(9,3)
>declare a_Cursor cursor for
>select * from #C4
>open a_Cursor
>fetch next from a_Cursor
>into @.F, @.S, @.P, @.aMonthTotalMS, @.aSeasonToDateMS
>while @.@.fetch_status = 0
>begin
> update nmlFMS set aMonthTotalMS = @.aMonthTotalMS, aSeasonToDateMS =
>@.aSeasonToDateMS
> where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
> fetch next from a_Cursor
> into @.F, @.S, @.P, @.aMonthTotalMS, @.aSeasonToDateMS
>end
>close a_Cursor
>deallocate a_Cursor
>----
-
>----
-
>---
>/* aFeedConvEff, [aFeedAlloc/LiveWeight], [aMS/Cow/Day], [aMthMS/Ha] */
>select #B3.FarmId, #B3.Season, #B3.Period, #B3.aTotalDMConsumed,
>sum(tblActualProductionData.Days) as [Days]
>into #D1
>from #B3
>inner join tblActualProductionData on (tblActualProductionData.FarmId =
>#B3.FarmId) and (tblActualProductionData.Season = #B3.Season) and
>(tblActualProductionData.Period = #B3.Period) and
>(month(tblActualProductionData.[Date]) = @.ThisPeriod_Month)
>group by #B3.FarmId, #B3.Season, #B3.Period, #B3.aTotalDMConsumed
>select #D1.FarmId, #D1.Season, #D1.Period, #D1.aTotalDMConsumed, #D1.[Days],
>avg(cast(tblActualPastureData.FarmSize as decimal(9,3))) as aAverageArea
>into #D2
>from #D1
>inner join tblActualPastureData on (tblActualPastureData.FarmId =
>#D1.FarmId) and (tblActualPastureData.Season = #D1.Season) and
>(tblActualPastureData.Period = #D1.Period) and
>(month(tblActualPastureData.[Date]) = @.ThisPeriod_Month)
>group by #D1.FarmId, #D1.Season, #D1.Period, #D1.aTotalDMConsumed,
>#D1.[Days]
>select #D2.*, #B1.aAvgCowsMilked, #C4.aMonthTotalMS,
>tblActualFarmData.FarmSize as 'aAvailableArea',
>tblFarmAdditionalInfo.Liveweight
>into #D3
>from #D2
>inner join #B1 on (#B1.FarmId = #D2.FarmId) and (#B1.Season = #D2.Season)
>and (#B1.Period = #D2.Period)
>inner join #C4 on (#C4.FarmId = #D2.FarmId) and (#C4.Season = #D2.Season)
>and (#C4.Period = #D2.Period)
>inner join tblActualFarmData on (tblActualFarmData.FarmId = #D2.FarmId) an
d
>(tblActualFarmData.Season = #D2.Season) and (tblActualFarmData.Period =
>#D2.Period)
>inner join tblFarmAdditionalInfo on (tblFarmAdditionalInfo.FarmId =
>#D2.FarmId)
>
>select #D3.FarmId, #D3.Season, #D3.Period, ((#D3.aTotalDMConsumed * #D3.Day
s
>* (#D3.aAvgCowsMilked / #D3.aAverageArea)) / (#D3.aMonthTotalMS /
>#D3.aAvailableArea)) as aFeedConvEff, (#D3.aTotalDMConsumed / #D3.Liveweigh
t
>* 100) as [aFeedAlloc/LiveWeight], (#D3.aMonthTotalMS / #D3.aAvgCowsMilked /
>#D3.Days) as [aMS/Cow/Day], (#D3.aMonthTotalMS / #D3.aAvailableArea) as
>[aMthMS/Ha]
>into #D4
>from #D3
>declare @.aFeedConvEff as decimal(9,3)
>declare @.aFeedAllocLiveWeight as decimal(9,3)
>declare @.aMSCowDay as decimal (9,3)
>declare @.aMthMSHa as decimal(9,3)
>declare a_Cursor cursor for
>select * from #D4
>open a_Cursor
>fetch next from a_Cursor
>into @.F, @.S, @.P, @.aFeedConvEff, @.aFeedAllocLiveWeight, @.aMSCowDay, @.aMthMSH
a
>while @.@.fetch_status = 0
>begin
> update nmlFMS set aFeedConvEff = cast(@.aFeedConvEff as decimal(6,4)),
>[aFeedAlloc/LiveWeight] = cast(@.aFeedAllocLiveWeight as decimal(6,4)),
>[aMS/Cow/Day] = cast(@.aMSCowDay as decimal(6,4)), [aMthMS/Ha] =
>cast(@.aMthMSHa as decimal(9,3))
> where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
> fetch next from a_Cursor
> into @.F, @.S, @.P, @.aFeedConvEff, @.aFeedAllocLiveWeight, @.aMSCowDay,
>@.aMthMSHa
>end
>close a_Cursor
>deallocate a_Cursor
>----
-
>----
-
>---
>/* [aMthFeedCosts/KgMS], rSeasonMS, rSeasonSuppUsed, rSeasonFeedCosts,
>rSeasonGrossMargin */
>select #D1.FarmId, #D1.Season, #D1.Period, #D1.Days,
>cast(avg(tblActualMilkingCowData.NoOfCows) as smallint) as aAvgCowsMilked,
>avg(tblActualMilkingCowData.dmc2 * tblFarmAdditionalInfo.dmc2$) as aDmc2$,
>avg(tblActualMilkingCowData.dmc3 * tblFarmAdditionalInfo.dmc3$) as aDmc3$,
>avg(tblActualMilkingCowData.dmc4 * tblFarmAdditionalInfo.dmc4$) as aDmc4$,
>avg(tblActualMilkingCowData.dmc5 * tblFarmAdditionalInfo.dmc5$) as aDmc5$,
>avg(tblActualMilkingCowData.dmc6 * tblFarmAdditionalInfo.dmc6$) as aDmc6$,
>avg(tblActualMilkingCowData.dmc7 * tblFarmAdditionalInfo.dmc7$) as aDmc7$,
>avg(tblActualMilkingCowData.Crop1 * tblFarmAdditionalInfo.Crop1$) as
>aCrop1$, avg(tblActualMilkingCowData.Crop2 * tblFarmAdditionalInfo.Crop2$)
>as aCrop2$, avg(tblActualMilkingCowData.Crop3 *
>tblFarmAdditionalInfo.Crop3$) as aCrop3$, avg(tblActualMilkingCowData.Crop4
>* tblFarmAdditionalInfo.Crop4$) as aCrop4$,
>avg(tblActualMilkingCowData.Crop5 * tblFarmAdditionalInfo.Crop5$) as aCrop5
$
>into #E1
>from #D1
>inner join tblActualMilkingCowData on (tblActualMilkingCowData.FarmId =
>#D1.FarmId) and (tblActualMilkingCowData.Season = #D1.Season) and
>(tblActualMilkingCowData.Period = #D1.Period) and
>(month(tblActualMilkingCowData.[Date]) = @.ThisPeriod_Month)
>inner join tblFarmAdditionalInfo on (tblFarmAdditionalInfo.FarmId =
>#D1.FarmId)
>group by #D1.FarmId, #D1.Season, #D1.Period, #D1.Days
>create index C2_ndx on #C2 (FarmId, Season, Period)
>select #E1.FarmId, #E1.Season, #E1.Period, #E1.Days, aAvgCowsMilked,
>#C2.aMonthTotalMS, (aDmc2$ + aDmc3$ + aDmc4$ + aDmc5$ + aDmc6$ + aDmc7$ +
>aCrop1$ + aCrop2$ + aCrop3$ + aCrop4$ + aCrop5$) as aMthFeedCosts,
>tblActualFarmData.Adjistment$ as aGrazingCosts$, (tblForecastFarmData.Fat +
>tblForecastFarmData.Protein) as rSeasonMS, (tblForecastFarmData.IOFC$ -
>tblForecastFarmData.CowCosts$ - tblForecastFarmData.GrossMargin$) as
>aIrrigationCosts$, (tblForecastFarmData.dmc2fed +
>tblForecastFarmData.dmc3fed + tblForecastFarmData.dmc4fed +
>tblForecastFarmData.dmc5fed + tblForecastFarmData.dmc6fed +
>tblForecastFarmData.dmc7fed) as rSeasonSuppUsed,
>(tblForecastFarmData.Concentrates$ + tblForecastFarmData.Fodder$ +
>tblForecastFarmData.Nitrogen$ + tblForecastFarmData.Adjistment$ +
>tblForecastFarmData.Crop1$ + tblForecastFarmData.Crop2$ +
>tblForecastFarmData.Crop3$ + tblForecastFarmData.Crop4$ +
>tblForecastFarmData.Crop5$) as rSeasonFeedCosts,
>cast(tblForecastFarmData.GrossMargin$ as decimal(11,3)) as
>rSeasonGrossMargin
>into #E2
>from #E1
>inner join #C2 on (#C2.FarmId = #E1.FarmId) and (#C2.Season = #E1.Season)
>and (#C2.Period = #E1.Period)
>inner join tblActualFarmData on (tblActualFarmData.FarmId = #E1.FarmId) and
>(tblActualFarmData.Season = #E1.Season) and (tblActualFarmData.Period =
>#E1.Period)
>inner join tblForecastFarmData on (tblForecastFarmData.FarmId = #E1.FarmId)
>and (tblForecastFarmData.Season = #E1.Season) and
>(tblForecastFarmData.Period = #E1.Period)
>select #E2.*, (((#E2.aMthFeedCosts * #E2.aAvgCowsMilked * #E2.Days) /
>#E2.aMonthTotalMS) + (#E2.aGrazingCosts$ / #E2.rSeasonMS) +
>(#E2.aIrrigationCosts$ / #E2.rSeasonMS)) as aMthFeedCostsKgMS
>into #E3
>from #E2
>declare @.rSeasonMS as integer
>declare @.aMthFeedCostsKgMS as decimal(9,4)
>declare @.rSeasonSuppUsed as decimal(6,2)
>declare @.rSeasonFeedCosts as decimal(8,2)
>declare @.rSeasonGrossMargin as decimal(11,3)
>declare a_Cursor cursor for
>select FarmId, Season, Period, rSeasonMS, aMthFeedCostsKgMS,
>rSeasonSuppUsed, rSeasonFeedCosts, rSeasonGrossMargin from #E3
>open a_Cursor
>fetch next from a_Cursor
>into @.F, @.S, @.P, @.rSeasonMS, @.aMthFeedCostsKgMS, @.rSeasonSuppUsed,
>@.rSeasonFeedCosts, @.rSeasonGrossMargin
>while @.@.fetch_status = 0
>begin
> update nmlFMS set rSeasonMS = @.rSeasonMS, [aMthFeedCosts/KgMS] =
>@.aMthFeedCostsKgMS, rSeasonSuppUsed = @.rSeasonSuppUsed, rSeasonFeedCosts =
>@.rSeasonFeedCosts, rSeasonGrossMargin = @.rSeasonGrossMargin
> where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
> fetch next from a_Cursor
> into @.F, @.S, @.P, @.rSeasonMS, @.aMthFeedCostsKgMS, @.rSeasonSuppUsed,
>@.rSeasonFeedCosts, @.rSeasonGrossMargin
>end
>close a_Cursor
>deallocate a_Cursor
>----
-
>----
-
>---
>/* tSeasonMS, tSeasonSuppUsed, tSeasonFeedCosts, tSeasonGrossMargin */
>select drvFMS.FarmId, drvFMS.Season, drvFMS.Period,
>tblOriginalFarmData.Adjistment$ as oGrazingCosts$, (tblOriginalFarmData.Fat
>+ tblOriginalFarmData.Protein) as oSeasonMS, (tblOriginalFarmData.IOFC$ -
>tblOriginalFarmData.CowCosts$ - tblOriginalFarmData.GrossMargin$) as
>oIrrigationCosts$, (tblOriginalFarmData.dmc2fed +
>tblOriginalFarmData.dmc3fed + tblOriginalFarmData.dmc4fed +
>tblOriginalFarmData.dmc5fed + tblOriginalFarmData.dmc6fed +
>tblOriginalFarmData.dmc7fed) as oSeasonSuppUsed,
>(tblOriginalFarmData.Concentrates$ + tblOriginalFarmData.Fodder$ +
>tblOriginalFarmData.Nitrogen$ + tblOriginalFarmData.Crop1$ +
>tblOriginalFarmData.Crop2$ + tblOriginalFarmData.Crop3$ +
>tblOriginalFarmData.Crop4$ + tblOriginalFarmData.Crop5$) as
>oSeasonFeedCosts, (cast(tblOriginalFarmData.GrossMargin$ as decimal(11,3))
-
>(cast(tblOriginalFarmData.IOFC$ as decimal(11,3)) -
>cast(tblOriginalFarmData.CowCosts$ as decimal(11,3)) -
>cast(tblOriginalFarmData.GrossMargin$ as decimal(11,3)))) as
>oSeasonGrossMargin
>into #F1
>from drvFMS
>inner join tblOriginalFarmData on (tblOriginalFarmData.FarmId =
>drvFMS.FarmId) and (tblOriginalFarmData.Season = drvFMS.Season)
>select #F1.FarmId, #F1.Season, #F1.Period, #F1.oSeasonMS as tSeasonMS,
>#F1.oSeasonSuppUsed as tSeasonSuppUsed, (#F1.oSeasonFeedCosts +
>#F1.oGrazingCosts$ + #F1.oIrrigationCosts$) as tSeasonFeedCosts,
>#F1.oSeasonGrossMargin as tSeasonGrossMargin
>into #F2
>from #F1
>declare @.tSeasonMS as integer
>declare @.tSeasonSuppUsed as decimal(6,2)
>declare @.tSeasonFeedCosts as decimal(8,2)
>declare @.tSeasonGrossMargin as decimal(11,3)
>declare a_Cursor cursor for
>select FarmId, Season, Period, tSeasonMS, tSeasonSuppUsed, tSeasonFeedCosts
,
>tSeasonGrossMargin from #F2
>open a_Cursor
>fetch next from a_Cursor
>into @.F, @.S, @.P, @.tSeasonMS, @.tSeasonSuppUsed, @.tSeasonFeedCosts,
>@.tSeasonGrossMargin
>while @.@.fetch_status = 0
>begin
> update nmlFMS set tSeasonMS = @.tSeasonMS, tSeasonSuppUsed =
>@.tSeasonSuppUsed, tSeasonFeedCosts = @.tSeasonFeedCosts, tSeasonGrossMargin
=
>@.tSeasonGrossMargin
> where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
> fetch next from a_Cursor
> into @.F, @.S, @.P, @.tSeasonMS, @.tSeasonSuppUsed, @.tSeasonFeedCosts,
>@.tSeasonGrossMargin
>end
>close a_Cursor
>deallocate a_Cursor
>----
-
>----
-
>---
>/* tMthEndAvgCover, tPastureGrowth */
>select drvFMS.FarmId, drvFMS.Season, drvFMS.Period,
>tblForecastPastureData.Cover as tMthEndAvgCover
>into #G1
>from drvFMS
>inner join tblForecastPastureData on (tblForecastPastureData.FarmId =
>drvFMS.FarmId) and (tblForecastPastureData.Season = drvFMS.Season) and
>(tblForecastPastureData.Period = @.PrevPeriod) and
>(month(tblForecastPastureData.[Date]) = @.ThisPeriod_Month) and
>(day(tblForecastPastureData.[Date]) = 21)
>select #G1.*, tblForecastPastureData.Growth
>into #G2
>from #G1
>inner join tblForecastPastureData on (tblForecastPastureData.FarmId =
>#G1.FarmId) and (tblForecastPastureData.Season = #G1.Season) and
>(tblForecastPastureData.Period = @.PrevPeriod) and
>(month(tblForecastPastureData.[Date]) = @.ThisPeriod_Month)
>select #G2.FarmId, #G2.Season, #G2.Period, #G2.tMthEndAvgCover,
>avg(cast(#G2.Growth as decimal(9,3))) as tPastureGrowth
>into #G3
>from #G2
>group by #G2.FarmId, #G2.Season, #G2.Period, #G2.tMthEndAvgCover
>declare @.tMthEndAvgCover as smallint
>declare @.tPastureGrowth as decimal(9,3)
>declare a_Cursor cursor for
>select FarmId, Season, Period, tMthEndAvgCover, tPastureGrowth from #G3
>open a_Cursor
>fetch next from a_Cursor
>into @.F, @.S, @.P, @.tMthEndAvgCover, @.tPastureGrowth
>while @.@.fetch_status = 0
>begin
> update nmlFMS set tMthEndAvgCover = @.tMthEndAvgCover, tPastureGrowth =
>@.tPastureGrowth
> where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
> fetch next from a_Cursor
> into @.F, @.S, @.P, @.tMthEndAvgCover, @.tPastureGrowth
>end
>close a_Cursor
>deallocate a_Cursor
>----
-
>----
-
>---
>/* tAvgCowsMilked, tDmc1, tDmc2, tDmc3, tDmc4, tDmc5, tDmc6, tDmc7, tCrops,
>tTotalDMConsumed */
>select drvFMS.FarmId, drvFMS.Season, drvFMS.Period,
>avg(cast(tblForecastMilkingCowData.NoOfCows as decimal(9,3))) as
>tAvgCowsMilked, avg(cast(tblForecastMilkingCowData.dmc1 as decimal(9,3))) a
s
>tDmc1avg, avg(cast(tblForecastMilkingCowData.dmc2 as decimal(9,3))) as
>tDmc2avg, avg(cast(tblForecastMilkingCowData.dmc3 as decimal(9,3))) as
>tDmc3avg, avg(cast(tblForecastMilkingCowData.dmc4 as decimal(9,3))) as
>tDmc4avg, avg(cast(tblForecastMilkingCowData.dmc5 as decimal(9,3))) as
>tDmc5avg, avg(cast(tblForecastMilkingCowData.dmc6 as decimal(9,3))) as
>tDmc6avg, avg(cast(tblForecastMilkingCowData.dmc7 as decimal(9,3))) as
>tDmc7avg, avg(cast(tblForecastMilkingCowData.Crop1 as decimal(9,3))) as
>tCrop1avg, avg(cast(tblForecastMilkingCowData.Crop2 as decimal(9,3))) as
>tCrop2avg, avg(cast(tblForecastMilkingCowData.Crop3 as decimal(9,3))) as
>tCrop3avg, avg(cast(tblForecastMilkingCowData.Crop4 as decimal(9,3))) as
>tCrop4avg, avg(cast(tblForecastMilkingCowData.Crop5 as decimal(9,3))) as
>tCrop5avg
>into #H1
>from tblForecastMilkingCowData
>inner join drvFMS on (tblForecastMilkingCowData.FarmId = drvFMS.FarmId) and
>(tblForecastMilkingCowData.Season = drvFMS.Season) and
>(tblForecastMilkingCowData.Period = @.PrevPeriod)
>where (month(tblForecastMilkingCowData.[Date]) = @.ThisPeriod_Month)
>group by drvFMS.FarmId, drvFMS.Season, drvFMS.Period
>select #H1.*, (tCrop1avg + tCrop2avg + tCrop3avg + tCrop4avg + tCrop5avg) a
s
>tCrops, (round(tDmc1avg,1) + round(tDmc2avg,1) + round(tDmc3avg,1) +
>round(tDmc4avg,1) + round(tDmc5avg,1) + round(tDmc6avg,1) +
>round(tDmc7avg,1) + round(tCrop1avg,1) + round(tCrop2avg,1) +
>round(tCrop3avg,1) + round(tCrop4avg,1) + round(tCrop5avg,1)) as
>tTotalDMConsumed
>into #H2
>from #H1
>declare @.tAvgCowsMilked as smallint
>declare @.tDmc1 as decimal(9,3)
>declare @.tDmc2 as decimal(9,3)
>declare @.tDmc3 as decimal(9,3)
>declare @.tDmc4 as decimal(9,3)
>declare @.tDmc5 as decimal(9,3)
>declare @.tDmc6 as decimal(9,3)
>declare @.tDmc7 as decimal(9,3)
>declare @.tCrops as decimal(9,3)
>declare @.tTotalDMConsumed as decimal(9,3)
>declare a_Cursor cursor for
>select FarmId, Season, Period, tAvgCowsMilked, tDmc1avg, tDmc2avg, tDmc3avg
,
>tDmc4avg, tDmc5avg, tDmc6avg, tDmc7avg, tCrops, tTotalDMConsumed from #H2
>open a_Cursor
>fetch next from a_Cursor
>into @.F, @.S, @.P, @.tAvgCowsMilked, @.tDmc1, @.tDmc2, @.tDmc3, @.tDmc4, @.tDmc5,
>@.tDmc6, @.tDmc7, @.tCrops, @.tTotalDMConsumed
>while @.@.fetch_status = 0
>begin
> update nmlFMS set tAvgCowsMilked = @.tAvgCowsMilked, tDmc1 = @.tDmc1, tDmc2
=
>@.tDmc2, tDmc3 = @.tDmc3, tDmc4 = @.tDmc4, tDmc5 = @.tDmc5, tDmc6 = @.tDmc6,
>tDmc7 = @.tDmc7, tCrops = @.tCrops, tTotalDMConsumed = @.tTotalDMConsumed
> where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
> fetch next from a_Cursor
> into @.F, @.S, @.P, @.tAvgCowsMilked, @.tDmc1, @.tDmc2, @.tDmc3, @.tDmc4, @.tDmc5,
>@.tDmc6, @.tDmc7, @.tCrops, @.tTotalDMConsumed
>end
>close a_Cursor
>deallocate a_Cursor
>----
-
>----
-
>---
>/* tSeasonToDateMS, tMonthTotalMS, [tMS/Cow/Day] */
>select drvFMS.FarmId, drvFMS.Season, drvFMS.Period,
>month(tblForecastProductionData.[Date]) as [Month],
>(tblForecastProductionData.Fat + tblForecastProductionData.Protein) as
>MilkSolids, tblForecastProductionData.Days
>into #I1
>from tblForecastProductionData
>inner join drvFMS on (tblForecastProductionData.FarmId = drvFMS.FarmId) and
>(tblForecastProductionData.Season = drvFMS.Season) and
>(tblForecastProductionData.Period = @.PrevPeriod)
>select #I1.FarmId, #I1.Season, #I1.Period, sum(#I1.MilkSolids) as
>tMonthTotalMS, sum(#I1.Days) as [Days]
>into #I2
>from #I1
>where (#I1.[Month] = @.ThisPeriod_Month)
>group by FarmId, Season, Period
>create index I2_ndx on #I2 (FarmId, Season, Period)
>create index H2_ndx on #H2 (FarmId, Season, Period)
>select #I1.*, dbo.fnMonthToPeriod([Month]) as MonthPeriod,
>#I2.tMonthTotalMS, cast((#I2.tMonthTotalMS / #H2.tAvgCowsMilked / #I2.Days)
>as decimal(6,4)) as [tMS/Cow/Day]
>into #I3
>from #I1
>inner join #I2 on #I2.FarmId = #I1.FarmId and #I2.Season = #I1.Season and
>#I2.Period = #I1.Period
>inner join #H2 on #H2.FarmId = #I1.FarmId and #H2.Season = #I1.Season and
>#H2.Period = #I1.Period
>select #I3.FarmId, #I3.Season, #I3.Period, #I3.tMonthTotalMS, [tMS/Cow/Day],
>sum(#I3.MilkSolids) as tSeasonToDateMS
>into #I4
>from #I3
>where #I3.MonthPeriod <= #I3.Period
>group by FarmId, Season, Period, tMonthTotalMS, [tMS/Cow/Day]
>declare @.tMonthTotalMS as decimal(9,3)
>declare @.tSeasonToDateMS as decimal(9,3)
>declare @.tMSCowDay as decimal(6,4)
>declare a_Cursor cursor for
>select * from #I4
>open a_Cursor
>fetch next from a_Cursor
>into @.F, @.S, @.P, @.tMonthTotalMS, @.tMSCowDay, @.tSeasonToDateMS
>while @.@.fetch_status = 0
>begin
> update nmlFMS set tMonthTotalMS = @.tMonthTotalMS, [tMS/Cow/Day] =
>@.tMSCowDay, tSeasonToDateMS = @.tSeasonToDateMS
> where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
> fetch next from a_Cursor
> into @.F, @.S, @.P, @.tMonthTotalMS, @.tMSCowDay, @.tSeasonToDateMS
>end
>close a_Cursor
>deallocate a_Cursor
>----
-
>----
-
>---
>/* tFeedConvEff, [tFeedAlloc/LiveWeight], [tMthMS/Ha] */
>select #H2.FarmId, #H2.Season, #H2.Period, #H2.tTotalDMConsumed,
>sum(tblForecastProductionData.Days) as [Days]
>into #J1
>from #H2
>inner join tblForecastProductionData on (tblForecastProductionData.FarmId =
>#H2.FarmId) and (tblForecastProductionData.Season = #H2.Season) and
>(tblForecastProductionData.Period = @.PrevPeriod) and
>(month(tblForecastProductionData.[Date]) = @.ThisPeriod_Month)
>group by #H2.FarmId, #H2.Season, #H2.Period, #H2.tTotalDMConsumed
>select #J1.FarmId, #J1.Season, #J1.Period, #J1.tTotalDMConsumed, #J1.Days,
>avg(cast(tblForecastPastureData.FarmSize as decimal(9,3))) as tAverageArea
>into #J2
>from #J1
>inner join tblForecastPastureData on (tblForecastPastureData.FarmId =
>#J1.FarmId) and (tblForecastPastureData.Season = #J1.Season) and
>(tblForecastPastureData.Period = @.PrevPeriod) and
>(month(tblForecastPastureData.[Date]) = @.ThisPeriod_Month)
>group by #J1.FarmId, #J1.Season, #J1.Period, #J1.tTotalDMConsumed,
>#J1.[Days]
>select #J2.*, #H2.tAvgCowsMilked, #I4.tMonthTotalMS,
>tblForecastFarmData.FarmSize as 'tAvailableArea',
>tblFarmAdditionalInfo.Liveweight
>into #J3
>from #J2
>inner join #H2 on (#H2.FarmId = #J2.FarmId) and (#H2.Season = #J2.Season)
>and (#H2.Period = #J2.Period)
>inner join #I4 on (#I4.FarmId = #J2.FarmId) and (#I4.Season = #J2.Season)
>and (#I4.Period = #J2.Period)
>inner join tblForecastFarmData on (tblForecastFarmData.FarmId = #J2.FarmId)
>and (tblForecastFarmData.Season = #J2.Season) and
>(tblForecastFarmData.Period = #J2.Period)
>inner join tblFarmAdditionalInfo on (tblFarmAdditionalInfo.FarmId =
>#J2.FarmId)
>select #J3.FarmId, #J3.Season, #J3.Period, ((#J3.tTotalDMConsumed * #J3.Day
s
>* (#J3.tAvgCowsMilked / #J3.tAverageArea)) / (#J3.tMonthTotalMS /
>#J3.tAvailableArea)) as tFeedConvEff, (#J3.tTotalDMConsumed / #J3.Liveweigh
t
>* 100) as [tFeedAlloc/LiveWeight], (#J3.tMonthTotalMS / #J3.tAvailableArea)
>as [tMthMS/Ha]
>into #J4
>from #J3
>declare @.tFeedConvEff as decimal(9,3)
>declare @.tFeedAllocLiveWeight as decimal(9,3)
>declare @.tMthMSHa as decimal(9,3)
>declare a_Cursor cursor for
>select * from #J4
>open a_Cursor
>fetch next from a_Cursor
>into @.F, @.S, @.P, @.tFeedConvEff, @.tFeedAllocLiveWeight, @.tMthMSHa
>while @.@.fetch_status = 0
>begin
> update nmlFMS set tFeedConvEff = cast(@.tFeedConvEff as decimal(6,4)),
>[tFeedAlloc/LiveWeight] = cast(@.tFeedAllocLiveWeight as decimal(6,4)),
>[tMthMS/Ha] = @.tMthMSHa
> where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
> fetch next from a_Cursor
> into @.F, @.S, @.P, @.tFeedConvEff, @.tFeedAllocLiveWeight, @.tMthMSHa
>end
>close a_Cursor
>deallocate a_Cursor
>----
-
>----
-
>---
>/* [tMthFeedCosts/KgMS] */
>select #J1.FarmId, #J1.Season, #J1.Period, #J1.Days,
>cast(avg(tblForecastMilkingCowData.NoOfCows) as smallint) as tAvgCowsMilked
,
>avg(tblForecastMilkingCowData.dmc2 * tblFarmAdditionalInfo.dmc2$) as tDmc2$
,
>avg(tblForecastMilkingCowData.dmc3 * tblFarmAdditionalInfo.dmc3$) as tDmc3$
,
>avg(tblForecastMilkingCowData.dmc4 * tblFarmAdditionalInfo.dmc4$) as tDmc4$
,
>avg(tblForecastMilkingCowData.dmc5 * tblFarmAdditionalInfo.dmc5$) as tDmc5$
,
>avg(tblForecastMilkingCowData.dmc6 * tblFarmAdditionalInfo.dmc6$) as tDmc6$
,
>avg(tblForecastMilkingCowData.dmc7 * tblFarmAdditionalInfo.dmc7$) as tDmc7$
,
>avg(tblForecastMilkingCowData.Crop1 * tblFarmAdditionalInfo.Crop1$) as
>tCrop1$, avg(tblForecastMilkingCowData.Crop2 * tblFarmAdditionalInfo.Crop2$
)
>as tCrop2$, avg(tblForecastMilkingCowData.Crop3 *
>tblFarmAdditionalInfo.Crop3$) as tCrop3$,
>avg(tblForecastMilkingCowData.Crop4 * tblFarmAdditionalInfo.Crop4$) as
>tCrop4$, avg(tblForecastMilkingCowData.Crop5 * tblFarmAdditionalInfo.Crop5$
)
>as tCrop5$
>into #K1
>from #J1
>inner join tblForecastMilkingCowData on (tblForecastMilkingCowData.FarmId =
>#J1.FarmId) and (tblForecastMilkingCowData.Season = #J1.Season) and
>(tblForecastMilkingCowData.Period = @.PrevPeriod) and
>(month(tblForecastMilkingCowData.[Date]) = @.ThisPeriod_Month)
>inner join tblFarmAdditionalInfo on (tblFarmAdditionalInfo.FarmId =
>#J1.FarmId)
>group by #J1.FarmId, #J1.Season, #J1.Period, #J1.Days
>select #K1.FarmId, #K1.Season, #K1.Period, #K1.Days, #K1.tAvgCowsMilked,
>#I2.tMonthTotalMS, (#K1.tDmc2$ + #K1.tDmc3$ + #K1.tDmc4$ + #K1.tDmc5$ +
>#K1.tDmc6$ + #K1.tDmc7$ + #K1.tCrop1$ + #K1.tCrop2$ + #K1.tCrop3$ +
>#K1.tCrop4$ + #K1.tCrop5$) as tMthFeedCosts, tblForecastFarmData.Adjistment
$
>as tGrazingCosts$, (tblForecastFarmData.Fat + tblForecastFarmData.Protein)
>as tSeasonMS, (tblForecastFarmData.IOFC$ - tblForecastFarmData.CowCosts$ -
>tblForecastFarmData.GrossMargin$) as tIrrigationCosts$,
>nmlFMS.rSeasonSuppUsed, nmlFMS.rSeasonFeedCosts
>into #K2
>from #K1
>inner join #I2 on (#I2.FarmId = #K1.FarmId) and (#I2.Season = #K1.Season)
>and (#I2.Period = #K1.Period)
>inner join tblForecastFarmData on (tblForecastFarmData.FarmId = #K1.FarmId)
>and (tblForecastFarmData.Season = #K1.Season) and
>(tblForecastFarmData.Period = @.PrevPeriod)
>inner join nmlFMS on (nmlFMS.FarmId = #K1.FarmId) and (nmlFMS.Season =
>#K1.Season) and (nmlFMS.Period = #K1.Period)
>select #K2.*, (((#K2.tMthFeedCosts * #K2.tAvgCowsMilked * #K2.Days) /
>#K2.tMonthTotalMS) + (#K2.tGrazingCosts$ / #K2.tSeasonMS) +
>(#K2.tIrrigationCosts$ / #K2.tSeasonMS)) as tMthFeedCostsKgMS
>into #K3
>from #K2
>declare @.tMthFeedCostsKgMS as decimal(9,4)
>declare a_Cursor cursor for
>select FarmId, Season, Period, tMthFeedCostsKgMS from #K3
>open a_Cursor
>fetch next from a_Cursor
>into @.F, @.S, @.P, @.tMthFeedCostsKgMS
>while @.@.fetch_status = 0
>begin
> update nmlFMS set [tMthFeedCosts/KgMS] = @.tMthFeedCostsKgMS
> where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
> fetch next from a_Cursor
> into @.F, @.S, @.P, @.tMthFeedCostsKgMS
>end
>close a_Cursor
>deallocate a_Cursor
>----
-
>----
-
>---
>/* [tSeasonMS/Ha], [rSeasonMS/Ha], [tSeasonPastureHarvest/Ha],
>[rSeasonPastureHarvest/Ha], [tSeasonSuppUsed/Ha], [rSeasonSuppUsed/Ha],
>[tSeasonFeedCosts/Ha], [rSeasonFeedCosts/Ha], [tSeasonGrossMargin/Ha],
>[rSeasonGrossMargin/Ha] */
>select drvFMS.FarmId, drvFMS.Season, drvFMS.Period, (nmlFMS.tSeasonMS /
>tblOriginalFarmData.FarmSize) as [tSeasonMS/Ha], (nmlFMS.rSeasonMS /
>tblForecastFarmData.FarmSize) as [rSeasonMS/Ha],
>tblOriginalFarmData.Dmc1Used as [tSeasonPastureHarvest/Ha],
>tblForecastFarmData.Dmc1Used as [rSeasonPastureHarvest/Ha],
>(nmlFMS.tSeasonSuppUsed / tblOriginalFarmData.FarmSize) as
>[tSeasonSuppUsed/Ha], (nmlFMS.rSeasonSuppUsed /
>tblForecastFarmData.FarmSize) as [rSeasonSuppUsed/Ha],
>(nmlFMS.tSeasonFeedCosts / tblOriginalFarmData.FarmSize) as
>[tSeasonFeedCosts/Ha], (nmlFMS.rSeasonFeedCosts /
>tblForecastFarmData.FarmSize) as [rSeasonFeedCosts/Ha],
>(nmlFMS.tSeasonGrossMargin / tblOriginalFarmData.FarmSize) as
>[tSeasonGrossMargin/Ha], (nmlFMS.rSeasonGrossMargin /
>tblForecastFarmData.FarmSize) as [rSeasonGrossMargin/Ha]
>into #L1
>from drvFMS
>inner join nmlFMS on (nmlFMS.FarmId = drvFMS.FarmId) and (nmlFMS.Season =
>drvFMS.Season) and (nmlFMS.Period = drvFMS.Period)
>inner join tblOriginalFarmData on (tblOriginalFarmData.FarmId =
>drvFMS.FarmId) and (tblOriginalFarmData.Season = drvFMS.Season)
>inner join tblForecastFarmData on (tblForecastFarmData.FarmId =
>drvFMS.FarmId) and (tblForecastFarmData.Season = drvFMS.Season) and
>(tblForecastFarmData.Period = drvFMS.Period)
>declare @.tSeasonMSHa as integer
>declare @.rSeasonMSHa as integer
>declare @.tSeasonPastureHarvestHa as decimal(9,3)
>declare @.rSeasonPastureHarvestHa as decimal(9,3)
>declare @.tSeasonSuppUsedHa as decimal(9,3)
>declare @.rSeasonSuppUsedHa as decimal(9,3)
>declare @.tSeasonFeedCostsHa as decimal(9,3)
>declare @.rSeasonFeedCostsHa as decimal(9,3)
>declare @.tSeasonGrossMarginHa as decimal(9,3)
>declare @.rSeasonGrossMarginHa as decimal(9,3)
>declare a_Cursor cursor for select * from #L1
>open a_Cursor
>fetch next from a_Cursor
>into @.F, @.S, @.P, @.tSeasonMSHa, @.rSeasonMSHa, @.tSeasonPastureHarvestHa,
>@.rSeasonPastureHarvestHa, @.tSeasonSuppUsedHa, @.rSeasonSuppUsedHa,
>@.tSeasonFeedCostsHa, @.rSeasonFeedCostsHa, @.tSeasonGrossMarginHa,
>@.rSeasonGrossMarginHa
>while @.@.fetch_status = 0
>begin
> update nmlFMS set [tSeasonMS/Ha] = @.tSeasonMSHa, [rSeasonMS/Ha] =
>@.rSeasonMSHa, [tSeasonPastureHarvest/Ha] = @.tSeasonPastureHarvestHa,
>[rSeasonPastureHarvest/Ha] = @.rSeasonPastureHarvestHa, [tSeasonSuppUsed/Ha]
>= @.tSeasonSuppUsedHa, [rSeasonSuppUsed/Ha] = @.rSeasonSuppUsedHa,
>[tSeasonFeedCosts/Ha] = @.tSeasonFeedCostsHa, [rSeasonFeedCosts/Ha] =
>@.rSeasonFeedCostsHa, [tSeasonGrossMargin/Ha] = @.tSeasonGrossMarginHa,
>[rSeasonGrossMargin/Ha] = @.rSeasonGrossMarginHa
> where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
> fetch next from a_Cursor
> into @.F, @.S, @.P, @.tSeasonMSHa, @.rSeasonMSHa, @.tSeasonPastureHarvestHa,
>@.rSeasonPastureHarvestHa, @.tSeasonSuppUsedHa, @.rSeasonSuppUsedHa,
>@.tSeasonFeedCostsHa, @.rSeasonFeedCostsHa, @.tSeasonGrossMarginHa,
>@.rSeasonGrossMarginHa
>end
>close a_Cursor
>deallocate a_Cursor
>----
-
>----
-
>---
>/* [tSeasonMS/Cow], [rSeasonMS/Cow], [tSeasonPastureHarvest/Cow],
>[rSeasonPastureHarvest/Cow], [tSeasonSuppUsed/Cow], [rSeasonSuppUsed/Cow],
>[tSeasonFeedCosts/Cow], [rSeasonFeedCosts/Cow], [tSeasonGrossMargin/Cow],
>[rSeasonGrossMargin/Cow] */
>select drvFMS.FarmId, drvFMS.Season, drvFMS.Period,
>max(tblForecastMilkingCowData.NoOfCows) as PeakCows
>into #M1
>from drvFMS
>inner join tblForecastMilkingCowData on (tblForecastMilkingCowData.FarmId =
>drvFMS.FarmId) and (tblForecastMilkingCowData.Season = drvFMS.Season) and
>(tblForecastMilkingCowData.Period = drvFMS.Period)
>group by drvFMS.FarmId, drvFMS.Season, drvFMS.Period
>order by drvFMS.FarmId, drvFMS.Season, drvFMS.Period
>select #M1.*, tblOriginalFarmData.FarmSize
>into #M2
>from #M1
>inner join tblOriginalFarmData on (tblOriginalFarmData.FarmId = #M1.FarmId)
>and (tblOriginalFarmData.Season = #M1.Season)
>select #M2.FarmId, #M2.Season, #M2.Period, cast(round((cast(nmlFMS.tSeasonM
S
>as decimal(11,3)) / cast(#M2.PeakCows as decimal(11,3))),0) as integer) as
>'tSeasonMS/Cow', cast(round((cast(nmlFMS.rSeasonMS as decimal(11,3)) /
>cast(#M2.PeakCows as decimal(11,3))),0) as integer) as 'rSeasonMS/Cow',
>(nmlFMS.[tSeasonPastureHarvest/Ha] / (#M2.PeakCows / #M2.FarmSize)) as
>'tSeasonPastureHarvest/Cow', (nmlFMS.[rSeasonPastureHarvest/Ha] /
>(#M2.PeakCows / #M2.FarmSize)) as 'rSeasonPastureHarvest/Cow',
>(nmlFMS.tSeasonSuppUsed / #M2.PeakCows) as 'tSeasonSuppUsed/Cow',
>(nmlFMS.rSeasonSuppUsed / #M2.PeakCows) as 'rSeasonSuppUsed/Cow',
>(nmlFMS.tSeasonFeedCosts / #M2.PeakCows) as 'tSeasonFeedCosts/Cow',
>(nmlFMS.rSeasonFeedCosts / #M2.PeakCows) as 'rSeasonFeedCosts/Cow',
>(nmlFMS.tSeasonGrossMargin / #M2.PeakCows) as 'tSeasonGrossMargin/Cow',
>(nmlFMS.rSeasonGrossMargin / #M2.PeakCows) as 'rSeasonGrossMargin/Cow'
>into #M3
>from #M2
>inner join nmlFMS on (nmlFMS.FarmId = #M2.FarmId) and (nmlFMS.Season =
>#M2.Season) and (nmlFMS.Period = #M2.Period)
>declare @.tSeasonMSCow as integer
>declare @.rSeasonMSCow as integer
>declare @.tSeasonPastureHarvestCow as decimal(9,3)
>declare @.rSeasonPastureHarvestCow as decimal(9,3)
>declare @.tSeasonSuppUsedCow as decimal(9,3)
>declare @.rSeasonSuppUsedCow as decimal(9,3)
>declare @.tSeasonFeedCostsCow as decimal(9,3)
>declare @.rSeasonFeedCostsCow as decimal(9,3)
>declare @.tSeasonGrossMarginCow as decimal(9,3)
>declare @.rSeasonGrossMarginCow as decimal(9,3)
>declare a_Cursor cursor for select * from #M3
>open a_Cursor
>fetch next from a_Cursor
>into @.F, @.S, @.P, @.tSeasonMSCow, @.rSeasonMSCow, @.tSeasonPastureHarvestCow,
>@.rSeasonPastureHarvestCow, @.tSeasonSuppUsedCow, @.rSeasonSuppUsedCow,
>@.tSeasonFeedCostsCow, @.rSeasonFeedCostsCow, @.tSeasonGrossMarginCow,
>@.rSeasonGrossMarginCow
>while @.@.fetch_status = 0
>begin
> update nmlFMS set [tSeasonMS/Cow] = @.tSeasonMSCow, [rSeasonMS/Cow] =
>@.rSeasonMSCow, [tSeasonPastureHarvest/Cow] = @.tSeasonPastureHarvestCow,
>[rSeasonPastureHarvest/Cow] = @.rSeasonPastureHarvestCow,
>[tSeasonSuppUsed/Cow] = @.tSeasonSuppUsedCow, [rSeasonSuppUsed/Cow] =
>@.rSeasonSuppUsedCow, [tSeasonFeedCosts/Cow] = @.tSeasonFeedCostsCow,
>[rSeasonFeedCosts/Cow] = @.rSeasonFeedCostsCow, [tSeasonGrossMargin/Cow] =
>@.tSeasonGrossMarginCow, [rSeasonGrossMargin/Cow] = @.rSeasonGrossMarginCow
> where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
> fetch next from a_Cursor
> into @.F, @.S, @.P, @.tSeasonMSCow, @.rSeasonMSCow, @.tSeasonPastureHarvestCow,
>@.rSeasonPastureHarvestCow, @.tSeasonSuppUsedCow, @.rSeasonSuppUsedCow,
>@.tSeasonFeedCostsCow, @.rSeasonFeedCostsCow, @.tSeasonGrossMarginCow,
>@.rSeasonGrossMarginCow
>end
>close a_Cursor
>deallocate a_Cursor
>----
-
>----
-
>---
>/* [tSeasonPastureHarvest/KgMS], [rSeasonPastureHarvest/KgMS],
>[tSeasonSuppUsed/KgMS], [rSeasonSuppUsed/KgMS], [tSeasonFeedCosts/KgMS],
>[rSeasonFeedCosts/KgMS], [tSeasonGrossMargin/KgMS],
>[rSeasonGrossMargin/KgMS] */
>select drvFMS.FarmId, drvFMS.Season, drvFMS.Period,
>((nmlFMS.[tSeasonPastureHarvest/Ha] * 1000) / nmlFMS.[tSeasonMS/Ha]) as
>'tSeasonPastureHarvest/KgMS', ((nmlFMS.[rSeasonPastureHarvest/Ha] * 1000) /
>nmlFMS.[rSeasonMS/Ha]) as 'rSeasonPastureHarvest/KgMS',
>((nmlFMS.tSeasonSuppUsed * 1000 / tblOriginalFarmData.FarmSize) /
>nmlFMS.[tSeasonMS/Ha]) as 'tSeasonSuppUsed/KgMS', ((nmlFMS.rSeasonSuppUsed *
>1000 / tblOriginalFarmData.FarmSize) / nmlFMS.[rSeasonMS/Ha]) as
>'rSeasonSuppUsed/KgMS', (nmlFMS.tSeasonFeedCosts / nmlFMS.tSeasonMS) as
>'tSeasonFeedCosts/KgMS', (nmlFMS.rSeasonFeedCosts / nmlFMS.rSeasonMS) as
>'rSeasonFeedCosts/KgMS', (nmlFMS.tSeasonGrossMargin / nmlFMS.tSeasonMS) as
>'tSeasonGrossMargin/KgMS', (nmlFMS.rSeasonGrossMargin / nmlFMS.rSeasonMS) a
s
>'rSeasonGrossMargin/KgMS'
>into #N1
>from drvFMS
>inner join nmlFMS on (nmlFMS.FarmId = drvFMS.FarmId) and (nmlFMS.Season =
>drvFMS.Season) and (nmlFMS.Period = drvFMS.Period)
>inner join tblOriginalFarmData on (tblOriginalFarmData.FarmId =
>drvFMS.FarmId) and (tblOriginalFarmData.Season = drvFMS.Season)
>declare @.tSeasonPastureHarvestKgMS as decimal(9,3)
>declare @.rSeasonPastureHarvestKgMS as decimal(9,3)
>declare @.tSeasonSuppUsedKgMS as decimal(9,3)
>declare @.rSeasonSuppUsedKgMS as decimal(9,3)
>declare @.tSeasonFeedCostsKgMS as decimal(9,3)
>declare @.rSeasonFeedCostsKgMS as decimal(9,3)
>declare @.tSeasonGrossMarginKgMS as decimal(9,3)
>declare @.rSeasonGrossMarginKgMS as decimal(9,3)
>declare a_Cursor cursor for select * from #N1
>open a_Cursor
>fetch next from a_Cursor
>into @.F, @.S, @.P, @.tSeasonPastureHarvestKgMS, @.rSeasonPastureHarvestKgMS,
>@.tSeasonSuppUsedKgMS, @.rSeasonSuppUsedKgMS, @.tSeasonFeedCostsKgMS,
>@.rSeasonFeedCostsKgMS, @.tSeasonGrossMarginKgMS, @.rSeasonGrossMarginKgMS
>while @.@.fetch_status = 0
>begin
> update nmlFMS set [tSeasonPastureHarvest/KgMS] =
>@.tSeasonPastureHarvestKgMS, [rSeasonPastureHarvest/KgMS] =
>@.rSeasonPastureHarvestKgMS, [tSeasonSuppUsed/KgMS] = @.tSeasonSuppUsedKgMS,
>[rSeasonSuppUsed/KgMS] = @.rSeasonSuppUsedKgMS, [tSeasonFeedCosts/KgMS] =
>@.tSeasonFeedCostsKgMS, [rSeasonFeedCosts/KgMS] = @.rSeasonFeedCostsKgMS,
>[tSeasonGrossMargin/KgMS] = @.tSeasonGrossMarginKgMS,
>[rSeasonGrossMargin/KgMS] = @.rSeasonGrossMarginKgMS
> where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
> fetch next from a_Cursor
> into @.F, @.S, @.P, @.tSeasonPastureHarvestKgMS, @.rSeasonPastureHarvestKgMS,
>@.tSeasonSuppUsedKgMS, @.rSeasonSuppUsedKgMS, @.tSeasonFeedCostsKgMS,
>@.rSeasonFeedCostsKgMS, @.tSeasonGrossMarginKgMS, @.rSeasonGrossMarginKgMS
>end
>close a_Cursor
>deallocate a_Cursor
>----
-
>----
-
>---
>/* nmMthEndAvgCover, nmPastureGrowth */
>select drvFMS.FarmId, drvFMS.Season, drvFMS.Period,
>tblForecastPastureData.Cover as nmMthEndAvgCover
>into #O1
>from drvFMS
>inner join tblForecastPastureData on (tblForecastPastureData.FarmId =
>drvFMS.FarmId) and (tblForecastPastureData.Season = drvFMS.Season) and
>(tblForecastPastureData.Period = drvFMS.Period) and
>(month(tblForecastPastureData.[Date]) = @.NextPeriod_Month) and
>(day(tblForecastPastureData.[Date]) = 21)
>select #O1.*, tblForecastPastureData.Growth
>into #O2
>from #O1
>inner join tblForecastPastureData on (tblForecastPastureData.FarmId =
>#O1.FarmId) and (tblForecastPastureData.Season = #O1.Season) and
>(tblForecastPastureData.Period = #O1.Period) and
>(month(tblForecastPastureData.[Date]) = @.NextPeriod_Month)
>select #O2.FarmId, #O2.Season, #O2.Period, #O2.nmMthEndAvgCover,
>avg(cast(#O2.Growth as decimal(9,3))) as nmPastureGrowth
>into #O3
>from #O2
>group by #O2.FarmId, #O2.Season, #O2.Period, #O2.nmMthEndAvgCover
>declare @.nmMthEndAvgCover as smallint
>declare @.nmPastureGrowth as decimal(9,3)
>declare a_Cursor cursor for select * from #O3
>open a_Cursor
>fetch next from a_Cursor
>into @.F, @.S, @.P, @.nmMthEndAvgCover, @.nmPastureGrowth
>while @.@.fetch_status = 0
>begin
> update nmlFMS set nmMthEndAvgCover = @.nmMthEndAvgCover, nmPastureGrowth =
>@.nmPastureGrowth
> where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
> fetch next from a_Cursor
> into @.F, @.S, @.P, @.nmMthEndAvgCover, @.nmPastureGrowth
>end
>close a_Cursor
>deallocate a_Cursor
>----
-
>----
-
>---
>/* nmAvgCowsMilked, nmDmc1, nmDmc2, nmDmc3, nmDmc4, nmDmc5, nmDmc6, nmDmc7,
>nmCrops, nmTotalDMConsumed */
>select drvFMS.FarmId, drvFMS.Season, drvFMS.Period,
>avg(cast(tblForecastMilkingCowData.NoOfCows as decimal(9,3))) as
>nmAvgCowsMilked, avg(cast(tblForecastMilkingCowData.dmc1 as decimal(9,3)))
>as nmDmc1avg, avg(cast(tblForecastMilkingCowData.dmc2 as decimal(9,3))) as
>nmDmc2avg, avg(cast(tblForecastMilkingCowData.dmc3 as decimal(9,3))) as
>nmDmc3avg, avg(cast(tblForecastMilkingCowData.dmc4 as decimal(9,3))) as
>nmDmc4avg, avg(cast(tblForecastMilkingCowData.dmc5 as decimal(9,3))) as
>nmDmc5avg, avg(cast(tblForecastMilkingCowData.dmc6 as decimal(9,3))) as
>nmDmc6avg, avg(cast(tblForecastMilkingCowData.dmc7 as decimal(9,3))) as
>nmDmc7avg, avg(cast(tblForecastMilkingCowData.Crop1 as decimal(9,3))) as
>nmCrop1avg, avg(cast(tblForecastMilkingCowData.Crop2 as decimal(9,3))) as
>nmCrop2avg, avg(cast(tblForecastMilkingCowData.Crop3 as decimal(9,3))) as
>nmCrop3avg, avg(cast(tblForecastMilkingCowData.Crop4 as decimal(9,3))) as
>nmCrop4avg, avg(cast(tblForecastMilkingCowData.Crop5 as decimal(9,3))) as
>nmCrop5avg
>into #P1
>from tblForecastMilkingCowData
>inner join drvFMS on (tblForecastMilkingCowData.FarmId = drvFMS.FarmId) and
>(tblForecastMilkingCowData.Season = drvFMS.Season) and
>(tblForecastMilkingCowData.Period = @.ThisPeriod) and
>(month(tblForecastMilkingCowData.[Date]) = @.NextPeriod_Month)
>group by drvFMS.FarmId, drvFMS.Season, drvFMS.Period
>select #P1.*, (nmCrop1avg + nmCrop2avg + nmCrop3avg + nmCrop4avg +
>nmCrop5avg) as nmCrops, (round(nmDmc1avg,1) + round(nmDmc2avg,1) +
>round(nmDmc3avg,1) + round(nmDmc4avg,1) + round(nmDmc5avg,1) +
>round(nmDmc6avg,1) + round(nmDmc7avg,1) + round(nmCrop1avg,1) +
>round(nmCrop2avg,1) + round(nmCrop3avg,1) + round(nmCrop4avg,1) +
>round(nmCrop5avg,1)) as nmTotalDMConsumed
>into #P2
>from #P1
>declare @.nmAvgCowsMilked as smallint
>declare @.nmDmc1 as decimal(9,3)
>declare @.nmDmc2 as decimal(9,3)
>declare @.nmDmc3 as decimal(9,3)
>declare @.nmDmc4 as decimal(9,3)
>declare @.nmDmc5 as decimal(9,3)
>declare @.nmDmc6 as decimal(9,3)
>declare @.nmDmc7 as decimal(9,3)
>declare @.nmCrops as decimal(9,3)
>declare @.nmTotalDMConsumed as decimal(9,3)
>declare a_Cursor cursor for
>select FarmId, Season, Period, nmAvgCowsMilked, nmDmc1avg, nmDmc2avg,
>nmDmc3avg, nmDmc4avg, nmDmc5avg, nmDmc6avg, nmDmc7avg, nmCrops,
>nmTotalDMConsumed from #P2
>open a_Cursor
>fetch next from a_Cursor
>into @.F, @.S, @.P, @.nmAvgCowsMilked, @.nmDmc1, @.nmDmc2, @.nmDmc3, @.nmDmc4,
>@.nmDmc5, @.nmDmc6, @.nmDmc7, @.nmCrops, @.nmTotalDMConsumed
>while @.@.fetch_status = 0
>begin
> update nmlFMS set nmAvgCowsMilked = @.nmAvgCowsMilked, nmDmc1 = @.nmDmc1,
>nmDmc2 = @.nmDmc2, nmDmc3 = @.nmDmc3, nmDmc4 = @.nmDmc4, nmDmc5 = @.nmDmc5,
>nmDmc6 = @.nmDmc6, nmDmc7 = @.nmDmc7, nmCrops = @.nmCrops, nmTotalDMConsumed =
>@.nmTotalDMConsumed
> where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
> fetch next from a_Cursor
> into @.F, @.S, @.P, @.nmAvgCowsMilked, @.nmDmc1, @.nmDmc2, @.nmDmc3, @.nmDmc4,
>@.nmDmc5, @.nmDmc6, @.nmDmc7, @.nmCrops, @.nmTotalDMConsumed
>end
>close a_Cursor
>deallocate a_Cursor
>----
-
>----
-
>---
>/* nmSeasonToDateMS, nmMonthTotalMS, [nmMS/Cow/Day] */
>select drvFMS.FarmId, drvFMS.Season, drvFMS.Period,
>month(tblForecastProductionData.[Date]) as [Month],
>(tblForecastProductionData.Fat + tblForecastProductionData.Protein) as
>MilkSolids, tblForecastProductionData.Days
>into #Q1
>from drvFMS
>inner join tblForecastProductionData on (tblForecastProductionData.FarmId =
>drvFMS.FarmId) and (tblForecastProductionData.Season = drvFMS.Season) and
>(tblForecastProductionData.Period = @.ThisPeriod)
>select #Q1.FarmId, #Q1.Season, #Q1.Period, sum(#Q1.MilkSolids) as
>nmMonthTotalMS, sum(#Q1.Days) as [Days]
>into #Q2
>from #Q1
>where (#Q1.[Month] = @.NextPeriod_Month)
>group by FarmId, Season, Period
>create index Q2_ndx on #Q2 (FarmId, Season, Period)
>create index P2_ndx on #P2 (FarmId, Season, Period)
>select #Q1.*, dbo.fnMonthToPeriod([Month]) as MonthPeriod,
>#Q2.nmMonthTotalMS, cast((#Q2.nmMonthTotalMS / #P2.nmAvgCowsMilked /
>#Q2.Days) as decimal(9,3)) as [nmMS/Cow/Day]
>into #Q3
>from #Q1
>inner join #Q2 on (#Q2.FarmId = #Q1.FarmId) and (#Q2.Season = #Q1.Season)
>and (#Q2.Period = #Q1.Period)
>inner join #P2 on (#P2.FarmId = #Q1.FarmId) and (#P2.Season = #Q1.Season)
>and (#P2.Period = #Q1.Period)
>select #Q3.FarmId, #Q3.Season, #Q3.Period, #Q3.nmMonthTotalMS,
>[nmMS/Cow/Day], sum(#Q3.MilkSolids) as nmSeasonToDateMS
>into #Q4
>from #Q3
>where #Q3.MonthPeriod <= @.NextPeriod
>group by FarmId, Season, Period, nmMonthTotalMS, [nmMS/Cow/Day]
>declare @.nmMonthTotalMS as decimal(9,3)
>declare @.nmMSCowDay as decimal(9,3)
>declare @.nmSeasonToDateMS as decimal(9,3)
>declare a_Cursor cursor for select * from #Q4
>open a_Cursor
>fetch next from a_Cursor
>into @.F, @.S, @.P, @.nmMonthTotalMS, @.nmMSCowDay, @.nmSeasonToDateMS
>while @.@.fetch_status = 0
>begin
> update nmlFMS set nmMonthTotalMS = @.nmMonthTotalMS, [nmMS/Cow/Day] =
>@.nmMSCowDay, nmSeasonToDateMS = @.nmSeasonToDateMS
> where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
> fetch next from a_Cursor
> into @.F, @.S, @.P, @.nmMonthTotalMS, @.nmMSCowDay, @.nmSeasonToDateMS
>end
>close a_Cursor
>deallocate a_Cursor
>----
-
>----
-
>---
>/* nmFeedConvEff, [nmFeedAlloc/LiveWeight], [nmMthMS/Ha] */
>select #P2.FarmId, #P2.Season, #P2.Period, #P2.nmTotalDMConsumed,
>sum(tblForecastProductionData.Days) as [Days]
>into #R1
>from #P2
>inner join tblForecastProductionData on (tblForecastProductionData.FarmId =
>#P2.FarmId) and (tblForecastProductionData.Season = #P2.Season) and
>(tblForecastProductionData.Period = @.ThisPeriod) and
>(month(tblForecastProductionData.[Date]) = @.NextPeriod_Month)
>group by #P2.FarmId, #P2.Season, #P2.Period, #P2.nmTotalDMConsumed
>select #R1.FarmId, #R1.Season, #R1.Period, #R1.nmTotalDMConsumed, #R1.Days,
>avg(cast(tblForecastPastureData.FarmSize as decimal(9,3))) as nmAverageArea
>into #R2
>from #R1
>inner join tblForecastPastureData on (tblForecastPastureData.FarmId =
>#R1.FarmId) and (tblForecastPastureData.Season = #R1.Season) and
>(tblForecastPastureData.Period = @.ThisPeriod) and
>(month(tblForecastPastureData.[Date]) = @.NextPeriod_Month)
>group by #R1.FarmId, #R1.Season, #R1.Period, #R1.nmTotalDMConsumed,
>#R1.[Days]
>select #R2.*, #P2.nmAvgCowsMilked, #Q4.nmMonthTotalMS,
>tblForecastFarmData.FarmSize as 'nmAvailableArea',
>tblFarmAdditionalInfo.Liveweight
>into #R3
>from #R2
>inner join #P2 on (#P2.FarmId = #R2.FarmId) and (#P2.Season = #R2.Season)
>and (#P2.Period = #R2.Period)
>inner join #Q4 on (#Q4.FarmId = #R2.FarmId) and (#Q4.Season = #R2.Season)
>and (#Q4.Period = #R2.Period)
>inner join tblForecastFarmData on (tblForecastFarmData.FarmId = #R2.FarmId)
>and (tblForecastFarmData.Season = #R2.Season) and
>(tblForecastFarmData.Period = #R2.Period)
>inner join tblFarmAdditionalInfo on (tblFarmAdditionalInfo.FarmId =
>#R2.FarmId)
>select #R3.FarmId, #R3.Season, #R3.Period, ((#R3.nmTotalDMConsumed *
>#R3.Days * (#R3.nmAvgCowsMilked / #R3.nmAverageArea)) / (#R3.nmMonthTotalMS
>/ #R3.nmAvailableArea)) as nmFeedConvEff, (#R3.nmTotalDMConsumed /
>#R3.Liveweight * 100) as [nmFeedAlloc/LiveWeight], (#R3.nmMonthTotalMS /
>#R3.nmAvailableArea) as [nmMthMS/Ha]
>into #R4
>from #R3
>declare @.nmFeedConvEff as decimal(9,3)
>declare @.nmFeedAllocLiveWeight as decimal(9,3)
>declare @.nmMthMSHa as decimal(9,3)
>declare a_Cursor cursor for select * from #R4
>open a_Cursor
>fetch next from a_Cursor
>into @.F, @.S, @.P, @.nmFeedConvEff, @.nmFeedAllocLiveWeight, @.nmMthMSHa
>while @.@.fetch_status = 0
>begin
> update nmlFMS set nmFeedConvEff = cast(@.nmFeedConvEff as decimal(6,4)),
>[nmFeedAlloc/LiveWeight] = cast(@.nmFeedAllocLiveWeight as decimal(6,4)),
>[nmMthMS/Ha] = @.nmMthMSHa
> where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
> fetch next from a_Cursor
> into @.F, @.S, @.P, @.nmFeedConvEff, @.nmFeedAllocLiveWeight, @.nmMthMSHa
>end
>close a_Cursor
>deallocate a_Cursor
>----
-
>----
-
>---
>/* [nmMthFeedCosts/KgMS] */
>select #R1.FarmId, #R1.Season, #R1.Period, #R1.Days,
>cast(avg(tblForecastMilkingCowData.NoOfCows) as smallint) as
>nmAvgCowsMilked, avg(tblForecastMilkingCowData.dmc2 *
>tblFarmAdditionalInfo.dmc2$) as nmDmc2$, avg(tblForecastMilkingCowData.dmc3
>* tblFarmAdditionalInfo.dmc3$) as nmDmc3$,
>avg(tblForecastMilkingCowData.dmc4 * tblFarmAdditionalInfo.dmc4$) as
>nmDmc4$, avg(tblForecastMilkingCowData.dmc5 * tblFarmAdditionalInfo.dmc5$)
>as nmDmc5$, avg(tblForecastMilkingCowData.dmc6 *
>tblFarmAdditionalInfo.dmc6$) as nmDmc6$, avg(tblForecastMilkingCowData.dmc7
>* tblFarmAdditionalInfo.dmc7$) as nmDmc7$,
>avg(tblForecastMilkingCowData.Crop1 * tblFarmAdditionalInfo.Crop1$) as
>nmCrop1$, avg(tblForecastMilkingCowData.Crop2 *
>tblFarmAdditionalInfo.Crop2$) as nmCrop2$,
>avg(tblForecastMilkingCowData.Crop3 * tblFarmAdditionalInfo.Crop3$) as
>nmCrop3$, avg(tblForecastMilkingCowData.Crop4 *
>tblFarmAdditionalInfo.Crop4$) as nmCrop4$,
>avg(tblForecastMilkingCowData.Crop5 * tblFarmAdditionalInfo.Crop5$) as
>nmCrop5$
>into #S1
>from #R1
>inner join tblForecastMilkingCowData on (tblForecastMilkingCowData.FarmId =
>#R1.FarmId) and (tblForecastMilkingCowData.Season = #R1.Season) and
>(tblForecastMilkingCowData.Period = @.ThisPeriod) and
>(month(tblForecastMilkingCowData.[Date]) = @.NextPeriod_Month)
>inner join tblFarmAdditionalInfo on (tblFarmAdditionalInfo.FarmId =
>#R1.FarmId)
>group by #R1.FarmId, #R1.Season, #R1.Period, #R1.Days
>select #S1.FarmId, #S1.Season, #S1.Period, #S1.Days, #S1.nmAvgCowsMilked,
>#Q2.nmMonthTotalMS, (#S1.nmDmc2$ + #S1.nmDmc3$ + #S1.nmDmc4$ + #S1.nmDmc5$
+
>#S1.nmDmc6$ + #S1.nmDmc7$ + #S1.nmCrop1$ + #S1.nmCrop2$ + #S1.nmCrop3$ +
>#S1.nmCrop4$ + #S1.nmCrop5$) as nmMthFeedCosts,
>tblForecastFarmData.Adjistment$ as nmGrazingCosts$, (tblForecastFarmData.Fa
t
>+ tblForecastFarmData.Protein) as nmSeasonMS, (tblForecastFarmData.IOFC$ -
>tblForecastFarmData.CowCosts$ - tblForecastFarmData.GrossMargin$) as
>nmIrrigationCosts$, nmlFMS.rSeasonSuppUsed, nmlFMS.rSeasonFeedCosts
>into #S2
>from #S1
>inner join #Q2 on (#Q2.FarmId = #S1.FarmId) and (#Q2.Season = #S1.Season)
>and (#Q2.Period = #S1.Period)
>inner join tblForecastFarmData on (tblForecastFarmData.FarmId = #S1.FarmId)
>and (tblForecastFarmData.Season = #S1.Season) and
>(tblForecastFarmData.Period = @.ThisPeriod)
>inner join nmlFMS on (nmlFMS.FarmId = #S1.FarmId) and (nmlFMS.Season =
>#S1.Season) and (nmlFMS.Period = #S1.Period)
>select #S2.*, (((#S2.nmMthFeedCosts * #S2.nmAvgCowsMilked * #S2.Days) /
>#S2.nmMonthTotalMS) + (#S2.nmGrazingCosts$ / #S2.nmSeasonMS) +
>(#S2.nmIrrigationCosts$ / #S2.nmSeasonMS)) as nmMthFeedCostsKgMS
>into #S3
>from #S2
>declare @.nmMthFeedCostsKgMS as decimal(9,4)
>declare a_Cursor cursor for select FarmId, Season, Period,
>nmMthFeedCostsKgMS from #S3
>open a_Cursor
>fetch next from a_Cursor
>into @.F, @.S, @.P, @.nmMthFeedCostsKgMS
>while @.@.fetch_status = 0
>begin
> update nmlFMS set [nmMthFeedCosts/KgMS] = @.nmMthFeedCostsKgMS
> where (FarmId = @.F) and (Season = @.S) and (Period = @.P)
> fetch next from a_Cursor
> into @.F, @.S, @.P, @.nmMthFeedCostsKgMS
>end
>close a_Cursor
>deallocate a_Cursor
>----
-
>----
-
>---
>EndOf:
>Return
>GO
>
>
>[/color]|||"SBeetham" <sbeetham@.xtra.co.nz> wrote a message
news:O9XQLNyHFHA.3760@.TK2MSFTNGP12.phx.gbl...
Start by writing some tests that 1) show you understand the requirements of
the procedure, and 2) make sure those requirements are still met after you
rewrite it.|||Thanks for your input Steve.
I've taken your comments on board... I'm just going to break out portions,
remove the cursors and most of the temp tables and see how I go from there..
.
appreciate all of your inputs...
Cheers, Simon