Solved

How to join an additional table to an existing stored procedure using SQL Server 2008 for an Access 2003 app?

Posted on 2014-12-31
3
295 Views
Last Modified: 2014-12-31
I am developing an Access 2003 application using Access 2003 as the front end and SQL Server 2008 as the back end database.

I have a stored procedure as follows:

If  Exists(SELECT * FROM dbo.SYSOBJECTS WHERE NAME = 'tblFlINTEXCL' AND TYPE = 'U')
DELETE FROM dbo.tblFlINTEXCL
INSERT INTO dbo.tblFlINTEXCL([Account Number], RedFlag, [Property Type], TrillFixField,EmployeeID,SwingInd,MasterCardNumber, ActiveRecord)
SELECT Right(C.OfficeNumber,3) + ' ' + C.CustomerNumber AS [Account Number],
C.RedFlag, UPPER(P.PropertyType) As [Property Type], C.TrillFixField,C.EmployeeID,C.SwingInd,C.MasterCardNumber, 1
FROM dbo.tblStatesAll  As S INNER JOIN (dbo.tblCustomersNew As C INNER JOIN dbo.tblProductsNew As P ON C.CustomerNumber = P.CustomerNumber AND C.OfficeNumber = P.OfficeNumber) ON S.StateFS = C.ResStateCode
WHERE (S.FallCycle= 1) AND
               (P.PropertyType='FIXED INCOME' AND C.DateLost <= CAST((@RptYear - S.IRAFS) AS VARCHAR) + '-06-30' AND LEN(P.IRACode) > 0 AND (P.Quantity > 0 OR P.CashBalance > 0) AND
               (C.DateOfBirth <= CAST(((@RptYear - S.IRAFS)-71) AS VARCHAR) + '-12-31')))
ORDER BY C.ResStateCode, [Account Number],P.PropertyType

How would you join table "tblIntlAccountFallExcl" to this stored procedure to exclude its "Account" values?
This table only contains the field "Account".

I have used the following SQL Statement separately but I want to incorporate it into my existing stored procedure:

SELECT * FROM dbo.tblFlINTEXCL AS FlINT WHERE NOT EXISTS
(SELECT NULL FROM dbo.tblIntlAccountFallExcl as EXCL
where FlINT.[Account Number] = EXCL.Account AND ActiveRecord = '1');
0
Comment
Question by:zimmer9
3 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40525543
It sounds like you

1 put your first bit of code up to the end of the second-to-last line,

2. Add an AND

3. Put your second code from NOT EXISTS up to, but not including, the semicolon, but change
FINT.[Account Number]
To
Right(C.OfficeNumber,3) + ' ' + C.CustomerNumber

4. Continue with your first bit of code.
0
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 40525553
So you don't want to insert records into tblFlINTEXCL if the C.CustomerNumber exists in tblIntlAccountFallExcl.Account?

If so, you might use a LEFT JOIN and exclude all records that return a non-null value in the [Account] field. This would be the select, I believe:

SELECT Right(C.OfficeNumber,3) + ' ' + C.CustomerNumber AS [Account Number],C.RedFlag, UPPER(P.PropertyType) As [Property Type], C.TrillFixField,C.EmployeeID,C.SwingInd,C.MasterCardNumber, 1
FROM dbo.tblStatesAll  As S INNER JOIN
(dbo.tblCustomersNew As C INNER JOIN
dbo.tblProductsNew As P ON C.CustomerNumber = P.CustomerNumber AND C.OfficeNumber = P.OfficeNumber) ON S.StateFS = C.ResStateCode
LEFT OUTER JOIN tblFLINTEXCL FL ON FL.[Account Number]=C.CustomerNumber
WHERE (S.FallCycle= 1) AND
               (P.PropertyType='FIXED INCOME' AND C.DateLost <= CAST((@RptYear - S.IRAFS) AS VARCHAR) + '-06-30' AND LEN(P.IRACode) > 0 AND (P.Quantity > 0 OR P.CashBalance > 0)
                     AND
               (C.DateOfBirth <= CAST(((@RptYear - S.IRAFS)-71) AS VARCHAR) + '-12-31')))
                    AND FL.Account IS NULL

Essentially, if the value in C.CustomerNumber matches a value foundd in tblFLINTEXCL.Account, you ignore that line.
0
 
LVL 26

Expert Comment

by:Nick67
ID: 40525570
Well,
(and I am sure someone else will chime in)
You have your FROM clause
FROM dbo.tblStatesAll  As S INNER JOIN (dbo.tblCustomersNew As C INNER JOIN dbo.tblProductsNew As P ON C.CustomerNumber = P.CustomerNumber AND C.OfficeNumber = P.OfficeNumber) ON S.StateFS = C.ResStateCode

So you are going to need to alter that to get dbo.tblIntlAccountFallExcl in there with the appropriate relationship
I can't really see how that's going to happen, intuitively -- but then it's not my data.  You many look at it and see exactly how.

You have your WHERE clause
WHERE (S.FallCycle= 1) AND
                (P.PropertyType='FIXED INCOME' AND C.DateLost <= CAST((@RptYear - S.IRAFS) AS VARCHAR) + '-06-30' AND LEN(P.IRACode) > 0 AND (P.Quantity > 0 OR P.CashBalance > 0) AND
                (C.DateOfBirth <= CAST(((@RptYear - S.IRAFS)-71) AS VARCHAR) + '-12-31')))


You now want to flange in an additional condition.  Above you flanged in dbo.tblIntlAccountFallExcl.
Now you are looking to exclude records where FlINT.[Account Number]  = dbo.tblIntlAccountFallExcl.Account and  ActiveRecord = '1', right?

S0 probably appending this to your WHERE statement
AND (FlINT.[Account Number]  <> dbo.tblIntlAccountFallExcl.Account and  ActiveRecord <> '1')
Might be the right logic.
You got a WHERE NOT EXISTS and a SELECT NULL.  Those make for a bit of mind-bending logic.

But that's the idea at any rate.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question