简体   繁体   English

从表中选择并使用ms访问中的查询插入到另一个表的不同类型的列中

[英]Selecting from a table and inserting into another table's column of a different type using query in ms access

I have some txt files that contain tables with a mix of different records on them which have diferent types of values and definitons for columns. 我有一些txt文件,其中包含混合了不同记录的表,这些表具有不同类型的值和列的定义。 I was thinking of importing it into a table and running a query to separate the different record types since a identifier to this is listed in the first column. 我正在考虑将其导入表并运行查询以分隔不同的记录类型,因为第一列中列出了对此的标识符。 Is there a way to change the value type of a column in a query? 有没有办法在查询中更改列的值类型? since it will be a pain to treat all of them as text. 因为将所有这些都视为文本会很痛苦。 If you have any other suggestions on how to solve this please let me know as well. 如果您对如何解决此问题有任何其他建议,请告诉我。

Here is an example of tables for 2 record types provided by the website where I got the data from 以下是我从中获取数据的网站提供的2种记录类型的表的示例

create table dbo.PUBACC_A2
(
      Record_Type               char(2)              null,
      unique_system_identifier  numeric(9,0)         not null,
      ULS_File_Number           char(14)             null,
      EBF_Number                varchar(30)          null,
      spectrum_manager_leasing  char(1)              null,
      defacto_transfer_leasing  char(1)              null,
      new_spectrum_leasing      char(1)              null,
      spectrum_subleasing       char(1)              null,
      xfer_control_lessee       char(1)              null,
      revision_spectrum_lease   char(1)              null,
      assignment_spectrum_lease char(1)              null,
      pfr_status        char(1)          null

)

go
create table dbo.PUBACC_AC
(
      record_type               char(2)              null,
      unique_system_identifier  numeric(9,0)         not null,
      uls_file_number           char(14)             null,
      ebf_number                varchar(30)          null,
      call_sign                 char(10)             null,
      aircraft_count            int                  null,
      type_of_carrier           char(1)              null,
      portable_indicator        char(1)              null,
      fleet_indicator           char(1)              null,
      n_number                  char(10)             null
)

Yes, you can do what you want. 是的,你可以做你想做的事。 In ms access you can use any VBA functions and with some 在ms访问中,您可以使用任何VBA功能和一些

IIF(FirstColumn="value1", CDate(SecondColumn), NULL) as DateValue,
IIF(FirstColumn="value2", CDec(SecondColumn), NULL) as DecimalValue,
IIF(FirstColumn="value3", CStr(SecondColumn), NULL) as StringValue

You can use all/any of the above in your SELECT. 您可以在SELECT中使用上述全部/任何一项。

EDIT: 编辑:

From your comments it seems that you want to split them into different tables - importing as text should not be a problem in that case. 从您的评论看来,您似乎希望将它们拆分为不同的表格 - 在这种情况下,作为文本导入应该不是问题。

a) After you import and get it in the initial table, create the proper table manually setting you can INSERT into the proper table. a)导入并在初始表中获取后,手动创建正确的表设置,您可以插入正确的表。

b) You could even do a make table query , but it might be faster to create it manually. b)您甚至可以进行make table查询 ,但手动创建它可能会更快。 If you do a make table query you have to be sure that you have casted the data into proper type in your select. 如果进行生成表查询,则必须确保已将数据转换为选择中的正确类型。

EDIT2: As you updated the question showing the structure it becomes obvious that my suggestion above will not help directly. 编辑2:当您更新显示结构的问题时,很明显我的建议无法直接帮助。

If this is one time process you can follow HLGEM's solution. 如果这是一次性过程,您可以遵循HLGEM的解决方案。 Here are some more details. 这里有一些更多的细节。

1) Import into a table with two columns - RecordType char(2), Rest memo 1)导入到具有两列的表中 - RecordType char(2),Rest memo

2) Now you can split the data (make two queries that select based on RecordType) and re-export the data (to be able to use access' import wizard) 2)现在您可以拆分数据(根据RecordType进行两个查询选择)并重新导出数据(以便能够使用访问'导入向导)

3) Now you have two text files with proper structure which can be easily imported 3)现在您有两个具有适当结构的文本文件,可以轻松导入

I did this in my last job. 我在上一份工作中这样做了。 You start with a staging table that has one column or two coulmns if your identifier is always the same length. 如果您的标识符始终具有相同的长度,则从具有一列或两个coulmns的临时表开始。 Then using the record identifier, you move the data to another set of staging tables, one for each type of record you have. 然后使用记录标识符将数据移动到另一组临时表,每个表对应一种类型的记录。 This will be in columns for the data and can have the correct data types. 这将在数​​据列中,并且可以具有正确的数据类型。 Then you do any data cleaning you need to do. 然后,您需要执行任何数据清理。 Then you insert into the real production table. 然后插入到真实的生产表中。

If you have a column defined as text, because it has both alphas and numbers, you'll only be able to query it as if it were text. 如果您将列定义为文本,因为它同时具有字母和数字,您将只能将其视为文本。 Once you've separated out the different "types" of data into their own tables, you should be able to change the schema definition. 一旦将不同的“类型”数据分离到自己的表中,您就应该能够更改模式定义。 Please comment here if I'm misunderstanding what you're trying to do. 如果我误解了你想要做什么,请在这里发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM