none
Simple sql query assingment

    Question

  • Thanks in advance

     

    Please, help me with a code to assign query result ( count(*)) to @RowCount.

     

    /* DECLARE Part Omitted */

     

    SET @Job_Code = '(Job_Code = ''A'')';

    SET @RowCount = -1;

     

    SET @SQL = 'Select Count(*) FROM MyTable WHERE' + @Job_Code;

     

    EXEC(@SQL);

    /* Help please!

    I need code kind of doing assining Count(*) to @RowCount --

    SET @RowCount = EXEC(@SQL)*/

     

    Print @RowCount;

    Wednesday, December 13, 2006 12:21 AM

All replies

  • Why are you using dynamic SQL? Can't you just do:
     
    SET @RowCount = (SELECT COUNT(*) FROM MyTable WHERE Job_Code = 'A');
     
    -- or
    SET @RowCount = (SELECT COUNT(*) FROM MyTable WHERE Job_Code = @Job_Code);
    Wednesday, December 13, 2006 1:44 AM
  • the following SQL also work..

    SELECT @RowCount=COUNT(*) FROM MyTable WHERE Job_Code = 'A';
     
    -- or
    SELECT @RowCount=COUNT(*) FROM MyTable WHERE Job_Code = @Job_Code
    Wednesday, December 13, 2006 5:50 AM