473,327 Members | 2,094 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 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 4319
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......
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.