更新命令

时间:2017-03-23 03:45:42

标签: c# sql visual-studio-2015

尝试使用存储过程从Visual Studio更新SQL数据库中的现有条目。调试实际SQL语句时得到的错误是:

Conversion failed when converting date and/or time from character string.

即使我以NULL或适当的时间格式存根。 这是SQL代码

ALTER PROCEDURE [dbo].[SP_UpdateQuery] (
        @NewName NVARCHAR(25) = NULL,
        @Name NVARCHAR(25) = NULL,
        @Surname NVARCHAR(25) = NULL,
        @NewSurname NVARCHAR(25) = NULL,
        @CompanyName NVARCHAR(25) = NULL,
        @NewCompanyName NVARCHAR(25) = NULL,
        @Address NVARCHAR(35) = NULL,
        @NewAddress NVARCHAR(35) = NULL,
        @MobileNo NVARCHAR(12) = NULL,
        @NewMobileNo NVARCHAR(12) = NULL,
        @PhoneNo NVARCHAR(12) = NULL,
        @NewPhoneNo NVARCHAR(12) = NULL,
        @Email NVARCHAR(40) = NULL,
        @NewEmail NVARCHAR(40) = NULL,
        @AreaCode NVARCHAR(10) = NULL,
        @NewAreaCode NVARCHAR(10) = NULL,
        @DateTime DateTime = NULL,
        @NewDateTime DateTime = NULL
        )
AS
UPDATE [dbo].[CUSTOMERS]
SET Name = @NewName, Surname = @NewSurname, Email = @NewEmail, MobileNo = @NewMobileNo, PhoneNo = @NewPhoneNo,
    CompanyName = @NewCompanyName, Address = @NewAddress, AreaCode = @NewAreaCode, DateTime = @NewDateTime
WHERE (@Name IS NULL OR Name LIKE '%' + @Name + '%')
      AND (@Surname IS NULL OR Surname LIKE '%' + @Surname + '%')
      AND (@Email IS NULL OR Email LIKE '%' + @Email + '%')
      AND (@MobileNo IS NULL OR MobileNo LIKE '%' + @MobileNo + '%')
      AND (@PhoneNo IS NULL OR PhoneNo LIKE '%' + @PhoneNo + '%')
      AND (@Address IS NULL OR Address LIKE '%' + @Address + '%')
      AND (@CompanyName IS NULL OR CompanyName LIKE '%' + @CompanyName + '%')
      AND (@Email IS NULL OR Email LIKE '%' + @Email + '%')
      AND (@AreaCode IS NULL OR AreaCode LIKE '%' + @AreaCode + '%')
      AND (@DateTime IS NULL OR DateTime LIKE '%' + @DateTime + '%')

表中的现有时间条目是这样的

2016-11-10 00:00:00.000

同样,在实际调试SQL时会发生错误,因此尚未在Visual Studio中修复任何问题。

我的测试用例一直允许Time变量默认为NULL并输入这些值

@DateTime = N'2016-11-10T00:00:00',
@NewDateTime = N'1900-01-01T01:01:01'
//New Test Case
@DateTime = N'1900-01-01 00:00:00',
@NewDateTime = N'1900-01-01 01:01:01'

我能找到的最相似的问题最终是C#代码中的错误,但由于SQL实际上是在这里讨论,可以使用一些帮助。谢谢你的时间!

1 个答案:

答案 0 :(得分:0)

如评论中所述,替换

AND (@DateTime IS NULL OR DateTime LIKE '%' + @DateTime + '%')

AND @DateTime IS NULL