SQL Union Query with dual parameters

CEnd99

New Member
Joined
Sep 2, 2015
Messages
4
Hi,
I am trying to create a SQL Query accessing a SQL database combining two tables together. I was able to successfully create the Union Query however, now I want to add a parameter that is the same in both queries. I can get it in the first part but in the second half, I get an error message "The multi-part identifier "MRI_Journal.Period" could not be bound. The query I have used is below:
SELECT
MRI_GHIS.ENTITYID,
MRI_GHIS.ACCTNUM,
MRI_GHIS.PERIOD,
MRI_GHIS.REF,
MRI_GHIS.SOURCE,
MRI_GHIS.AMT,
MRI_GHIS.DESCRPN
FROM MRIData.dbo.MRI_GHIS MRI_GHIS
WHERE (MRI_GHIS.ENTITYID='C01400') AND (MRI_GHIS.ACCTNUM Like '%CP1810344%') AND (MRI_GHIS.PERIOD>=?) and (MRI_GHIS.period <=?)
UNION
SELECT
MRI_JOURNAL.ENTITYID,
MRI_JOURNAL.ACCTNUM,
MRI_JOURNAL.PERIOD,
MRI_JOURNAL.REF,
MRI_JOURNAL.SOURCE,
MRI_JOURNAL.AMT,
MRI_JOURNAL.DESCRPN
FROM MRIData.dbo.MRI_JOURNAL MRI_JOURNAL
WHERE (MRI_JOURNAL.ENTITYID='C01400') AND (MRI_JOURNAL.ACCTNUM Like '%CP1810344%') and (MRI_JOURNAL.PERIOD >=?)
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Is this in MSAccess?
You seem to have two parameters, not one.

Note: would also recommend you get it working with the parameter in the first query only, then proceed to see if it works in a union query.
 
Last edited:
Upvote 0
Sorry, I did not realize the second parameter was still there in the first half of the query. I can get it to work with either one or two in the first half but the minute I put a parameter after the Union statement, I get the unbound error. I want the first parameter to be the same as the parameter in the second half of the query.
SELECT
MRI_GHIS.ENTITYID,
MRI_GHIS.ACCTNUM,
MRI_GHIS.PERIOD,
MRI_GHIS.REF,
MRI_GHIS.SOURCE,
MRI_GHIS.AMT,
MRI_GHIS.DESCRPN
FROM MRIData.dbo.MRI_GHIS MRI_GHIS
WHERE (MRI_GHIS.ENTITYID='C01400') AND (MRI_GHIS.ACCTNUM Like '%CP1810344%') AND (MRI_GHIS.PERIOD>=?)
UNION
SELECT
MRI_JOURNAL.ENTITYID,
MRI_JOURNAL.ACCTNUM,
MRI_JOURNAL.PERIOD,
MRI_JOURNAL.REF,
MRI_JOURNAL.SOURCE,
MRI_JOURNAL.AMT,
MRI_JOURNAL.DESCRPN
FROM MRIData.dbo.MRI_JOURNAL MRI_JOURNAL
WHERE (MRI_JOURNAL.ENTITYID='C01400') AND (MRI_JOURNAL.ACCTNUM Like '%CP1810344%') and (MRI_JOURNAL.PERIOD >=?)
 
Upvote 0
Is this in MSAccess?
It was originally posted in the Excel forum.
I moved it to the Access forum, because SQL is closer to Access than Excel!;)
 
Upvote 0
Does the union query work without the parameters (for instance if you hard code in the dates)? Can you think of another way to provide the parameters?
 
Upvote 0
Does the union query work without the parameters (for instance if you hard code in the dates)? Can you think of another way to provide the parameters?

yes, if I hard code the dates it works perfectly. However, I am wanting to roll this query out to a large number of staff members that do not have coding knowledge and I don't want them to have to go in and manually change the dates on a periodic basis.
 
Upvote 0
You'll probably have to use a different strategy for this. It just doesn't work with that type of parameterization.
 
Upvote 0

Forum statistics

Threads
1,221,691
Messages
6,161,310
Members
451,696
Latest member
Senthil Murugan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top