创建存储过程时无效的列名称错误

时间:2011-12-22 23:10:42

标签: sql sql-server sql-server-2008

我正在尝试创建存储过程,但我收到了这些错误:

Msg 207, Level 16, State 1, Procedure Insert_QuickLabDump, Line 42
Invalid column name 'Date_Collected'.
Msg 207, Level 16, State 1, Procedure Insert_QuickLabDump, Line 43
Invalid column name 'Time_Collected'.
Msg 207, Level 16, State 1, Procedure Insert_QuickLabDump, Line 43
Invalid column name 'Date_Entered'.
Msg 207, Level 16, State 1, Procedure Insert_QuickLabDump, Line 44
Invalid column name 'Time_Entered'.
Msg 207, Level 16, State 1, Procedure Insert_QuickLabDump, Line 45
Invalid column name 'Date_Completed'.
Msg 207, Level 16, State 1, Procedure Insert_QuickLabDump, Line 46
Invalid column name 'Time_Completed'.
Msg 207, Level 16, State 1, Procedure Insert_QuickLabDump, Line 47
Invalid column name 'Test_Date'.
Msg 207, Level 16, State 1, Procedure Insert_QuickLabDump, Line 48
Invalid column name 'Test_Time'.

这里有完整的资料来源:

USE [SalesDWH]
GO

/****** Object:  StoredProcedure [dbo].[Insert_QuickLabDump]    Script Date: 12/22/2011 14:52:48 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER on
GO

-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
create PROCEDURE [dbo].[Insert_QuickLabDump]
    -- Add the parameters for the stored procedure here
    @Specimen_ID [varchar](50),
    @Client_Key int,
    @Outcome [varchar](50),
    @Medications [varchar] (max),
    @Date_Collected date,
@Time_Collected time ,
@Date_Entered date,
@Time_Entered time ,
@Date_Completed date,
@Time_Completed time ,
@Test_Date date ,
@Test_Time time ,

    @Practice_Name [varchar] (500),
    @Practice_Code [varchar] (500),
    @Client_ID [varchar] (500),
    @Requesting_Physician [varchar] (500),
    @Other_Medications [varchar] (max),
    @Order_Comments [varchar] (max),
    @Reference_Number [varchar] (500),
    @Order_Count int

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    INSERT INTO [SalesDWH].[dbo].[QuickLabDump]
           ([Specimen ID]
           ,[Client Key]
           ,[Outcome]
           ,[Medications]
           ,[Date_Collected]
           ,[Time_Collected]
           ,[Date_Entered]
           , [Time_Entered]
           , Date_Completed
           , Time_Completed
           , Test_Date
           , Test_Time
           ,[Practice Name]
           ,[Practice Code]
           ,[Client ID]
           ,[Requesting Physician]
           ,[Other Medications]
           ,[Order Comments]
           ,[Reference Number]
           ,[Order Count]
     )
     VALUES
           (@Specimen_ID,
@Client_Key,
@Outcome,
@Medications,
@Date_Collected ,
@Time_Collected ,
@Date_Entered,
@Time_Entered ,
@Date_Completed ,
@Time_Completed,
@Test_Date ,
@Test_Time,
@Practice_Name,
@Practice_Code,
@Client_ID,
@Requesting_Physician,
@Other_Medications,
@Order_Comments,
@Reference_Number,
@Order_Count
)


END

我做错了什么?

这是表结构:

USE [SalesDWH]
GO

/****** Object:  Table [dbo].[QuickLabDump]    Script Date: 12/22/2011 15:13:40 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[QuickLabDump](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [Specimen ID] [varchar](50) NOT NULL,
    [Client Key] [int] NOT NULL,
    [Outcome] [varchar](50) NOT NULL,
    [Medications] [varchar](max) NULL,
    [Date Collected] [date] NOT NULL,
    [Time Collected] [time](7) NOT NULL,
    [Date Entered] [date] NOT NULL,
    [Time Entered] [time](7) NOT NULL,
    [Date Completed] [date] NOT NULL,
    [Time Completed] [time](7) NOT NULL,
    [Test Date] [date] NOT NULL,
    [Test Time] [time](7) NOT NULL,
    [Practice Name] [varchar](500) NOT NULL,
    [Practice Code] [varchar](500) NOT NULL,
    [Client ID] [varchar](500) NULL,
    [Requesting Physician] [varchar](500) NULL,
    [Other Medications] [varchar](max) NULL,
    [Order Comments] [varchar](max) NULL,
    [Reference Number] [varchar](500) NULL,
    [Order Count] [int] NOT NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

4 个答案:

答案 0 :(得分:9)

错误表明Date_Collected表中没有此类列(例如QuickLabDump)。对于insert语句,您必须具有正确的列名。例如,您有一个名为Date Collected的列,而不是Date_Collected的列(注意空格而不是下划线)。

答案 1 :(得分:6)

看起来您已将这些列添加到现有过程中。您是否已将列添加到要尝试插入的数据库表中?

答案 2 :(得分:5)

您在存储过程中使用[Date_Collected],但根据CREATE TABLE语句的实际列名是[Date Collected](无下划线)。

答案 3 :(得分:2)

[收集日期] [日期] NOT NULL, 与消息207,级别16,状态1,过程Insert_QuickLabDump,第42行不同 列名称“Date_Collected”无效。