列名无效 - 列是否存在

时间:2017-07-14 17:19:02

标签: sql-server

我有一个表格,我添加了一个新列,如下:

ALTER TABLE [Invoice].[Invoice] ADD [DaysToPay] int
GO
ALTER TABLE [Invoice].[Invoice] ADD CONSTRAINT [DF_Invoice_DaysToPay]  DEFAULT ((30)) FOR [DaysToPay]
GO
UPDATE [Invoice].[Invoice] SET [DaysToPay] = 30
GO
ALTER TABLE [Invoice].[Invoice] ALTER COLUMN [DaysToPay] int NOT NULL
GO

如果我现在SELECT * FROM [Invoice].[Invoice]我收到Invalid column name 'DaysToPay'错误。无论我是通过SSMS还是通过任何其他方式(通过代码)运行语句,我都会收到此错误。

当我选择特定列时,有或没有列时会发生同样的错误。例如SELECT [InvoiceID] FROM [Invoice].[Invoice]

我关闭了SSMS并重新启动了SQL服务。仍然是同样的错误。

该列是在获取错误的同一用户下添加的,并且该用户是架构的所有者。

这两个语句都返回0行:

SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'DaysToPay'

SELECT *
FROM sys.columns 
WHERE Name = N'DaysToPay'

我已尝试 Ctrl + Shift + R 但我不认为这是SSMS的事情它没有任何区别。

不会复制数据库。

我跑了DBCC OPENTRAN没有活动。

我已经删除了列(在SSMS中手动使用设计器),我仍然得到相同的错误。该表在SSMS设计器中正确显示(就像我之前手动删除它一样)。

除了删除和重新创建表格之外的任何想法?

编辑: 运行的其余脚本如下(我删除了某些列以便不识别):

ALTER PROCEDURE [Invoice].[Invoice_Save] (
    @InvoiceID int,
    @TypeID int,
    @InvoiceNumber int,
    @InvoiceDate datetime,
    @DatePaid datetime,
    @PaymentEnteredBy int,
    @AmountPaid float,
    @PaymentTypeID int,
    @HasVAT bit,
    @VATRate float,
    @TranslationPercentageCharge float,
    @IsVoid bit,
    @VoidDate datetime,
    @VoidBy int,
    @DateSent datetime,
    @SentBy int,
    @PurchaseOrderNumber varchar(100),
    @BillingContact varchar(255),
    @BillingAddress varchar(MAX),
    @BillingContactEmail nvarchar(255),
    @BillingContactTelNo varchar(50),
    @Notes varchar(MAX),
    @CompanyID int,
    @DaysToPay int,
    @DateCreated datetime,
    @CreatedBy int,
    @LastUpdated datetime,
    @LastUpdatedBy int
)

AS

    IF NOT EXISTS (SELECT * FROM [Invoice].[Invoice] WHERE [InvoiceID] = @InvoiceID)
    BEGIN
        INSERT INTO [Invoice].[Invoice] ([TypeID], [InvoiceNumber], [InvoiceDate], , [DatePaid], [PaymentEnteredBy], [AmountPaid], [PaymentTypeID], [HasVAT], [VATRate], [TranslationPercentageCharge], [IsVoid], [VoidDate], [VoidBy], [DateSent], [SentBy], [PurchaseOrderNumber], [BillingContact], [BillingAddress], [BillingContactEmail], [BillingContactTelNo], [Notes], [CompanyID], [DaysToPay], [DateCreated], [CreatedBy], [LastUpdated], [LastUpdatedBy])
        VALUES (@TypeID, @InvoiceNumber, @InvoiceDate, @DatePaid, @PaymentEnteredBy, @AmountPaid, @PaymentTypeID, @HasVAT, @VATRate, @TranslationPercentageCharge, @IsVoid, @VoidDate, @VoidBy, @DateSent, @SentBy, @PurchaseOrderNumber, @BillingContact, @BillingAddress, @BillingContactEmail, @BillingContactTelNo, @Notes, @CompanyID, @DaysToPay, @DateCreated, @CreatedBy, @LastUpdated, @LastUpdatedBy)

        SELECT MAX([InvoiceID]) AS MaxID FROM [Invoice].[Invoice]
    END
    ELSE
        UPDATE [Invoice].[Invoice]
        SET [TypeID] = @TypeID,
        [InvoiceNumber] = @InvoiceNumber,
        [InvoiceDate] = @InvoiceDate,
        [DatePaid] = @DatePaid,
        [PaymentEnteredBy] = @PaymentEnteredBy,
        [AmountPaid] = @AmountPaid,
        [PaymentTypeID] = @PaymentTypeID,
        [HasVAT] = @HasVAT,
        [VATRate] = @VATRate,
        [TranslationPercentageCharge] = @TranslationPercentageCharge,
        [IsVoid] = @IsVoid,
        [VoidDate] = @VoidDate,
        [VoidBy] = @VoidBy,
        [DateSent] = @DateSent,
        [SentBy] = @SentBy,
        [PurchaseOrderNumber] = @PurchaseOrderNumber,
        [BillingContact] = @BillingContact,
        [BillingAddress] = @BillingAddress,
        [BillingContactEmail] = @BillingContactEmail,
        [BillingContactTelNo] = @BillingContactTelNo,
        [Notes] = @Notes,
        [CompanyID] = @CompanyID,
        [DaysToPay] = @DaysToPay,
        [DateCreated] = @DateCreated,
        [CreatedBy] = @CreatedBy,
        [LastUpdated] = @LastUpdated,
        [LastUpdatedBy] = @LastUpdatedBy
        WHERE [InvoiceID] = @InvoiceID
GO

0 个答案:

没有答案