• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

SQL UNION Query

Hi

How can I make this work if tblEmailTemplates is empty?

SELECT 0 AS lngTemplateID,' Default Template' AS strTemplateName  
FROM tblEmailTemplates
UNION
SELECT tblEmailTemplates.lngTemplateID, tblEmailTemplates.strTemplateName  
FROM tblEmailTemplates    
ORDER BY tblEmailTemplates.strTemplateName;

If tblEmailTemplates is empty it doesn't return any records when I want it to return a record for Default Template

I want to use it to populate the RowSource property in an Access combo box

Cheers
MikeW
0
Reklaw
Asked:
Reklaw
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
please try:

SELECT 0 AS lngTemplateID,' Default Template' AS strTemplateName  
FROM DUAL
UNION
SELECT tblEmailTemplates.lngTemplateID, tblEmailTemplates.strTemplateName  
FROM tblEmailTemplates    
ORDER BY tblEmailTemplates.strTemplateName;

Open in new window


if "DUAL" doesn't work, please ensure you have a table that has at least 1 row, and do:


SELECT TOP 1 0 AS lngTemplateID,' Default Template' AS strTemplateName  
FROM some_non_empty_table
UNION
SELECT tblEmailTemplates.lngTemplateID, tblEmailTemplates.strTemplateName  
FROM tblEmailTemplates    
ORDER BY tblEmailTemplates.strTemplateName;

Open in new window

0
 
ReklawAuthor Commented:
DUAL didn't work but using any none empty table seems to work fine. I would have thought the field names would have had to match but it seems not.

Thanks muchly
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now