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
292 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now