Stored procedure - Inserting getutcdate() returns error

Last post 07-06-2009 12:57 PM by zalkin. 6 replies.

Sort Posts:

  • Stored procedure - Inserting getutcdate() returns error

    07-06-2009, 6:24 AM
    • Member
      9 point Member
    • zalkin
    • Member since 08-13-2007, 4:35 AM
    • Posts 39

     This is my stored procedure:

    ALTER PROCEDURE [dbo].[Addentry]( 
                   @entHead VARCHAR(50)  = NULL, 
                   @entBody VARCHAR(2000)  = NULL, 
                   @entType SYSNAME, 
                   @entuID  INT  = 0) 
    AS 
      BEGIN 
        SET @entType = Rtrim(@entType) 
         
        SET @entuID = Rtrim(@entuID) 
         
        DECLARE  @cmd  AS NVARCHAR(MAX) 
         
        SET @cmd = N'INSERT INTO ' + Quotename(@entType) + N' (uID, Heading, Body, pTime) VALUES ' + (@entuID + ', ' + @entHead + ', ' + @entBody + ', ' + Getutcdate()) 
         
        EXEC Sp_executesql 
          @cmd 
         
        IF @@ERROR <> 0 
          RAISERROR ('Post was not added',16,1) 
      END 
    
    RETURN  
    

    The error I get is this: Conversion failed when converting datetime from character string

     

    I guess it has something to do with turning the getutcdate() into a string and then back to datetime. Is there another way around this?
    Any suggestions?

    Sincerely
    Niklas Kihl

  • Re: Stored procedure - Inserting getutcdate() returns error

    07-06-2009, 11:01 AM
    • Contributor
      5,138 point Contributor
    • papabear
    • Member since 08-08-2005, 3:49 PM
    • Posts 851

    I must admit, one the reasons I dislike dynamic sql is that it can be really, really hard to debug...

    What appears to be happening is that you do not have getUtcDate() included within the quotes and so it is actually running, not as part of the Insert, but as part of the Alter ...

    VALUES '+(@entuID+', '+@entHead+', '+ @entBody+', +getutcdate())'  EXEC sp_executesql @cmd  

    ie - move the quotes to put the getUtcDate() within them, rather than outside of them...

    Hope that helps,

    m

    give me suggestions for what to blog... http://www.myfriedmind.com/techblog -> thx

    Mark as "Answered" if this solves that wee old problem...
  • Re: Stored procedure - Inserting getutcdate() returns error

    07-06-2009, 11:40 AM
    • All-Star
      30,739 point All-Star
    • Naom
    • Member since 12-31-2007, 7:08 PM
    • Wisconsin
    • Posts 6,811

     Can you explain the reason of using Dynamic SQL in your case and not a direct insert command? In your case you just need to add ' to surround date.

    Looking for a job opportunity.

    Beware of bugs in the above code; I have only proved it correct, not tried it.
    (Donald Knuth)

    Visit my blog

    PluralSight Learning Library
  • Re: Stored procedure - Inserting getutcdate() returns error

    07-06-2009, 12:26 PM
    • Member
      9 point Member
    • zalkin
    • Member since 08-13-2007, 4:35 AM
    • Posts 39

     I'd be just as glad without a dynamic SQL but I can't seem to get the "QUOTENAME(@entType)" to work then. If you can help me write the INSERT statement without dynamic SQL I'd be just as happy =)

    I tried moving the '' around like u said but nothing seemed to help....got another error though...

    error: Conversion failed when converting the nvarchar value 'INSERT INTO [Articles] (uID, Heading, Body, pTime) VALUES (' to data type int. at System.Data.SqlClient.SqlConnection.OnError(SqlExce...

    Thanks in advance!

    Niklas Kihl

  • Re: Stored procedure - Inserting getutcdate() returns error

    07-06-2009, 12:29 PM
    Answer
    • All-Star
      30,739 point All-Star
    • Naom
    • Member since 12-31-2007, 7:08 PM
    • Wisconsin
    • Posts 6,811

     How many possible EventTypes you may have?

    If only a few, then just try

    IF @EventType = 'MyFirstType'

       insert into MyFirstType ...

    ELSE IF @EventType = 'MySecondType'

       ...

     

    etc.

     

    Otherwise you need to look into the syntax of sp_ExecuteSQL with attention and pass parameters definition there.

     

    Looking for a job opportunity.

    Beware of bugs in the above code; I have only proved it correct, not tried it.
    (Donald Knuth)

    Visit my blog

    PluralSight Learning Library
  • Re: Stored procedure - Inserting getutcdate() returns error

    07-06-2009, 12:31 PM
    • All-Star
      30,739 point All-Star
    • Naom
    • Member since 12-31-2007, 7:08 PM
    • Wisconsin
    • Posts 6,811
    Looking for a job opportunity.

    Beware of bugs in the above code; I have only proved it correct, not tried it.
    (Donald Knuth)

    Visit my blog

    PluralSight Learning Library
  • Re: Stored procedure - Inserting getutcdate() returns error

    07-06-2009, 12:57 PM
    • Member
      9 point Member
    • zalkin
    • Member since 08-13-2007, 4:35 AM
    • Posts 39

    Ahh, that works just perfect.

    Thank you Naom! 

Page 1 of 1 (7 items)