Failed to convert varchar value to data type int

Source: Internet
Author: User

This is the error that may occur in the stored procedure.

Like what

Use [School]
go
alter Procedure [dbo].[ S_get_student]
(
	@sex int	--gender sex:1 (male) 2 (female)
)
as
begin
	declare @sql varchar (max)-- Can contain many many characters
	set @sql = ' SELECT * FROM dbo. Class where sex ' + @sex
	exec (@sql)
end

--[s_get_student] 1

This situation prompts

Message 245, Level 16, State 1, Process s_get_student, line 8th
In the varchar value ' SELECT * FROM dbo. Class where sex ' conversion to data type int failed.
This is because your @sql is varchar value, and your @sex is shaping data

So you're going to convert @sex to the data type, and replace it with varchar.

Set @sql = ' SELECT * FROM dbo. Class where sex= ' +convert (varchar, @sex)

And, of course, it doesn't rule out anything else. You make the type of the concatenation statement the same for your variable convert (varchar,@x) because you can't add a date or an integer to a string

Click to open the link


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.