MsSql server:running 存储过程
MsSql server:running stored procedure
我有一个table
CREATE TABLE [dbo].[DealerInfo](
[DealerName] [nvarchar](100) NULL,
[Address] [nvarchar](100) NULL,
[City] [nvarchar](100) NULL,
[County] [nvarchar](100) NULL,
[Fax] [nvarchar](50) NULL,
[CompanyWebSite] [nvarchar](max) NULL,
[EmailAddress] [nvarchar](100) NULL,
[Currency] [nvarchar](20) NULL,
[LicenceID] [int] NULL,
[TaxRegistration] [int] NULL,
[Phone] [nvarchar](50) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
还有一个问题,当我尝试插入一个货币值时,它报告说它需要一个整数值
我使用存储过程插入值
ALTER PROCEDURE [dbo].[DealerInformation]
-- Add the parameters for the stored procedure here
@DealerName nvarchar(100),
@Address nvarchar(100),
@City nvarchar (100),
@County nvarchar (100),
@Phone nvarchar (100),
@Fax nvarchar(50),
@CompanyWebSite nvarchar (100),
@EmailAddress nvarchar(Max),
@Currency nvarchar(20),
@LicenceID int,
@TaxRegistration int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
Insert into DealerInfo values (@DealerName, @Address, @City, @County, @Phone, @Fax, @CompanyWebSite,@EmailAddress,@Currency,@LicenceID,@TaxRegistration)
货币应该是一个字符串值,如 USD、CAD、EUR 等
当我执行存储过程时,这是我得到的错误
Msg 245, Level 16, State 1, Procedure DealerInformation, Line 26 [Batch Start Line 2]
Conversion failed when converting the nvarchar value 'USD' to data type int.
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=CarDealership;Integrated Security=True") )
{
conn.Open();
SqlCommand cmd = new SqlCommand("DealerInformation", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("DealerName", dealership.DealershipName);
cmd.Parameters.AddWithValue("Address", dealership.Address);
cmd.Parameters.AddWithValue("City", dealership.City);
cmd.Parameters.AddWithValue("County", dealership.Country);
cmd.Parameters.AddWithValue("Phone", dealership.Telephone);
cmd.Parameters.AddWithValue("Fax", dealership.Fax);
cmd.Parameters.AddWithValue("CompanyWebSite", dealership.CompanyWebSite);
cmd.Parameters.AddWithValue("EmailAddress", dealership.Email);
cmd.Parameters.AddWithValue("Currency", dealership.Currency); //The currency is string type
cmd.Parameters.AddWithValue("LicenceID", dealership.LicenceID);
cmd.Parameters.AddWithValue("TaxRegistration", dealership.TaxRegistration);
int RowsAffected = cmd.ExecuteNonQuery();
return RowsAffected;
}
您正在使用 INSERT INTO 语句,但未指定字段名称。
因此,您的参数应该按照 table
中字段名称的相同顺序列出
现在,数据库引擎正在使用这些参数设置字段
[DealerName] = @DealerName,
[Address] = @Address
[City] = @City
[County] = @County
[Fax] = @Phone
[CompanyWebSite] = @Fax
[EmailAddress] = @CompanyWebSite
[Currency] = @EmailAddress,
[LicenceID] = @Currency,
[TaxRegistration] = @LicenceID
[Phone] = @TaxRegistration
如您所见,您的 LicenceID 字段(一个 int)接收 @Currency 参数的值(一个 nvarchar)。
这是错误的原因,解决方法是始终在 INSERT INTO 语句中指定字段名称,并以正确的顺序列出参数以设置关联的字段。
Insert into DealerInfo
(DealerName, Address, City, County, Fax, CompanyWebSite,
EmailAddress,Currency,LicenceID,TaxRegistration,Phone)
values (@DealerName, @Address, @City, @County, @Fax, @CompanyWebSite,
@EmailAddress,@Currency,@LicenceID,@TaxRegistration, @Phone)
对我来说,您的 table 定义和存储过程代码似乎不同步。那就是你的 table 定义列数据类型 currency
实际上是 int
,但不知何故你在编辑器中将它更改为 nvarchar
。
要解决此问题,请右键单击您的 table 并检查您 table 的定义(而不是您在编辑器 window 中的代码),您将得到答案.
希望对你有所帮助。
谢谢
我有一个table
CREATE TABLE [dbo].[DealerInfo](
[DealerName] [nvarchar](100) NULL,
[Address] [nvarchar](100) NULL,
[City] [nvarchar](100) NULL,
[County] [nvarchar](100) NULL,
[Fax] [nvarchar](50) NULL,
[CompanyWebSite] [nvarchar](max) NULL,
[EmailAddress] [nvarchar](100) NULL,
[Currency] [nvarchar](20) NULL,
[LicenceID] [int] NULL,
[TaxRegistration] [int] NULL,
[Phone] [nvarchar](50) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
还有一个问题,当我尝试插入一个货币值时,它报告说它需要一个整数值
我使用存储过程插入值
ALTER PROCEDURE [dbo].[DealerInformation]
-- Add the parameters for the stored procedure here
@DealerName nvarchar(100),
@Address nvarchar(100),
@City nvarchar (100),
@County nvarchar (100),
@Phone nvarchar (100),
@Fax nvarchar(50),
@CompanyWebSite nvarchar (100),
@EmailAddress nvarchar(Max),
@Currency nvarchar(20),
@LicenceID int,
@TaxRegistration int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
Insert into DealerInfo values (@DealerName, @Address, @City, @County, @Phone, @Fax, @CompanyWebSite,@EmailAddress,@Currency,@LicenceID,@TaxRegistration)
货币应该是一个字符串值,如 USD、CAD、EUR 等
当我执行存储过程时,这是我得到的错误
Msg 245, Level 16, State 1, Procedure DealerInformation, Line 26 [Batch Start Line 2]
Conversion failed when converting the nvarchar value 'USD' to data type int.
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=CarDealership;Integrated Security=True") )
{
conn.Open();
SqlCommand cmd = new SqlCommand("DealerInformation", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("DealerName", dealership.DealershipName);
cmd.Parameters.AddWithValue("Address", dealership.Address);
cmd.Parameters.AddWithValue("City", dealership.City);
cmd.Parameters.AddWithValue("County", dealership.Country);
cmd.Parameters.AddWithValue("Phone", dealership.Telephone);
cmd.Parameters.AddWithValue("Fax", dealership.Fax);
cmd.Parameters.AddWithValue("CompanyWebSite", dealership.CompanyWebSite);
cmd.Parameters.AddWithValue("EmailAddress", dealership.Email);
cmd.Parameters.AddWithValue("Currency", dealership.Currency); //The currency is string type
cmd.Parameters.AddWithValue("LicenceID", dealership.LicenceID);
cmd.Parameters.AddWithValue("TaxRegistration", dealership.TaxRegistration);
int RowsAffected = cmd.ExecuteNonQuery();
return RowsAffected;
}
您正在使用 INSERT INTO 语句,但未指定字段名称。
因此,您的参数应该按照 table
现在,数据库引擎正在使用这些参数设置字段
[DealerName] = @DealerName,
[Address] = @Address
[City] = @City
[County] = @County
[Fax] = @Phone
[CompanyWebSite] = @Fax
[EmailAddress] = @CompanyWebSite
[Currency] = @EmailAddress,
[LicenceID] = @Currency,
[TaxRegistration] = @LicenceID
[Phone] = @TaxRegistration
如您所见,您的 LicenceID 字段(一个 int)接收 @Currency 参数的值(一个 nvarchar)。
这是错误的原因,解决方法是始终在 INSERT INTO 语句中指定字段名称,并以正确的顺序列出参数以设置关联的字段。
Insert into DealerInfo
(DealerName, Address, City, County, Fax, CompanyWebSite,
EmailAddress,Currency,LicenceID,TaxRegistration,Phone)
values (@DealerName, @Address, @City, @County, @Fax, @CompanyWebSite,
@EmailAddress,@Currency,@LicenceID,@TaxRegistration, @Phone)
对我来说,您的 table 定义和存储过程代码似乎不同步。那就是你的 table 定义列数据类型 currency
实际上是 int
,但不知何故你在编辑器中将它更改为 nvarchar
。
要解决此问题,请右键单击您的 table 并检查您 table 的定义(而不是您在编辑器 window 中的代码),您将得到答案.
希望对你有所帮助。 谢谢