error with stored procedure

  • when i execute the SP with data parameters

    USE [ABC]

    GO

    DECLARE@return_value int

    EXEC@return_value = [dbo].[test]

    @QTRSTRTDATE = N'select CONVERT(datetime,DATEADD("M", DATEDIFF("M", 0, GETDATE()), 0),120)',

    @QTRENDDATE = N'select CONVERT(datetime,DATEADD("M",DATEDIFF("M",-1, GETDATE()),-1),120)'

    SELECT'Return Value' = @return_value

    GO

    i get the below error..

    Msg 8114, Level 16, State 5, Procedure usp_MarketingAccountLevel_test, Line 0

    Error converting data type nvarchar to datetime.

    (1 row(s) affected)

    Any help.

  • It would probably be useful to see what the stored procedure actually does.

    Any reason you pass dates converted to strings as parameters instead of the actual dates?

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • It's got to be because of the conversion of the dates to strings. Just leave them as dates. Use date math and date comparisons within T-SQL. They work. String manipulation instead of using core data types frequently leads to problems.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • SELECT

    @QTRSTRTDATE = DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0),

    @QTRENDDATE = DATEADD(m,DATEDIFF(m,-1, GETDATE()),-1)

    EXEC @return_value = [dbo].[test] @QTRSTRTDATE, @QTRENDDATE

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • in the procedure itself, do you have something like RETURN @MyDateValue

    the return of a stored procedure only can return an integer, and it typically means success(0) or failure(non zero) , like an error number.

    maybe you want to capture the resuts of the procedure instead? have it SELECT @MyDateValue

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Dooooooooooo (12/12/2013)


    when i execute the SP with data parameters

    USE [ABC]

    GO

    DECLARE@return_value int

    EXEC@return_value = [dbo].[test]

    @QTRSTRTDATE = N'select CONVERT(datetime,DATEADD("M", DATEDIFF("M", 0, GETDATE()), 0),120)',

    @QTRENDDATE = N'select CONVERT(datetime,DATEADD("M",DATEDIFF("M",-1, GETDATE()),-1),120)'

    SELECT'Return Value' = @return_value

    GO

    i get the below error..

    Msg 8114, Level 16, State 5, Procedure usp_MarketingAccountLevel_test, Line 0

    Error converting data type nvarchar to datetime.

    (1 row(s) affected)

    Any help.

    You're passing in strings (nvarchar), most likely, inside your procedure you've defined the parameters as datetime. It seems like you're expecting these to be evaluated and then passed, but I haven't had any luck passing an expression as a parameter to a stored procedure.

    CREATE PROCEDURE [dbo].[TEST]

    @TESTPARAM INT

    AS

    BEGIN

    SELECT @TESTPARAM

    END

    ...

    DECLARE @rc int

    DECLARE @TESTPARAM int

    -- TODO: Set parameter values here.

    EXECUTE @rc = [dbo].[TEST]

    @TESTPARAM=N'SELECT 1'

    GO

    Msg 8114, Level 16, State 4, Procedure TEST, Line 0

    Error converting data type nvarchar to int.

    DECLARE @rc int

    DECLARE @TESTPARAM int

    -- TODO: Set parameter values here.

    EXECUTE @rc = [dbo].[TEST]

    @TESTPARAM=(1+1)

    GO

    Msg 170, Level 15, State 1, Line 8

    Line 8: Incorrect syntax near '('.

Viewing 6 posts - 1 through 5 (of 5 total)

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