sp_executesql : Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'.

  • Hi,

    I am using dynamic query in stored procedure. while i am trying to execute the SQL Query in the Procedure, i am getting the error 'Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'.

    Because i have passed the parameter as Varchar in sp_executesql. The parms must be nVarchar. But my issues is, the length of Dynamic SQL Query is more than 4000. nVarchar maximum lenght is 4000.

    Please let me know the solution.

    my Stored Procedure as,

    CREATE procedure Search

    (

    @SearchKeyword Varchar(2000),

    @Productid varchar(2000),

    @Type varchar(200),

    @SQLPerms varchar(5000)

    )

    as

    Declare @sql varchar(7000)

    set @sql=' This Statement length goes more than 4000.'

    exec sp_executesql @sql

    GO

    Appriciate your response asap.

    Regards,

    Vijay

  • you can use exec, since you're not passing parameters in or out of the dynamic SQL

    CREATE procedure Search

    (

    @SearchKeyword Varchar(2000),

    @Productid varchar(2000),

    @Type varchar(200),

    @SQLPerms varchar(5000)

    )

    as

    Declare @sql varchar(7000)

    set @sql=' This Statement length goes more than 4000.'

    exec (@SQL)

    GO

    Make sure you're checking for SQL injection attempts.

    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
  • Sql string should be always either ntext/nvarchar/nchar.

    So ,please check type of sql query variable as nvarchar():w00t:

  • mayank.and.friends (6/10/2009)


    Sql string should be always either ntext/nvarchar/nchar.

    Only necessary when using sp_executesql (which takes nvarchar parameters). Not important when using EXEC. Also, you cannot define a variable of type ntext.

    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
  • this explains it why mine did not work also.. I had bigint and table data types..

    Cheers,
    John Esraelo

  • You can also use nvarchar(max) which replaces ntext.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • ..with possibilities of truncation?

    Cheers,
    John Esraelo

  • mayank.and.friends (6/10/2009)


    Sql string should be always either ntext/nvarchar/nchar.

    So ,please check type of sql query variable as nvarchar():w00t:

    Thank you! That solved my problem.

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

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