Announcement

Collapse
No announcement yet.

Stored Procedure to count rows

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Stored Procedure to count rows

    I am new to sql and I need a stored procedure that can count number of rows in a table. I have googled but it confuses me more then it helps me.

  • #2
    You will need to use dynamic SQL to accept your table name as a parameter

    Create your SP using this code:
    Code:
    CREATE PROCEDURE dbo.sp_selectcount 
    @tablename NVARCHAR(200) A
    S 
    DECLARE CMD NVARCHAR (255) 
    SET CMD = 'SELECT count(*) from ' + @tablename 
    EXEC sp_executesql CMD
    Execute it using EXEC dbo.sp_selectcount(mytable)
    Tom Jones
    MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
    PhD, MSc, FIAP, MIITT
    IT Trainer / Consultant
    Ossian Ltd
    Scotland

    ** Remember to give credit where credit is due and leave reputation points where appropriate **

    Comment

    Working...
    X