Failed to convert sqlwhereidin to data type int

Source: Internet
Author: User
If the SQL Condition Statement whereidin (@ parameter) is executed improperly, the following problems may occur:

If the SQL condition statement where id in (@ parameter) is executed improperly, the following problems may occur:

If the SQL condition statement where id in (@ parameter) is executed improperly, the following problems may occur:
See the following stored procedure:

Alter proc web_gettwgoldgameservergoldsell
@ ID int
As
Declare @ twgoldsellID nvarchar (1000)
Select @ twgoldsellID = twgoldsellID from twgoldgameserver where ID = @ ID
Set @ twgoldsellID = replace (@ twgoldsellID, '| ',',')
Set @ twgoldsellID = left (@ twgoldsellID, len (@ twgoldsellID)-1)
Select * from twgoldsell where ID in (@ twgoldsellID)

It seems that there is no problem, but an error is reported during execution:

Message 245, level 16, state 1, 1st rows
An error occurred while converting the varchar value '2017, 813,116, 1227,1232 'to the Data Type int.

In fact, when this statement is executed,
Select * from twgoldsell where ID in (@ twgoldsellID)
The executed statement is: select * from twgoldsell where ID in ('2017 813,116, 1219, 1227,1232 ')
An error occurs because @ twgoldsellID is a string and is passed as a parameter.

Solution:
Select * from twgoldsell where ID in (@ twgoldsellID)
Changed:
Exec ('select * from twgoldsell where ID in ('+ @ twgoldsellID + ')')

Remember: you must add exec to execute the function. Otherwise, the following error will be reported:
Message 245, level 16, status 1, process web_gettwgoldgameservergoldsell, row 8th
Failed to convert varchar value '+ @ twgoldsellID +' to data type int.

,

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.