如何在 MS Access 中的 SQL SELECT INTO 语句中为新字段定义类型(int)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20043231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 23:05:28  来源:igfitidea点击:

How to define the type (int) for a new field in SQL SELECT INTO statement in MS Access

sqlms-accessselect-into

提问by Onur T

I want to define the new field in a select into statement as integer. However, the NewField ends up as binary. How can I accomplish this?

我想将 select into 语句中的新字段定义为整数。但是,NewField 最终是二进制的。我怎样才能做到这一点?

SELECT ExistingField1, ExistingField2, NewField
INTO NewTable
FROM ExistingTable

I searched a lot but couldn't find a solution yet.

我搜索了很多,但还没有找到解决方案。

Edit 1: I am performing the SELECT INTO statement from within the Microsoft Access application itself. (it is not a table in Access that points to a SQL Server table)

编辑 1:我正在从 Microsoft Access 应用程序本身中执行 SELECT INTO 语句。(它不是 Access 中指向 SQL Server 表的表)

Edit 2: NewField is created with the SELECT INTO statement. It does not exist in a pre-existing table.

编辑 2:NewField 是使用 SELECT INTO 语句创建的。它不存在于预先存在的表中。

回答by SchmitzIT

The reason it ends up as a binary is because the field in the existing table most likely is a binary field.

它以二进制结尾的原因是因为现有表中的字段很可能是二进制字段。

You could try doing something like this:

你可以尝试做这样的事情:

SELECT ExistingField1, ExistingField2, CAST(NewField AS int)
INTO NewTable
FROM ExistingTable

CASTdoes not work in MsAccess. However, this should work:

CAST在 MsAccess 中不起作用。但是,这应该有效:

SELECT ExistingField1, ExistingField2, cInt(Field1 + Field2) AS NewField
INTO NewTable
FROM ExistingTable

回答by peterincumbria

For Access I would use this, then the parameter dialog does not show.

对于 Access,我会使用它,然后不显示参数对话框。

SELECT ExistingField1, cInt(0) AS NewField
into NewTable1
FROM Table1

For SQL Server

对于 SQL Server

CAST(null AS int) AS NewField

回答by Deepshikha

If you have structure of NewTable already defined you can try:

如果您已经定义了 NewTable 的结构,您可以尝试:

Insert Into NewTable 
Select 
      ExistingField1,
      ExistingField2,
      cast(NewField as int) as NewField,
From ExistingTable