【问题标题】:Why am I getting Incorrect syntax in my SQL query? [closed]为什么我的 SQL 查询中的语法不正确? [关闭]
【发布时间】:2023-01-04 20:34:42
【问题描述】:

我是新来的,但这是我获得 SQL 帮助的唯一想法。我是 SQL 查询的新手,只知道它的基础知识,所以我希望你能理解我。 我做了 2 个查询 - #1 查询创建了包含很多行的表 tempBus。并插入来自 Stock Procedure 的数据。 然后我得到带有数据的表 tempBus。现在 #2 查询正在创建另一个表 tempBus2,它仅从表 tempBus 中插入特定数据,并将一些字段从 1 转换为 YES,从 0 转换为 NO。但是在运行我的 #2 查询时我得到了不正确的语法错误。 例子:

use DATABASE

IF OBJECT_ID('tmpBus2') IS NOT NULL
DROP TABLE tmpBus2
CREATE TABLE tmpBus2

(
Application nvarchar(50),
OrgHierarchy nvarchar(max),
ManufacturerName nvarchar(50),
ApplicationMetric nvarchar(100),
TotalLicenses int,
LicenseRequirement int,
AvailableLicenses int,
Compliance int
)

insert into dbo.tmpBus2

Application,
OrgHierarchy AS 'Organisation',
manufacturername AS 'Manufacturer',
(case applicationmetric  
when '1' then 'Installations' 
when '2' then 'Custom compare values' 
when '7' then 'Number of processors' 
when '8' then 'Number of processor cores' 
when '9' then 'Users' 
when '10' then 'Devices' 
when '11' then 'Concurrent users' 
when '12' then 'Concurrent devices' 
when '13' then 'PVU' 
when '14' then 'CAL (Client Access License)'
else 'Unknown'
end) AS 'Metric',
totallicenses AS 'Total Licenses',
Licenserequirement AS 'License Requirement',
availablelicenses AS 'Available Licenses',
Compliance AS 'Compliance'

from tmpbus

它给我错误:消息 102,级别 15,状态 1,第 21 行。“应用程序”附近的语法不正确。

我希望将数据放入 tmpBus2 表中,并将 ApplicationMetric 显示为文本,而不是 1-14 的数字。

【问题讨论】:

  • 缺少选择。
  • 请注意,表格有,不是字段。
  • 你好@jarlh!这个 SELECT 应该在 INSERT 之前?
  • INSERT INTO targettable SELECT ...

标签: sql


【解决方案1】:

您在列列表之前缺少 select

insert into dbo.tmpBus2
select -- Here!
Application,
OrgHierarchy AS 'Organisation',
manufacturername AS 'Manufacturer',
(case applicationmetric  
when '1' then 'Installations' 
when '2' then 'Custom compare values' 
when '7' then 'Number of processors' 
when '8' then 'Number of processor cores' 
when '9' then 'Users' 
when '10' then 'Devices' 
when '11' then 'Concurrent users' 
when '12' then 'Concurrent devices' 
when '13' then 'PVU' 
when '14' then 'CAL (Client Access License)'
else 'Unknown'
end) AS 'Metric',
totallicenses AS 'Total Licenses',
Licenserequirement AS 'License Requirement',
availablelicenses AS 'Available Licenses',
Compliance AS 'Compliance'

from tmpbus

【讨论】:

    【解决方案2】:

    基本上你缺少的是申请前的 SELECT 。其次,您永远不应该依赖创建顺序并指定插入字段列表:

    IF OBJECT_ID('tmpBus2') IS NOT NULL
        DROP TABLE tmpBus2;
    CREATE TABLE tmpBus2
    (
        [Application] NVARCHAR(50),
        OrgHierarchy NVARCHAR(MAX),
        ManufacturerName NVARCHAR(50),
        ApplicationMetric NVARCHAR(100),
        TotalLicenses INT,
        LicenseRequirement INT,
        AvailableLicenses INT,
        Compliance INT
    );
    
    INSERT INTO dbo.tmpBus2
    (
        [Application],
        OrgHierarchy,
        ManufacturerName,
        ApplicationMetric,
        TotalLicenses,
        LicenseRequirement,
        AvailableLicenses,
        Compliance
    )
    SELECT
        [Application],
        OrgHierarchy,
        ManufacturerName,
        (CASE ApplicationMetric
             WHEN '1' THEN
                 'Installations'
             WHEN '2' THEN
                 'Custom compare values'
             WHEN '7' THEN
                 'Number of processors'
             WHEN '8' THEN
                 'Number of processor cores'
             WHEN '9' THEN
                 'Users'
             WHEN '10' THEN
                 'Devices'
             WHEN '11' THEN
                 'Concurrent users'
             WHEN '12' THEN
                 'Concurrent devices'
             WHEN '13' THEN
                 'PVU'
             WHEN '14' THEN
                 'CAL (Client Access License)'
             ELSE
                 'Unknown'
         END
        ),
        TotalLicenses,
        LicenseRequirement,
        AvailableLicenses,
        Compliance
    FROM tmpbus;
    

    【讨论】:

      猜你喜欢
      • 2011-12-25
      • 1970-01-01
      • 2020-07-11
      • 2017-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-16
      • 2023-01-06
      相关资源
      最近更新 更多