incorrect syntax in datetime

  • Hello

    I write code below and I use getdate() to get date

    declare @ID int

    DECLARE BCursor CURSOR FOR

    SELECT ID

    from exhibitor.mainTable

    OPEN BCursor

    FETCH NEXT FROM BCursor

    into @ID

    WHILE @@FETCH_STATUS =0

    BEGIN

    insert into exhibitor.dbo.blgBelongs(OwnerTable,OwnerID,Table1,Table1ID,Table2,Table2ID,TypeID,Enabled,Pro,blgid,LastUpdate)

    values (0,2,20,@ID,12,560,80,1,1000,11,[highlight=#ffff11]2013-11-26 12:20:30.560[/highlight])

    FETCH NEXT FROM BCursor

    into @ID

    end

    CLOSE BCursor

    DEALLOCATE BCursor

    I have this error

    Msg 102, Level 15, State 1, Line 11

    Incorrect syntax near '22'.

    Could you help me how can I solve it?

  • Put the date in single quotes

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Also, there is absolutely no need for a cursor here. You can turn this whole looping thing into a single insert statement like this.

    insert into exhibitor.dbo.blgBelongs(OwnerTable,OwnerID,Table1,Table1ID,Table2,Table2ID,TypeID,Enabled,Pro,blgid,LastUpdate)

    select 0,2,20,ID,12,560,80,1,1000,11, '2013-11-26 12:20:30.560'

    from exhibitor.mainTable

    _______________________________________________________________

    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 3 posts - 1 through 2 (of 2 total)

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