?
Solved

SQL Select UNION add database name

Posted on 2013-12-09
6
Medium Priority
?
405 Views
Last Modified: 2013-12-10
I am creating several UNION queries to eventually combine data from 3 different company databases.

One of the current queries looks like this:

SELECT     DocEntry, DocNum, DocType, CANCELED, DocStatus, DocDate, CardCode, CardName, NumAtCard, DocTotal, GrosProfit, Ref1, VatSumSy, DiscSumSy, TaxDate,
                      ShipToCode, OwnerCode, SlpCode
FROM         OINV
UNION ALL
SELECT     DocEntry, DocNum, DocType, CANCELED, DocStatus, DocDate, CardCode, CardName, NumAtCard, DocTotal, GrosProfit, Ref1, VatSumSy, DiscSumSy, TaxDate,
                      ShipToCode, OwnerCode, SlpCode
                     
FROM ORIN      

I want to add the database name or the company name to all queries so I can later combine them or pull the data from each company  separately.

If I can't get the database name, a column with a text field for each would be fine.
I just need to add it to every query. ie: "ABC" "XYZ" or "MMM"

How can I do this?
0
Comment
Question by:actsoft
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 39706982
Perhaps I'm over-simplifying it but...

SELECT     'OINV' AS Source, DocEntry, DocNum, DocType, CANCELED, DocStatus, DocDate, CardCode, CardName, NumAtCard, DocTotal, GrosProfit, Ref1, VatSumSy, DiscSumSy, TaxDate,
                      ShipToCode, OwnerCode, SlpCode
FROM         OINV
UNION ALL
SELECT     'ORIN', DocEntry, DocNum, DocType, CANCELED, DocStatus, DocDate, CardCode, CardName, NumAtCard, DocTotal, GrosProfit, Ref1, VatSumSy, DiscSumSy, TaxDate,
                      ShipToCode, OwnerCode, SlpCode
                     
FROM ORIN
0
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39707027
That's exactly what I understood from the question too BriCrowe!
0
 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 total points
ID: 39707058
--- What's the current database name?
SELECT db_name()

Also, where you have OINV and ORIN, usually if you're sporting a cross-database query the label is handled above, and you have to spell out database name.schema name.object name (or just database name..object name if it's the default schema), like this ...

SELECT 'OINV' AS Source, DocEntry, DocNum, DocType, CANCELED, DocStatus, DocDate, CardCode, CardName, NumAtCard, DocTotal, GrosProfit, Ref1, VatSumSy, DiscSumSy, TaxDate, ShipToCode, OwnerCode, SlpCode
FROM OINV..SomeTable
UNION ALL
SELECT  'ORIN', DocEntry, DocNum, DocType, CANCELED, DocStatus, DocDate, CardCode, CardName, NumAtCard, DocTotal, GrosProfit, Ref1, VatSumSy, DiscSumSy, TaxDate, 
ShipToCode, OwnerCode, SlpCode
FROM ORIN..SomeTable

Open in new window

0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

Author Comment

by:actsoft
ID: 39707082
The SELECT db_name(), worked perfectly. Thanks
0
 

Author Closing Comment

by:actsoft
ID: 39708809
worked perfectly, thanks
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39708960
Thanks for the grade, although I'm guessing BriCrowe's comment may have helped too.
Good luck with your project.  

Jim
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

770 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