INSERT INTO - 访问SQL与SQL Server

时间:2017-04-24 23:09:20

标签: sql sql-server ms-access

当我尝试将Access SQL代码用于INSERT INTO语句并转换为更有组织的SQL Server版本时,我遇到了一个随机问题。下面是Access SQL,除了第一个SELECT - " SELECT" Shipment" AS Type ...我没有看到它从中提取数据的任何来源,它在Access的设计视图中被称为表达式。我真的只在SQL Server中工作,所以我希望有人可以帮我解释这里的意图。感谢。

'INSERT INTO [Fcst/Order/Ship Data] ( Type, [Acct#], BU, Gender, Material, Description, [Size], [Year], Mth, Qty, BS )
SELECT "Shipment" AS Type, [Fcst v Order 03: Create Temp Invoice Tbl (Invoiced)].[BILL TO] AS [Acct#], dbo_vw_kd_BUSINESS_UNIT.BUSINESS_UNIT_DESC AS BU, dbo_vw_kd_GENDER.GENDER_DESC AS Gender, dbo_VW_INVOICES_SIZE.MATERIAL AS Material, dbo_vw_MM_MATERIAL.DESCRIPTION AS Description, dbo_VW_INVOICES_SIZE.SIZE_LITERAL, Year([requested_delivery_date]) AS [Year], Month([requested_delivery_date]) AS Mth, Sum(dbo_VW_INVOICES_SIZE.INVOICED_QUANTITY) AS Qty, dbo_vw_kd_BUSINESS_SEGMENT.BUSINESS_SEGMENT_DESC
FROM ([Fcst v Order 03: Create Temp Invoice Tbl (Invoiced)] INNER JOIN (dbo_VW_INVOICES_SIZE INNER JOIN ((dbo_vw_MM_MATERIAL INNER JOIN dbo_vw_kd_BUSINESS_UNIT ON dbo_vw_MM_MATERIAL.BUSINESS_UNIT_CODE = dbo_vw_kd_BUSINESS_UNIT.BUSINESS_UNIT_CODE) INNER JOIN dbo_vw_kd_GENDER ON dbo_vw_MM_MATERIAL.GENDER_CODE = dbo_vw_kd_GENDER.GENDER_CODE) ON dbo_VW_INVOICES_SIZE.MATERIAL = dbo_vw_MM_MATERIAL.MATERIAL) ON ([Fcst v Order 03: Create Temp Invoice Tbl (Invoiced)].SIZE_INDEX = dbo_VW_INVOICES_SIZE.SIZE_LITERAL) AND ([Fcst v Order 03: Create Temp Invoice Tbl (Invoiced)].BILLING_DOCUMENT_NUMBER = dbo_VW_INVOICES_SIZE.BILLING_DOCUMENT_NUMBER) AND ([Fcst v Order 03: Create Temp Invoice Tbl (Invoiced)].BILLING_ITEM_NUMBER = dbo_VW_INVOICES_SIZE.BILLING_ITEM_NUMBER)) INNER JOIN dbo_vw_kd_BUSINESS_SEGMENT ON dbo_vw_MM_MATERIAL.BUSINESS_SEGMENT_CODE = dbo_vw_kd_BUSINESS_SEGMENT.BUSINESS_SEGMENT_CODE
GROUP BY [Fcst v Order 03: Create Temp Invoice Tbl (Invoiced)].[BILL TO], dbo_vw_kd_BUSINESS_UNIT.BUSINESS_UNIT_DESC, dbo_vw_kd_GENDER.GENDER_DESC, dbo_VW_INVOICES_SIZE.MATERIAL, dbo_vw_MM_MATERIAL.DESCRIPTION, dbo_VW_INVOICES_SIZE.SIZE_LITERAL, Year([requested_delivery_date]), Month([requested_delivery_date]), dbo_vw_kd_BUSINESS_SEGMENT.BUSINESS_SEGMENT_DESC
HAVING (((Year([requested_delivery_date]))=2017));

2 个答案:

答案 0 :(得分:3)

对于所有插入的记录,它只是一个常量字符串[Fcst/Order/Ship Data].Type

您可以在SQL Server中执行相同的操作,只需使用单引号而不是双引号。

修改

在SQL Server中,您可以

INSERT INTO foo (type, bar)
SELECT 'Hello World' AS type, bazbar
FROM baz

或者可能使用[type],因为这是Transact-SQL中的保留字(CREATE TYPE)。

AS type并不是真的需要,这也可行:

INSERT INTO foo (type, bar)
SELECT 'Hello World', bazbar
FROM baz

列别名对

很有用
  • 单独测试SELECT部分​​
  • 自我记录SQL代码(用于非平凡的陈述)

答案 1 :(得分:1)

如果您只是专门讨论SELECT "Shipment" as Type,而不是后面的内容,那么正在做的只是将字符串文字"发货"在它添加的每一行的Type列中。

请参阅:https://docs.microsoft.com/en-us/sql/t-sql/queries/select-clause-transact-sql