472,961 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,961 software developers and data experts.

MSG 4145 Error

I am trying to write a stored procedure which will match the records from 2 tables and update a column in each saying the records match by using a counter. Both tables are exactly the same. For instance, if a record in table 1 matches a record in table by using a column as a key, then update the Match column in each table to say "1" then incremement the counter. What makes this difficult is that the records have to be matched by the closest posting date so when records are compared there can be a 1 to many table or many to 1. I believe I got all the criteria squared away. Need help with the following.
1. Performing this for all the records in a table.
2. Assign the select statement into a variable to perform an if statement.

Code is below and any help is greatly appreciated!

create procedure TEST
AS

BEGIN

declare @recordcount as integer
declare @runtime as integer
declare @counter as integer
declare @tempdate as datetime
set @recordcount = 50083
set @runtime = 50083
set @counter = 0
set @tempdate = CURRENT_TIMESTAMP


--while @runtime >= 1
-- INPUT SELECT STATEMENT TO GO THROUGH ALL RECORDS


--begin


If (select *

from reversals,invoices

where reversals.RefDoc_DocNum_BC = invoices.RefDoc_DocNum_BC and
reversals.Billing_Document like '098%' and
invoices.Billing_Document like '095%' and dbo.Reversals.Posting_Date_in_the_Document = dbo.Invoices.Posting_Date_in_the_Document)

/* FIND A WAY TO ASSIGN A VARIABLE FROM A SELECT STATEMENT */



--If (dbo.Reversals.Posting_Date_in_the_Document = dbo.Invoices.Posting_Date_in_the_Document)
begin
update reversals set reversals.Match_BC = @counter
update invoices set invoices.Match_BC = @counter
end

--else
--begin
--For 1 to @recordcount
else
begin
set @tempdate = invoices.Posting_Date_in_the_Document

While @recordcount >= 1
begin
if reversals.Posting_Date_in_the_Document <> @tempdate
begin
set @tempdate = DateAdd(dd,-1,@tempdate)
set @recordcount = @recordcount - 1
end
else
begin
update reversals set reversals.Match_BC = @counter
update invoices set invoices.Match_BC = @counter
end
end
end

--@recordcount = @recordcount - 1
--end



end
Nov 26 '08 #1
2 4295
ck9663
2,878 Expert 2GB
I did not look at your code...It's quite long. Next time, please use the code tag.

To answer your question, you can either:

1. Issue two updates command for the two tables.

or

2.Create an updateable view and use a trigger to cascade the update.

Good luck!

-- CK
Nov 27 '08 #2
Hi,
Sorry about that, I'm a newbie. If you forget about my code, how would you write the SQL to do the following:

If you have 1 table with all the data. You want to match rows based on some keys to get a grouping of them and then within the group to identify the 2 rows that should be matched. The 2 items are invoices and reversals. The invoice data should always match to the closest reversal date that is at least equal or after the date. Example below.

ID
123 invoice 1/20/2006 1
123 reversal 1/20/2006 1
123 invoice 1/23/2006 2
123 reversal 2/20/2006 2
123 reversal 1/22/2006

The first record would match with 2nd record so both are labeled with a '1'. 3rd and fourth are matched so are labeled with a '2'. Last record is not matched to anything because the reversal data is before the invoice date. An invoice does not need to have a reversal but if it does it can only have 1. Same thing for a versal.

THANK YOU
Nov 27 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
5
by: Tony Wright | last post by:
Hi, I am having a problem installing an msi for a web site. The error message I am getting is: "The specified path 'http://mipdev05/features/Fas2' is unavailable. The Internet Information...
13
by: deko | last post by:
I use this convention frequently: Exit_Here: Exit Sub HandleErr: Select Case Err.Number Case 3163 Resume Next Case 3376 Resume Next
7
by: p | last post by:
WE had a Crystal 8 WebApp using vs 2002 which we upgraded to VS2003. I also have Crystal 9 pro on my development machine. The web app runs fine on my dev machine but am having problems deploying....
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
2
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
5
by: yashu0209 | last post by:
I have an error occurring when i try to compile the program and the error is: D:\Program Files\visual studio 6.0\MSDev98\MyProjects\GLEnabledView\GLNew.cpp(164) : error C2653: 'CGLEnabledView' :...
2
by: randa zaghdan | last post by:
Hi I have a problem with my program in vc 2008 When I compile it, the following errors are listed. I try to resolve it but no hope Does any one can help me ? thanks . Linking......
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.