SQL 将数据类型 nvarchar 转换为 int 时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9953940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 15:06:48  来源:igfitidea点击:

Error converting datatype nvarchar to int

sqlsql-serverdatabasetsql

提问by chandu

I am getting the error

我收到错误

Error converting datatype nvarchar to int

将数据类型 nvarchar 转换为 int 时出错

Code:

代码:

ALTER procedure [dbo].[sp_rcdoc]
  @regno int,
  @appname varchar(50),
  @DOI datetime,
  @COV varchar(50),
  @validtill date,
  @imgloc varchar(500),
  @ImagNo char(20),
  @Purposecode varchar(50),
  @FLAG varchar(3)
AS BEGIN
   IF NOT EXISTS(SELECT regno FROM tblRCDocuments WHERE regno = @regno)
   BEGIN
       INSERT INTO tblRCDocuments(regno, appname, DOI, COV, validtill, imgloc, ImagNo, Purposecode, FLAG) 
       VALUES(@regno, @appname, @DOI, @COV, @validtill, @imgloc, @ImagNo, @Purposecode, @FLAG)
   END

回答by Darren

Looks like regno is a nvarchar data type in your table and you have passed an int via your your procedure, either use a cast and convert @regno to an nvarchar or change the regno data type to an integer in the table.

看起来 regno 是您表中的 nvarchar 数据类型,并且您已经通过您的过程传递了一个 int,或者使用强制转换并将 @regno 转换为 nvarchar 或将 regno 数据类型更改为表中的整数。

DECLARE @regnocast NVARCHAR(15)

SET @regnocast = CAST(@regno AS NVARCHAR)

Then in your SELECT, INSERT and WHERE clauses use @regnocast rather than @regno

然后在您的 SELECT、INSERT 和 WHERE 子句中使用 @regnocast 而不是 @regno