Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Error Conversion failed when converting the varchar value 'SELECT in sql server 2008


Error Conversion failed when converting the varchar value 'SELECT in sql server 2008

Author
Message
smriti.subramanian
smriti.subramanian
SSC Rookie
SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)

Group: General Forum Members
Points: 32 Visits: 107
Hi, I have the following stored proc written using dynamic query. i am appending an integer variable to the dynaic query. but i am getting an issue only with the integer variable.

DECLARE @SQL VARCHAR(4000)
SET @SQL=
'SELECT
[RECORD_TYPE],
[RECORD_VERSION_NUMBER],
[1LINK_BOOKING_REF],
[RESERVATION_NUMBER],
[CUSTOMER_ACCOUNT_NUMBER],
[CUSTOMER_NAME],
[CUSTOMER_ORDER_NUMBER],
[PURCHASE_ORDER_NUMBER],
[COST_CENTRE],
[HIRE_CONTACT],
[HIRE_CONTACT_TELEPHONE_NUMBER],
[LEAD_SOURCE_DESCRIPTION],
[LEAD_SOURCE_CODE],
[RELIEF_VEHICLE_REGISTRATION],
[HIRE_REASON_DESCRIPTION],
[HIRE_REASON_CODE],
[HIRE_REASON_NOTES],
[DRIVER_TITLE],
[DRIVER_FORENAME_OR_INITIAL],
[DRIVER_SURNAME],
[DRIVER_MOBILE_NUMBER],
[DRIVER_FAX_NUMBER],
[DRIVER_TELEPHONE],
[DRIVER_EMAIL],
[DRIVER_REFERENCE],
[ADDITIONAL_DRIVER_1_TITLE],
[ADDITIONAL_DRIVER_1_FORENAME_OR_INITIAL],
[ADDITIONAL_DRIVER_1_SURNAME],
[ADDITIONAL_DRIVER_2_TITLE],
[ADDITIONAL_DRIVER_2_FORENAME_OR_INITIAL],
[ADDITIONAL_DRIVER_2_SURNAME],
[ADDITIONAL_DRIVER_3_TITLE],
[ADDITIONAL_DRIVER_3_FORENAME_OR_INITIAL],
[ADDITIONAL_DRIVER_3_SURNAME],
[ADDITIONAL_DRIVER_4_TITLE],
[ADDITIONAL_DRIVER_4_FORENAME_OR_INITIAL],
[ADDITIONAL_DRIVER_4_SURNAME],
[ADDITIONAL_DRIVER_5_TITLE],
[ADDITIONAL_DRIVER_5_FORENAME_OR_INITIAL],
[ADDITIONAL_DRIVER_5_SURNAME],
[REQUESTED_MODEL_GROUP],
[START_DATE_OR_TIME_OF_HIRE],
[START_BRANCH_CODE],
[START_BRANCH_NAME],
[DELIVERY_ADDRESS_LINE_1],
[DELIVERY_ADDRESS_LINE_2],
[DELIVERY_ADDRESS_LINE_3],
[DELIVERY_ADDRESS_LINE_4],
[DELIVERY_ADDRESS_LINE_5],
[DELIVERY_ADDRESS_POSTCODE],
[REQUEST_OPTION_NOTES],
[ADDITIONAL_EMAIL_OR_FAX_NOTES],
[END_DATE_OR_TIME_OF_HIRE],
[END_BRANCH_CODE],
[END_BRANCH_NAME],
[COLLECTION_ADDRESS_LINE_1],
[COLLECTION_ADDRESS_LINE_2],
[COLLECTION_ADDRESS_LINE_3],
[COLLECTION_ADDRESS_LINE_4],
[COLLECTION_ADDRESS_LINE_5],
[COLLECTION_ADDRESS_POSTCODE],
[COLLECTION_KEY_LOCATION],
[INSURANCE_TYPE],
[FLIGHT_NUMBER],
[MEET_AND_GREET],
[CSI_EMAIL_ADDRESS],
[CHILD_SEATS],
[SATELLITE_NAVIGATION],
[HAND_CONTROLS],
[DAYS_ABROAD],
[LICENCE_AVAILABLE],
[YEARS_LICENCE_HELD],
[OFFENCE_CODES],
[FUEL_DEPOSIT_TYPE],
[EQUIFAX_CHECK],
[DRIVER_ADDRESS_LINE_1],
[DRIVER_ADDRESS_LINE_2],
[DRIVER_ADDRESS_LINE_3],
[DRIVER_ADDRESS_LINE_4],
[DRIVER_ADDRESS_LINE_5],
[HIRE_SUPPLIER],
[TARIFF_ACCOUNT_REFERENCE],
[SUPPLIERS_ACCOUNT_REFERENCE],
[BOOKING_STATUS],
[ORIGINAL_1LINK_BOOKING_REF],
[AIRPORT_PICKUP],
[BOOKING_TYPE_INDICATOR],
[EMPLOYEE_NUMBER],
[STORE_NUMBER],
[CANCELLATION_REFERENCE],
[ADDITIONAL_BOOKING_REFERENCE],
[TARIFF_TYPE_CODE],
[DATE_DAYS_ABROAD_FROM],
[DATE_DAYS_ABROAD_TO],
[DAYS_ABROAD_COUNTRIES],
[BOOKING_TIME_TYPE],
[REQUEST_TYPE],
[QUOTE_TYPE],
[QUOTE_NUMBER],
[RESERVED1],
[RESERVED2],
[RESERVED3],
[RESERVED4],
[RESERVED5],
[RESERVED6],
[DRIVER_PICKUP_SERVICE_CHOSEN],
[DRIVER_DROP_OFF_SERVICE_CHOSEN],
[DATE_CREATED],
[STATUS_ID],
[ERROR_CODE]
FROM [dbo].[WIP_RESERVATION_STATUS] RS WHERE(1=1)'

-- If 1link booking reference number is not null
IF @p_1LINK_BOOKING_REF IS NOT NULL
SET @SQL = @SQL + 'AND RS.1LINK_BOOKING_REF = ''' +@p_1Link_Booking_Ref + ''''
-- If customer account number is not null
IF @p_Customer_Account_Number IS NOT NULL
SET @SQL = @SQL + ' AND RS.CUSTOMER_ACCOUNT_NUMBER = ''' + @p_Customer_Account_Number+ ''''
-- If customer name is not null
IF @p_Customer_Name IS NOT NULL
SET @SQL =@SQL + ' AND RS.CUSTOMER_NAME LIKE ''' + @p_Customer_Name + '%'''
-- If Error is not null
IF @p_Error IS NOT NULL
SET @SQL = @SQL + 'AND RS.ERROR_CODE = ' + @p_Error+ ''
INSERT INTO @RecipientDetails
EXEC (@SQL)

In the table i have declated error as int. When i execute i get an error msg
Error Conversion failed when converting the varchar value 'SELECT
[RECORD_TYPE],
[RECORD_VERSION_NUMBER],
[1LINK_BOOKING_REF],
[RESERVATION_NUMBER],
[CUSTOMER_ACCOUNT_NUMBER],
[CUSTOMER_NAME],
Please help me.
anthony.green
anthony.green
SSCertifiable
SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)

Group: General Forum Members
Points: 6091 Visits: 6069
Either change the error code variable to a varchar or convert the variable when appending it to the string using cast or convert.


SET @SQL = @SQL + 'AND RS.ERROR_CODE = ' + CONVERT(VARCHAR,@p_Error)+ ''

As SQL is trying to convert the whole string to a number which is cannot do, so you need to make the number a string.



Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
When a question, really isn't a question - Jeff Smith
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


Jason-299789
Jason-299789
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1134 Visits: 3229
so is @p_error an integer?

In which case you will need to change the


IF @p_Error IS NOT NULL
SET @SQL = @SQL + 'AND RS.ERROR_CODE = ' + @p_Error+ ''
INSERT INTO @RecipientDetails
EXEC (@SQL)



to


IF @p_Error IS NOT NULL
SET @SQL = @SQL + 'AND RS.ERROR_CODE = ' + Cast(varchar(20),@p_error)+ ''
INSERT INTO @RecipientDetails
EXEC (@SQL)



and that should work.

_________________________________________________________________________
SSC Guide to Posting and Best Practices
anthony.green
anthony.green
SSCertifiable
SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)

Group: General Forum Members
Points: 6091 Visits: 6069
Jason-299789 (1/2/2013)
so is @p_error an integer?

In which case you will need to change the


IF @p_Error IS NOT NULL
SET @SQL = @SQL + 'AND RS.ERROR_CODE = ' + @p_Error+ ''
INSERT INTO @RecipientDetails
EXEC (@SQL)



to


IF @p_Error IS NOT NULL
SET @SQL = @SQL + 'AND RS.ERROR_CODE = ' + Cast(varchar(20),@p_error)+ ''
INSERT INTO @RecipientDetails
EXEC (@SQL)



and that should work.


Should be

Cast(@p_error, varchar(20))

Convert the data type is first, cast the data type is last.



Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
When a question, really isn't a question - Jeff Smith
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


smriti.subramanian
smriti.subramanian
SSC Rookie
SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)

Group: General Forum Members
Points: 32 Visits: 107
SET @SQL = @SQL + 'AND RS.ERROR_CODE = ' + CONVERT(VARCHAR,@p_Error)+ ''

thanks :-) it worked
Jason-299789
Jason-299789
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1134 Visits: 3229
anthony.green (1/2/2013)
Jason-299789 (1/2/2013)
so is @p_error an integer?

In which case you will need to change the


IF @p_Error IS NOT NULL
SET @SQL = @SQL + 'AND RS.ERROR_CODE = ' + @p_Error+ ''
INSERT INTO @RecipientDetails
EXEC (@SQL)



to


IF @p_Error IS NOT NULL
SET @SQL = @SQL + 'AND RS.ERROR_CODE = ' + Cast(varchar(20),@p_error)+ ''
INSERT INTO @RecipientDetails
EXEC (@SQL)



and that should work.


Should be

Cast(@p_error, varchar(20))

Convert the data type is first, cast the data type is last.


Doh!!!!, thanks anthony, its my fist day back after after 14 days holiday, and the brains still warming its cache.

_________________________________________________________________________
SSC Guide to Posting and Best Practices
anthony.green
anthony.green
SSCertifiable
SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)

Group: General Forum Members
Points: 6091 Visits: 6069
Jason-299789 (1/2/2013)
anthony.green (1/2/2013)
Jason-299789 (1/2/2013)
so is @p_error an integer?

In which case you will need to change the


IF @p_Error IS NOT NULL
SET @SQL = @SQL + 'AND RS.ERROR_CODE = ' + @p_Error+ ''
INSERT INTO @RecipientDetails
EXEC (@SQL)



to


IF @p_Error IS NOT NULL
SET @SQL = @SQL + 'AND RS.ERROR_CODE = ' + Cast(varchar(20),@p_error)+ ''
INSERT INTO @RecipientDetails
EXEC (@SQL)



and that should work.


Should be

Cast(@p_error, varchar(20))

Convert the data type is first, cast the data type is last.


Doh!!!!, thanks anthony, its my fist day back after after 14 days holiday, and the brains still warming its cache.


Same here, hope you had a good holiday.



Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
When a question, really isn't a question - Jeff Smith
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


Jason-299789
Jason-299789
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1134 Visits: 3229
I did thanks, hope you had a good christmas and new year as well.

_________________________________________________________________________
SSC Guide to Posting and Best Practices
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search