我有一个来自用户表的内容:
Id int Unchecked
Name varchar(50) Checked
EmailAddress varchar(200) Unchecked
Host varchar(250) Unchecked
Port int Unchecked
POPHost varchar(250) Checked
POPPort int Checked
Password varchar(50) Unchecked
CreatedDate smalldatetime Unchecked
user_id int Unchecked
EnableSSL bit Unchecked
Count int Unchecked
SentDateTime smalldatetime Checked
MaxCount int Checked
这是stored-proc,用于按照以下选项更新特定字段:
USE [EASYMAIL]
GO
/****** Object: StoredProcedure [dbo].[UpdateFromUserField] Script Date: 20/02/2017 8:35:24 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[UpdateFromUserField]
(
@fromuser_id int,
@var_name varchar(25),
@var_value varchar(100)
)
AS
BEGIN
SET NOCOUNT ON;
IF @var_name = 'Name'
UPDATE [dbo].[tbl_From_master] SET [Name] =CAST(@var_value AS TEXT) WHERE (Id=@fromuser_id)
ELSE IF @var_name = 'EmailAddress'
UPDATE [dbo].[tbl_From_master] SET [EmailAddress] = CAST(@var_value AS TEXT) WHERE (Id=@fromuser_id)
ELSE IF @var_name = 'Host'
UPDATE [EASYMAIL].[dbo].[tbl_From_master] SET [Host] = CAST(@var_value AS TEXT) WHERE (Id=@fromuser_id)
ELSE IF @var_name = 'Port'
UPDATE [EASYMAIL].[dbo].[tbl_From_master] SET [Port] = CAST(@var_value AS INT) WHERE (Id = @fromuser_id)
ELSE IF @var_name = 'POPHost'
UPDATE [EASYMAIL].[dbo].[tbl_From_master] SET [POPHost] = CAST(@var_value AS TEXT) WHERE (Id=@fromuser_id)
ELSE IF @var_name = 'POPPort'
UPDATE [EASYMAIL].[dbo].[tbl_From_master] SET [POPPort] = CAST(@var_value AS INT) WHERE (Id = @fromuser_id)
ELSE IF @var_name = 'EnableSSL'
UPDATE [EASYMAIL].[dbo].[tbl_From_master] SET [EnableSSL] = CAST(@var_value AS BIT) WHERE (Id = @fromuser_id)
ELSE IF @var_name = 'SentDateTime'
UPDATE [EASYMAIL].[dbo].[tbl_From_master] SET [SentDateTime] = SUBSTRING(@var_value,4,3) + LEFT(@var_value,3) + RIGHT(@var_value,4) WHERE (Id = @fromuser_id)
ELSE IF @var_name = 'Count'
UPDATE [EASYMAIL].[dbo].[tbl_From_master] SET [Count] = CAST (@var_value AS INT) WHERE (Id=@fromuser_id)
ELSE IF @var_name = 'MaxCount'
UPDATE [EASYMAIL].[dbo].[tbl_From_master] SET [MaxCount] = CAST (@var_value AS INT) WHERE (Id=@fromuser_id)
END
但是,执行时间会出错,如:
USE [EASYMAIL]
GO
DECLARE @return_value int
EXEC @return_value = [dbo].[UpdateFromUserField]
@fromuser_id = 1,
@var_name = N'SentDateTime',
@var_value = N'2017-02-20 8:32:12'
SELECT 'Return Value' = @return_value
GO
错误:
Msg 295, Level 16, State 3, Procedure UpdateFromUserField, Line 26
Conversion failed when converting a character string to smalldatetime data type.
这里有什么错误? 请帮帮我
更新
The conversion of a varchar data type to a smalldatetime data type resulted in an out-of-range value.
The statement has been terminated.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: The conversion of a varchar data type to a smalldatetime data type resulted in an out-of-range value.
The statement has been terminated.
Source Error:
Line 618: public int UpdateFromUserField([global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Int")] System.Nullable<int> fromuser_id, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType="VarChar(25)")] string var_name, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType="VarChar(100)")] string var_value)
Line 619: {
Line 620: IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), fromuser_id, var_name, var_value);
Line 621: return ((int)(result.ReturnValue));
Line 622: }
Source File: e:\EASYMAIL_off\EASYMAIL\App_Code\DataClasses.designer.cs Line: 620
答案 0 :(得分:1)
变化:
SET [SentDateTime] = SUBSTRING(@var_value,4,3) + LEFT(@var_value,3) + RIGHT(@var_value,4)
人:
SET [SentDateTime] = CAST(LEFT(@var_value,4) + SUBSTRING(@var_value,6,2) + SUBSTRING(@var_value,9,2)) AS SMALLDATETIME)
或者:
SET [SentDateTime] = CAST(@var_vaule AS SMALLDATETIME)