How to pass datetime parameter to SP dynamically

  • Hi,In below query I want to pass the parameter whose datatype is Datatime,dynamically,

    I am getting following error,

    Conversion failed when converting date and/or time from character string.

    Declare @P_AsonDate as datetime = Getdate(),@Str_ToDo As varchar(1000)

    Set @Str_ToDo = ''

    Set @Str_ToDo = 'SpTable_2mail' + ' ' + @P_AsonDate

    Print @Str_ToDo

    EXEC (@Str_ToDo )

    Please help me,

    This is urgent to me.

  • DECLARE @P_AsonDate AS DATETIME = GETDATE(),

    @Str_ToDo AS NVARCHAR(MAX) = '',

    @params AS NVARCHAR(MAX);

    SET @Str_ToDo = 'SpTable_2mail @P_AsonDate';

    SET @params = '@P_AsonDate AS DATETIME';

    EXEC sp_executesql @Str_ToDo, @params, @P_AsonDate = @P_AsonDate;

    http://technet.microsoft.com/en-us/library/ms188001.aspx

    The SQL Guy @ blogspot[/url]

    @SeanPearceSQL

    About Me[/url]

  • avdhut.k (10/14/2013)


    Hi,In below query I want to pass the parameter whose datatype is Datatime,dynamically,

    I am getting following error,

    Conversion failed when converting date and/or time from character string.

    Declare @P_AsonDate as datetime = Getdate(),@Str_ToDo As varchar(1000)

    Set @Str_ToDo = ''

    Set @Str_ToDo = 'SpTable_2mail' + ' ' + @P_AsonDate

    Print @Str_ToDo

    EXEC (@Str_ToDo )

    Please help me,

    This is urgent to me.

    Best way is just like Sean posted...

    On your post you need to close the quote marks after date and also need to convert the datetime to a string format with date format..

    CREATE PROCEDURE SpTable_2mail @Date DATETIME

    AS

    SELECT @date

    GO

    DECLARE @P_AsonDate as datetime = Getdate(),@Str_ToDo As varchar(1000)

    Set @Str_ToDo = ''

    Set @Str_ToDo = 'SpTable_2mail ''' + CONVERT(VARCHAR(100),@P_AsonDate , 109) + ''''

    Print @Str_ToDo

    EXEC (@Str_ToDo )



    If you need to work better, try working less...

  • Thanks!!!!!!!!!!!!

Viewing 4 posts - 1 through 3 (of 3 total)

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