This is a stored procedure that we just completed for a client, it inserts a default set of Analytical Accounting codes into a SOP invoice so that the they only have to change the items that are not default.
There is an amout of business logic here, so the script 'as is' will not work for any other client... but it's a good start if you're trying to understand Analytical Accounting.