Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How to find rows returned by EXEC statement.

How to find rows returned by EXEC statement.

Scheduled Pinned Locked Moved Database
tutorialdatabasequestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jdkulkarni
    wrote on last edited by
    #1

    Friends, Can somebody tell me how to find number of rows returned by EXEC statement? I have following example. SET @Sql = 'SELECT COUNT(*) FROM Table where columnA = 10' EXEC (@Sql). Where can I get count? Jayant

    A 1 Reply Last reply
    0
    • J jdkulkarni

      Friends, Can somebody tell me how to find number of rows returned by EXEC statement? I have following example. SET @Sql = 'SELECT COUNT(*) FROM Table where columnA = 10' EXEC (@Sql). Where can I get count? Jayant

      A Offline
      A Offline
      andyharman
      wrote on last edited by
      #2

      Hi Jayant You would only the "EXEC" if your SQL statement is dynamic. The following code would allow you to get the count for your above static SQL:

      DECLARE @Rows INT
      SELECT @Rows = COUNT(*) FROM Table where ColumnA = 10
      

      If you want to search for different values then use:

      DECLARE @Rows INT, @MySearch INT
      SET @MySearch = 9
      SELECT @Rows = COUNT(*) FROM Table where ColumnA = @MySearch
      

      If your SQL truely needs to be dynamic then use:

      DECLARE @Rows INT, @SQL VARCHAR(500)
       
      --Create temporary table to hold results.
      CREATE TABLE #temp_result (Rows INT NOT NULL)
       
      --Construct and execute dynamic SQL to be used (would normally be more complex).
      SET @SQL = 'INSERT INTO #temp_result (Rows)
            SELECT COUNT(*) FROM Table where columnA < 10'
      EXEC (@SQL)
       
      --Get results.
      SELECT @Rows = Rows FROM #temp_result
      

      Hope that helps. Andy

      If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message".

      J 1 Reply Last reply
      0
      • A andyharman

        Hi Jayant You would only the "EXEC" if your SQL statement is dynamic. The following code would allow you to get the count for your above static SQL:

        DECLARE @Rows INT
        SELECT @Rows = COUNT(*) FROM Table where ColumnA = 10
        

        If you want to search for different values then use:

        DECLARE @Rows INT, @MySearch INT
        SET @MySearch = 9
        SELECT @Rows = COUNT(*) FROM Table where ColumnA = @MySearch
        

        If your SQL truely needs to be dynamic then use:

        DECLARE @Rows INT, @SQL VARCHAR(500)
         
        --Create temporary table to hold results.
        CREATE TABLE #temp_result (Rows INT NOT NULL)
         
        --Construct and execute dynamic SQL to be used (would normally be more complex).
        SET @SQL = 'INSERT INTO #temp_result (Rows)
              SELECT COUNT(*) FROM Table where columnA < 10'
        EXEC (@SQL)
         
        --Get results.
        SELECT @Rows = Rows FROM #temp_result
        

        Hope that helps. Andy

        If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message".

        J Offline
        J Offline
        jdkulkarni
        wrote on last edited by
        #3

        Hey Andy, Thanks a lot for gr8 answer.. It worked well. I voted 5... Jayant

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups