How solve it??

  • I have the following procedure. When i execute it it's giving me syntax error near '='. When I remove @count it works. So what's wrong with the syntax like this and how to solve it??

    create PROCEDURE [dbo].[USP_GetFileCount] @tableName nvarchar(50),@colName nvarchar(50), @recordID int, @count int output

    AS

    exec( 'select ' + @count + ' = count(*) from '+ @tableName + 'where ' + @colName + ' = ' + @recordID)

  • You have to convert @Count to VARCHAR or CHAR

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • Try

    declare @sql nvarchar(100)

    set @sql = 'select @count = count(*) from ' +

    @tableName + ' where ' + @colName + ' = ' + CAST(@recordID as varchar)

    exec sp_executesql @sql,N'@count int output',@count output

    select @count

    Far away is close at hand in the images of elsewhere.
    Anon.

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

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