我有一个在特定数据库上运行的相当大的查询。此查询所做的是从特定表中选择特定列并将它们转储到新表中。稍后在查询中使用和修改新表。
我遇到的问题是,虽然数据库应该来自同一个程序,但一个数据库可能有 email1、email2 和 email3 列,但下一个可能只有 email1。因为 email1-3 用作 select 语句的一部分,所以当缺少这些列之一时,该部分将失败并且永远不会创建表。
我对此仍然很陌生,所以我的问题可能很简单或很愚蠢,但是当这样的列丢失时,有什么方法可以保存结果?我可以很容易地删除查询中缺少的列并再次运行它,但我想知道是否有更正确的方法来做到这一点?
例子:
SELECT Customer.[Name],
Customer.AcctKey,
SUBSTRING(Customer.LastName,1,25) AS [Last Name],
SUBSTRING(Customer.FirstName,1,25) AS [First Name],
Customer.Add1 AS Address,
Customer.Add2 AS Address2,
Customer.City,
Customer.State,
Customer.Zip,
Location.Add1 AS [Ship Address],
Location.Add2 AS [Ship Address2],
Location.City AS [Ship City],
Location.State AS [Ship State],
Location.Zip AS [Ship Zip],
Customer.Phone1 AS Phone,
Customer.Phone2 AS [Alt Phone],
Customer.Phone3 AS [Cell Phone],
Customer.Phone4 AS Fax,
Customer.lblPhone1,
Customer.lblPhone2,
Customer.lblPhone3,
Customer.lblPhone4,
Customer.Terms,
Customer.[Key] AS [Account Number],
Location.Notes,
Location.TaxCode AS [Tax Item],
Location.Zone AS [Map Code],
Location.Contact,
Location.Email,
Location.Email6, --Fails because column(s) Email2-6
Location.Email5, --don't exist in this database
Location.Email4, --but they might in another
Location.Email3, --and I'd like to get the data
Location.Email2, --if they do
Location.Phone6,
Location.Phone5
INTO [01Parents]
FROM Customer
INNER JOIN Location ON Customer.[Key] = Location.[Key]