Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert statement fails on SQL Server 2005 but work well on 2008 R2 or later version

Tags:

sql-server

I have the following SQL insert statement inside the loop which iterate through a cursor:

SELECT @q_sql = 'INSERT INTO SYS_PARAMETER(parameter_uno, parameter_key, description, parameter_value, comments, created_date, created_user_id, created_user_name, last_modified_user, last_modified_date, module_uno, data_type)
                 VALUES ('+@sysparam_uno + ','''+@q_parameter_key+''','''','''+b.pairvalue+''','''',
         getdate(),''setup'',''setup'',''setup'',getDate(),'''+@q_module_uno+''','''')'
         from UTIL_pairkeys a
         INNER JOIN UTIL_pairvalues b on a.pairkeyuno = b.pairkeyuno
         and b.languno = 1
         where a.pairkey=@q_parameter_key

         EXEC sp_executesql @q_sql

Due to value coming to b.pairvalue parameter having a single quote, insert statement fails on SQL Server 2005, but work well on SQL Server 2008R2 and later versions. Any knows reason for this? I know that insert statement fails once parameter value has single quote in between varchar columns. But this something strange here.

Sample insert statement as follows;

INSERT INTO SYS_PARAMETER(parameter_uno,parameter_key,description,parameter_value,comments,created_date,created_user_id,created_user_name,last_modified_user,last_modified_date,module_uno,data_type)
values (269,'application.fs.company','','St John's Foods','',getdate(),'setup','setup','setup',getDate(),'1','')
like image 712
nwGCham Avatar asked Nov 21 '25 21:11

nwGCham


1 Answers

If it is a problem of single quote only than you can replace it by two single quotes like this:

replace( b.pairvalue ,'''','''''')
like image 80
Sachin Avatar answered Nov 23 '25 15:11

Sachin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!