why incorrect syntax near ',' when inserting multiple rows at a time in a table?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • santhanalakshmi
    New Member
    • May 2009
    • 147

    why incorrect syntax near ',' when inserting multiple rows at a time in a table?

    Hi,

    I am working on SQL 2008 database. In Micosoft SQL server Management studio, i am trying to insert multiple records at a time in a table, using this query

    Code:
    [B] insert into test1 values(6,'san6',1),( 7,'san7',1);[/B]
    Error:
    Msg 170, Level 15, State 1, Line 1
    Line 1: Incorrect syntax near ','.


    I think so the syntax was correct. But i dont know why am facing this error. The same problem in SQL Server 2000 also.I am working on my server database.

    Please help me out. Thanks in advance.
    Last edited by Niheel; Oct 6 '10, 09:09 AM.
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    You can use a SELECT even with fixed value...Try something like:

    Code:
    insert into test1 
    select 6,'san6',1
    union all
    select 7,'san7',1
    Happy Coding!!!

    ~~ CK

    Comment

    • santhanalakshmi
      New Member
      • May 2009
      • 147

      #3
      Hi,

      The code is working finely, which you told to try me. This also very helpful for me. Thanks. Can you tell me ? why my code is not working ?

      Comment

      • gpl
        New Member
        • Jul 2007
        • 152

        #4
        Very simply, when inserting literal values, you may only insert 1 row at a time.

        As the language allows you insert the result of a query, you can use a union query to generate a result set containing multiple rows of literals

        Comment

        • almaz
          Recognized Expert New Member
          • Dec 2006
          • 168

          #5
          santhanalakshmi , your original script is absolutely correct for SQL Server 2008, but not for SQL Server 2000.

          Inserting multiple rows using "insert...value s" command is one of new features of SQL Server 2008

          Comment

          • santhanalakshmi
            New Member
            • May 2009
            • 147

            #6
            But my original script its not running..Facing the following errors:

            Error:
            Msg 170, Level 15, State 1, Line 1
            Line 1: Incorrect syntax near ','.

            Comment

            • almaz
              Recognized Expert New Member
              • Dec 2006
              • 168

              #7
              Do you connect to SQL Server 2008?

              To verify the version of SQL Server you are connected to run the following query:

              Code:
              print @@version
              To verify that your database have a SQL2008 compatibility level, check whether the following script returns "100" or not.

              Code:
              select compatibility_level from sys.databases where database_id=db_id()

              Comment

              • santhanalakshmi
                New Member
                • May 2009
                • 147

                #8
                hi,
                Thanks for your help ?

                when i run this query ?

                Code:
                [B]print @@version [/B]
                output :

                Microsoft SQL Server 2000 - 8.00.760 (Intel X86)
                Dec 17 2002 14:22:05
                Copyright (c) 1988-2003 Microsoft Corporation
                Enterprise Edition on Windows NT 5.2 (Build 3790: Service Pack 2)


                and the second query

                Code:
                [B]use master
                select cmptlevel from sysdatabases where dbid=db_id()[/B]
                output :
                80

                In my system, i installed SQL 2000 plus SQL 2008 ? How can i change my version from SQL 2000 to SQL 2008 version.

                Comment

                • almaz
                  Recognized Expert New Member
                  • Dec 2006
                  • 168

                  #9
                  It means that you are connecting to SQL Server 2000, not SQL Server 2008.

                  SQL Server 2000 does not allow you to add multiple rows using "insert...value s" command, so you should use approach suggested by ck9663

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32653

                    #10
                    Well caught Almaz. I thought that was available in 2008 (I knew it wasn't in 2005 because I tried it and it failed ;-( ), but I wasn't sure if an outer level of parentheses was required (INSERT Examples (Transact-SQL) tells me it isn't). The OP stated it was SQL 2008 right from the start, which threw me.

                    Comment

                    • santhanalakshmi
                      New Member
                      • May 2009
                      • 147

                      #11
                      Dear sir,

                      You explained me a lot. Thanks for your response. Did you having own blogs ? In future, i need of your help. How can i contact you ? thanks

                      Comment

                      • NeoPa
                        Recognized Expert Moderator MVP
                        • Oct 2006
                        • 32653

                        #12
                        Originally posted by SanthanaLakshmi
                        SanthanaLakshmi :
                        In future, i need of your help. How can i contact you ?
                        I'm not sure who this is addressed to, but the only way you can request help on here is to post questions in the forums.

                        Any member is welcome to invite you to make alternative contact with them, but otherwise we have strict rules forbidding members contacting other members directly for technical help.

                        Comment

                        • kissa49
                          New Member
                          • Aug 2016
                          • 1

                          #13
                          Thank you!

                          ALMAZ, This just helped me a great deal, thank you very much!!!

                          Comment

                          • almaz
                            Recognized Expert New Member
                            • Dec 2006
                            • 168

                            #14
                            My pleasure. It's great to know that even after six years these posts are helpful for someone :).

                            Comment

                            Working...