tags:

views:

68

answers:

5

We performed an upgrade of a proprietary program that formerly used Access and now uses SQL. The table schema are exactly the same, but the problem is that the primary key restarted when we switched to SQL. I've imported the old access database into another sql table in the database. The current (SQL based) table is named Archive and the imported Access one is ArchivePreSQL. What I'm trying to do is to create a query that joins those two tables (so I have all records in one query) and adding an "S" to the primary key if it's from the SQL table and an "A" if it's from the imported Access table. Is it possible to do this?

I can get the first chunk of data like this, but I'm unsure how to JOIN it with the second table how I desire.

SELECT 'S' + CAST(id AS varchar(MAX)) AS id, Account, BillingNumber, TakenTimestamp FROM Archive

Any help with this is much appreciated!

+1  A: 

I think you want to use a UNION. The idea with UNION is that you have two (or more) set of SELECT statements, both of which are expected to return the same exact number of columns with the same types for each, and then they get concatenated together.

So for example:

SELECT 'S' + CAST(id AS varchar(MAX)) AS id, Account, BillingNumber, TakenTimestamp FROM Archive
UNION SELECT 'A' + CAST(id AS varchar(MAX)) AS id, Account, BillingNumber, TakenTimestamp FROM ArchivePreSQL
Adam Batkin
A: 

Use the UNION keyword


SELECT 'S' + CAST(id AS varchar(MAX)) AS id,
       Account,
       BillingNumber,
       TakenTimestamp
FROM Archive
UNION
SELECT 'A' + CAST(id AS varchar(MAX)) AS id,
       Account,
       BillingNumber,
       TakenTimestamp
FROM ArchivePreSql
Simon Nickerson
+2  A: 

If the sets don't intersect, just UNION ALL them:

  SELECT 'S' + CAST(id AS varchar(MAX)) AS id, Account, BillingNumber, TakenTimestamp
  FROM Archive
  UNION ALL
  SELECT 'A' + CAST(id AS varchar(MAX)) AS id, Account, BillingNumber, TakenTimestamp
  FROM ArchivePreSQL
Quassnoi
Not formatted, but has UNION ALL and does provide the columns desired without second-guessing the requirements (I agree the resulting column might be less than useful, but I'd say so in a comment). Note that with that hard-coded "S" and "A", the sets cannot intersect.
Philip Kelley
`@Philip`: by "sets don't intersect" I meant that `Archive` does not contain data from `ArchivePreSQL`, i. e. this is a completely new table, not a table imported from `Access` and filled with new data by `SQL Server`.
Quassnoi
+1  A: 

Yes, you can do that, and it can be accomplished via a Union

SELECT 'S' + CAST(id AS varchar(MAX)) AS id, Account, BillingNumber, TakenTimestamp FROM Archive
union
SELECT 'A' + CAST(id AS varchar(MAX)) AS id, Account, BillingNumber, TakenTimestamp FROM ArchivePreSQL

Another alternative, would be to seed your ARCHIVE Identifier with say a number higher than the ArchivePreSQL table's highest identity. Say you have 522 rows in the ArchivePreSQL then seed the ARCHIVE table at say 1000. Then you know that any Identifier below 1000 is in the ArchivePreSql table as opposed to the Archive table.

Also, don't forget that you can temporarily disable the IdentityInsert functionality and perform inserts (including of the identifier) and then re-enable the Identity Insert function (which is what generates the auto-incrementing identifier field) and that will allow you to get rid of the ArchivePreSQL table entirely. If it's a matter of needing to know, you could then couple this with the resetting the Identity Insert seed value so that new items are always above a set amount.

Stephen Wrighton
+2  A: 
SELECT a.id, 
      'A',
       a.account, 
       a.billingnumber, 
       a.takentimestamp
  FROM ARCHIVE a
UNION ALL
SELECT b.id,
      'B',
       b.account, 
       b.billingnumber, 
       b.takentimestamp
  FROM ARCHIVEPRESQL b
 ORDER BY id

I don't recommend prefixing the ID because then you can't order by it.

OMG Ponies
+1 just for formatting your code
Ed Guiness
Why would you order by ID? They generally aren't guaranteed to be in any particular order, just unique
Adam Batkin
Then it will be obvious where there are duplicates or oddities by reading the second column. It's also easier to deal with isolating things rather than have to substring to test for the existence of an arbitrary value.
OMG Ponies