Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Syntax error converting the nvarchar value 'INSERT INTO....

Syntax error converting the nvarchar value 'INSERT INTO....

Scheduled Pinned Locked Moved Database
databasehelp
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mahantesh_Hongal
    wrote on last edited by
    #1

    Hi All, I am getting the following error, Msg 245, Level 16, State 1, Line 13 Syntax error converting the nvarchar value 'INSERT INTO [mydb].[dbo].[temp] ( [f1], [f2], [f3], [f4] ) VALUES( 222, 333, 44, ' to a column of data type int. while executing the below code DECLARE @t_f1 as int DECLARE @t_f2 as nvarchar(50) DECLARE @t_f3 as nvarchar(50) DECLARE @t_f4 as nvarchar(50) set @t_f1 = 111 set @t_f2 = 222 set @t_f3e = 333 set @t_f4 = 44 declare @q as nvarchar(500) set @q = 'INSERT INTO [mydb].[dbo].[temp] ( [fld1], [fld2], [fld3], [fld4] ) VALUES( '+@t_f1+', '+@t_f2+', '+@t_f3+', '+@t_f4+' )' exec @q My database table structure is as follows, CREATE TABLE temp ( fld1 int, fld2 nvarchar(50), fld3 nvarchar(20), fld4 nvarchar(20), ) Thanks in advance... ~Mahantesh V H

    U 1 Reply Last reply
    0
    • M Mahantesh_Hongal

      Hi All, I am getting the following error, Msg 245, Level 16, State 1, Line 13 Syntax error converting the nvarchar value 'INSERT INTO [mydb].[dbo].[temp] ( [f1], [f2], [f3], [f4] ) VALUES( 222, 333, 44, ' to a column of data type int. while executing the below code DECLARE @t_f1 as int DECLARE @t_f2 as nvarchar(50) DECLARE @t_f3 as nvarchar(50) DECLARE @t_f4 as nvarchar(50) set @t_f1 = 111 set @t_f2 = 222 set @t_f3e = 333 set @t_f4 = 44 declare @q as nvarchar(500) set @q = 'INSERT INTO [mydb].[dbo].[temp] ( [fld1], [fld2], [fld3], [fld4] ) VALUES( '+@t_f1+', '+@t_f2+', '+@t_f3+', '+@t_f4+' )' exec @q My database table structure is as follows, CREATE TABLE temp ( fld1 int, fld2 nvarchar(50), fld3 nvarchar(20), fld4 nvarchar(20), ) Thanks in advance... ~Mahantesh V H

      U Offline
      U Offline
      under2811
      wrote on last edited by
      #2

      Hi!!!! Execute statement only takes nvarchar,char....datatype only so u change data type like DECLARE @t_f1 as nvarchar(50) DECLARE @t_f2 as nvarchar(50) DECLARE @t_f3 as nvarchar(50) DECLARE @t_f4 as nvarchar(50) Regards ;) Shashank

      M 1 Reply Last reply
      0
      • U under2811

        Hi!!!! Execute statement only takes nvarchar,char....datatype only so u change data type like DECLARE @t_f1 as nvarchar(50) DECLARE @t_f2 as nvarchar(50) DECLARE @t_f3 as nvarchar(50) DECLARE @t_f4 as nvarchar(50) Regards ;) Shashank

        M Offline
        M Offline
        Mahantesh_Hongal
        wrote on last edited by
        #3

        Is there any other alternative means for integer data type? ~Mahantesh V H

        E 1 Reply Last reply
        0
        • M Mahantesh_Hongal

          Is there any other alternative means for integer data type? ~Mahantesh V H

          E Offline
          E Offline
          Edbert P
          wrote on last edited by
          #4

          You probably can use the CONVERT() function to convert the int variable to nvarchar. Try CONVERT(@t_f1 AS NVARCHAR(50)) Ed

          A 1 Reply Last reply
          0
          • E Edbert P

            You probably can use the CONVERT() function to convert the int variable to nvarchar. Try CONVERT(@t_f1 AS NVARCHAR(50)) Ed

            A Offline
            A Offline
            Anonymous
            wrote on last edited by
            #5

            Hi, I want to insert int not char... ~Mahantesh

            E 1 Reply Last reply
            0
            • A Anonymous

              Hi, I want to insert int not char... ~Mahantesh

              E Offline
              E Offline
              Edbert P
              wrote on last edited by
              #6

              Anonymous wrote:

              I want to insert int not char...

              Yes, I know. But from what I see it seems that the SQL is complaining that you are adding the nvarchar 'INSERT INTO [mydb].[dbo].[temp]..' into @t_f1 (which is an integer) when concatenating the @q. The convert is just to make sure your @q can be created by concatenating nvarchars instead of adding it to an int. When you execute it it should insert an integer instead of an nvarchar. Btw, why are you using exec @q anyway? You don't seem to need to create a dynamic SQL statement for that one. Ed

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups