Word Count for User

0

Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow Meta en español

Preguntas y respuestas sobre el sitio para programadores y profesionales de la informática

-- How many words a given user posted?
-- use this snippet to word count a user

DECLARE @UserId int = ##UserId##
DECLARE @total_count int, @post_id int, @post_body nvarchar(max);  

SET @total_count = 0;

DECLARE vendor_cursor CURSOR FOR
SELECT id, Body
FROM posts 
WHERE posts.OwnerUserId = @UserId;

OPEN vendor_cursor

FETCH NEXT FROM vendor_cursor   
INTO @post_id, @post_body
PRINT FORMATMESSAGE('  id   | count | total count')
WHILE @@FETCH_STATUS = 0
BEGIN
    DECLARE @count int;
    SELECT @count = (LEN(@post_body ) - LEN(replace(@post_body , ' ', '')))
    SELECT @total_count = @total_count + @count
    PRINT FORMATMESSAGE('%6d | %5d | %d', @post_id, @count, @total_count)
    FETCH NEXT FROM vendor_cursor
    INTO @post_id, @post_body
END

Enter Parameters

Options:
Switch to main site
loading Hold tight while we fetch your results