我执行存储过程时为什么会出现隐式转换错误? [英] Why I Am Getting Implicit Conversion Error When I Executing The Stored Procedure?

查看:67
本文介绍了我执行存储过程时为什么会出现隐式转换错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建过程Sp_InsertStudentDetails

@Adno int,@ First_name varchar(30),@ Last_name varchar(30),@ Gender varchar(10),@ Dateof_birth Date,@ Dateof_joining Date,@ Father_name varchar (30),@ Mother_name varchar(30),@ Occupation varchar(20),@ Branch varchar(10),@ Parent_mobilenumber bigint,@ Religion varchar(10),@ Caste varchar(20),@ House_number varchar(10), @Village_name varchar(20),@ District_name varchar(20),@ State_name varchar(20),@ Pincode int,@ Academic_year varchar(10),@ Image_file varbinary(MAX),@ Section Varchar(20)

as

begin

插入Student_Registration(Admission_number,First_name,Last_name,Gender,Dateof_birth,

Dateof_joining,Father_name,Mother_name,Occupation ,分支,宗教,种姓,House_number,

village_name,District_name,State_name,pincode,Academic_year,image_file,Parent_mobilenumber,

Section)值(@Adno,@ First_name,@ Last_name,@ Gender,@ Dateof_birth,@ Dateof_joining,

@ Father_name,@ Mother_name,@ Occupati on,@ Branch,@ Parent_mobilenumber,@ Religion,@ Caste,

@ House_number,@ Village_name,@ District_name,@ State_name,@ Pincode,@ Academic_year,

@Image_file ,@ Section)

结束





当我执行上面的查询时,我得到下面的错误但是当数据库被意外更改时,它已执行而没有任何错误。





< pre lang =" sql"> Msg 257,Level 16,State 3,Procedure Sp_InsertStudentDetails,第5行

不允许从数据类型varchar到varbinary(max)的隐式转换。使用CONVERT函数运行此查询。< / pre>

Create procedure Sp_InsertStudentDetails
@Adno int,@First_name varchar(30),@Last_name varchar(30),@Gender varchar(10),@Dateof_birth Date,@Dateof_joining Date,@Father_name varchar(30),@Mother_name varchar(30),@Occupation varchar(20),@Branch varchar(10),@Parent_mobilenumber bigint,@Religion varchar(10),@Caste varchar(20),@House_number varchar(10),@Village_name varchar(20),@District_name varchar(20),@State_name varchar(20),@Pincode int,@Academic_year varchar(10),@Image_file varbinary(MAX),@Section Varchar(20)
as
begin
Insert into Student_Registration(Admission_number,First_name,Last_name,Gender,Dateof_birth,
Dateof_joining,Father_name,Mother_name,Occupation,Branch,Religion,caste,House_number,
village_name,District_name,State_name,pincode,Academic_year,image_file,Parent_mobilenumber,
Section)values(@Adno,@First_name,@Last_name,@Gender,@Dateof_birth,@Dateof_joining,
@Father_name,@Mother_name,@Occupation,@Branch,@Parent_mobilenumber,@Religion,@Caste,
@House_number,@Village_name,@District_name,@State_name,@Pincode,@Academic_year,
@Image_file,@Section)
End


When i execute the query above i'am getting the error below but when accidentally the database got changed it has executed without any errors.


<pre lang="sql">Msg 257, Level 16, State 3, Procedure Sp_InsertStudentDetails, Line 5
Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query.</pre>

推荐答案

当数据库意外更改时非常重要。



不要写这样的代码:总是指定要插入数据的列 - 如果不这样做,那么SQL必须进入参数order - 所以如果DB被更改,并且列顺序不再与参数顺序匹配,它会尝试在错误的列中插入错误的数据,并且您会收到这样的错误 - 如果您幸运的话。如果你运气不好,你就不会收到错误,但是它会起作用,你会得到一个充满混合数据列的数据库:名字栏中的城市名称,账单总计列中的发票号码,那种东西。令人讨厌......



而不是像这样编写插页:

"when accidentally the database got changed" is pretty significant.

Don't write code like that: always specify the columns into which you want to insert your data - if you don't, then SQL has to go in parameter order - so if the DB gets changed, and the column order no longer matches the parameter order, it tries to insert the wrong data in the wrong column, and you get an error like this - if you are lucky. If you are unlucky, you don't get an error, but instead it works, and you get a database full of mixed up data columns: City name in the first name column, invoice number in the bill total column, that kind of thing. Nasty...

Instead of writing inserts like this:
INSERT INTO MyTable VALUES (1, 2, 'Joe Smith')

改为使用命名列:

Use named columns instead:

INSERT INTO MyTable (Id, Age, UserName) VALUES (1, 2, 'Joe Smith')



并且您的问题很可能会消失。


And there is a good chance your problem will disappear.


由于列序列不匹配,您收到错误。



只需改变查询中提到的列名就像



You are getting error because of column sequence got mismatch.

simply change query with column name mention in that like

Insert into Student_Registration 
(Adno,First_name,LastName,Gender,(add remaining columns))
values(@Adno,@First_name,@Last_name,@Gender,@Dateof_birth,@Dateof_joining,@Father_name,
@Mother_name,@Occupation,@Branch,@Parent_mobilenumber,@Religion,@Caste,
@House_number,@Village_name,@District_name,@State_name,@Pincode,@Academic_year
,@Image_file,@Section)


请发布表结构并执行查询数据。
Please post your table structure and execute query data.


这篇关于我执行存储过程时为什么会出现隐式转换错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆