Syntax error in insert statement

  • INSERT dbo.xdata (RcNBR ,

    Distance ,

    MT ,

    EMEASURE ,

    RC ,

    YEAR )

    EXEC sp_ExecuteSQL @sql_str

    Can anybody tell me why I am getting syntax error? Thanks

  • Ajdba (8/6/2012)


    INSERT dbo.xdata (RcNBR ,

    Distance ,

    MT ,

    EMEASURE ,

    RC ,

    YEAR )

    EXEC sp_ExecuteSQL @sql_str

    Can anybody tell me why I am getting syntax error? Thanks

    Well there are a couple things going on here. You are executing a sql string. That is in it's own batch. That means you can't do this quite like what you have going on. You could make the insert part of your dynamic string and execute the whole thing. Do you really need to use dynamic sql here? Dynamic sql can be very dangerous if you are not careful.

    There just isn't enough detail provided to give you a solution.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Ajdba (8/6/2012)


    INSERT dbo.xdata (RcNBR ,

    Distance ,

    MT ,

    EMEASURE ,

    RC ,

    YEAR )

    EXEC sp_ExecuteSQL @sql_str

    Can anybody tell me why I am getting syntax error? Thanks

    Here is the problem, you haven't provided us with the actual error message you are receiving. A little hard to say what is wrong when we can't see the error.

  • Sean Lange (8/6/2012)


    Ajdba (8/6/2012)


    INSERT dbo.xdata (RcNBR ,

    Distance ,

    MT ,

    EMEASURE ,

    RC ,

    YEAR )

    EXEC sp_ExecuteSQL @sql_str

    Can anybody tell me why I am getting syntax error? Thanks

    Well there are a couple things going on here. You are executing a sql string. That is in it's own batch. That means you can't do this quite like what you have going on. You could make the insert part of your dynamic string and execute the whole thing. Do you really need to use dynamic sql here? Dynamic sql can be very dangerous if you are not careful.

    There just isn't enough detail provided to give you a solution.

    Actually, Sean, the EXEC can be the source of the data for the insert. It is one way of persisting data from the execution of a stored proc or dynamic sql statement.

  • Thanks guys; its working fine now

  • Ajdba (8/6/2012)


    Thanks guys; its working fine now

    Okay, so share how you fixed it. Others may be interested, plus it is good forum etiquette.

  • Lynn Pettis (8/6/2012)


    Sean Lange (8/6/2012)


    Ajdba (8/6/2012)


    INSERT dbo.xdata (RcNBR ,

    Distance ,

    MT ,

    EMEASURE ,

    RC ,

    YEAR )

    EXEC sp_ExecuteSQL @sql_str

    Can anybody tell me why I am getting syntax error? Thanks

    Well there are a couple things going on here. You are executing a sql string. That is in it's own batch. That means you can't do this quite like what you have going on. You could make the insert part of your dynamic string and execute the whole thing. Do you really need to use dynamic sql here? Dynamic sql can be very dangerous if you are not careful.

    There just isn't enough detail provided to give you a solution.

    Actually, Sean, the EXEC can be the source of the data for the insert. It is one way of persisting data from the execution of a stored proc or dynamic sql statement.

    I guess I would just prefer to not use dynamic sql as the source of my insert and as such have not done so. Thanks for the correction. 🙂

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Sean Lange (8/6/2012)


    Lynn Pettis (8/6/2012)


    Sean Lange (8/6/2012)


    Ajdba (8/6/2012)


    INSERT dbo.xdata (RcNBR ,

    Distance ,

    MT ,

    EMEASURE ,

    RC ,

    YEAR )

    EXEC sp_ExecuteSQL @sql_str

    Can anybody tell me why I am getting syntax error? Thanks

    Well there are a couple things going on here. You are executing a sql string. That is in it's own batch. That means you can't do this quite like what you have going on. You could make the insert part of your dynamic string and execute the whole thing. Do you really need to use dynamic sql here? Dynamic sql can be very dangerous if you are not careful.

    There just isn't enough detail provided to give you a solution.

    Actually, Sean, the EXEC can be the source of the data for the insert. It is one way of persisting data from the execution of a stored proc or dynamic sql statement.

    I guess I would just prefer to not use dynamic sql as the source of my insert and as such have not done so. Thanks for the correction. 🙂

    Understand. I have only done it using dynamic sql a couple of times, and I had full control over the code. Usually, I have needed to capture the output of a stored procedure for further processing.

  • Lynn Pettis (8/6/2012)


    Sean Lange (8/6/2012)


    Lynn Pettis (8/6/2012)


    Sean Lange (8/6/2012)


    Ajdba (8/6/2012)


    INSERT dbo.xdata (RcNBR ,

    Distance ,

    MT ,

    EMEASURE ,

    RC ,

    YEAR )

    EXEC sp_ExecuteSQL @sql_str

    Can anybody tell me why I am getting syntax error? Thanks

    Well there are a couple things going on here. You are executing a sql string. That is in it's own batch. That means you can't do this quite like what you have going on. You could make the insert part of your dynamic string and execute the whole thing. Do you really need to use dynamic sql here? Dynamic sql can be very dangerous if you are not careful.

    There just isn't enough detail provided to give you a solution.

    Actually, Sean, the EXEC can be the source of the data for the insert. It is one way of persisting data from the execution of a stored proc or dynamic sql statement.

    I guess I would just prefer to not use dynamic sql as the source of my insert and as such have not done so. Thanks for the correction. 🙂

    Understand. I have only done it using dynamic sql a couple of times, and I had full control over the code. Usually, I have needed to capture the output of a stored procedure for further processing.

    Yeah I have done that type of thing using a proc but never dynamic sql. Nice to learn something from somebody else's thread. 😉

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply