?? query with a Declaired Variable shows data in the MS Query window but return no lines Excel?

I have a query that runs super fast in SQL Studio.  I have copied this into MS query and it shows the results in teh MS query window, but when I click return data nothing is returned to Excel.  If I comment out my variable statement it works just fine...
My statement is in the first 2 lines and is"
Declare @EmployerCode varchar(10)
Set @EmployerCode = 'FCH'

kgittingerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rory ArchibaldCommented:
I don't believe that querytables support that sort of declaration. Can you not create an SP (or view) in the SQL database and simply execute that?
0
kgittingerAuthor Commented:
I cannot create a view since the database is at a third party site...

0
Rory ArchibaldCommented:
Ah. I'd probably use code to change the commandtext of the querytable then. That way you can effectively hardcode the variables into the SQL string each time.
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

kgittingerAuthor Commented:
That is where I started...I am trying to get to the point where I can have a parameter field in a cell in excel for the user to change. Since my query is a union all it is giving me trouble in the union select
0
Rory ArchibaldCommented:
It shouldn't be a problem in code because you aren't really using parameters as such - the whole SQL string gets rebuilt including the range value as a literal value within the string. So along the lines of:
Activesheet.querytables(1).commandtext = "SELECT blah from yadda where foo = '" & range("A1").value & " UNION ALL Select summat from elsewhere where foo2 = '" & range("A1").value & "'"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kgittingerAuthor Commented:
I will give it a whirl. Thanks
0
Rory ArchibaldCommented:
If you add the code into a worksheet_change event then you can have the table updated automatically when a different value is typed into your criteria cell.
0
kgittingerAuthor Commented:
Thanks.  If I need more help I will ask a related question  I may need help tweeking my SQL  I am a beginner
0
kgittingerAuthor Commented:
I do need help in how to have my SQL refernce information from a page in a workbook.  Please let me know when you are availble so I can post my SQL for assistance.

0
Rory ArchibaldCommented:
I suggest you post it as a related question, then everyone can chime in as needed. :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.