I have a query in Access that I need to convert to a stored procedure in SQL Server. This has a lot of Access-specific SQL so any help making it work with SQL Server is appreciated!
FYI- the query is doing the following:
-The first select determines clientkeys where the expenses have been posted, but they have not been assigned to a statement.
-The second select determines clientkeys where expense payments have been posted, but not assigned to a statement.
-The third select is subtracting the sum of expense payments from the sum of expenses, for each client, to determine if there is a balance.
-I then cycle through this recordset (usually no more than a couple of hundred records) and insert a new statement into tExpenseStatements with the clientkey and balance and then print the statement to send to the client.
FYI- the query is doing the following:
-The first select determines clientkeys where the expenses have been posted, but they have not been assigned to a statement.
-The second select determines clientkeys where expense payments have been posted, but not assigned to a statement.
-The third select is subtracting the sum of expense payments from the sum of expenses, for each client, to determine if there is a balance.
-I then cycle through this recordset (usually no more than a couple of hundred records) and insert a new statement into tExpenseStatements with the clientkey and balance and then print the statement to send to the client.
Code:
SELECT DISTINCT tExpenses.ClientKey FROM tExpenses
WHERE (((tExpenses.ClientKey) Is Not Null)
AND ((nz([tExpenses].[StatementNumber],0))=0) AND ((tExpenses.PostDate) Is Not Null))
ORDER BY tExpenses.ClientKey
UNION
SELECT DISTINCT tExpensePayments.ClientKey FROM tExpensePayments
WHERE (((tExpensePayments.ClientKey) Is Not Null)
AND ((nz([tExpensePayments].[StatementNumber],0))=0)
AND ((tExpensePayments.PostDate) Is Not Null))
ORDER BY tExpensePayments.ClientKey
UNION
SELECT DISTINCT tClients.ClientNumber As ClientKey FROM tClients
WHERE (((nz((SELECT SUM([Amount]) FROM tExpenses WHERE ClientKey=[ClientNumber]),0)
-nz((SELECT SUM([PaymentAmount]) FROM tExpensePayments WHERE ClientKey=[ClientNumber]),0))<>0));