Matrix001
Matrix001

Reputation: 1272

Need help perfoming SQL Insert using parameters in a stored procedure

ALTER PROCEDURE dbo.Articles_InsertArticle

    @Article nvarchar(MAX),
    @UsersID uniqueidentifier
AS
    INSERT INTO Articles 
          (UsersID,Article,PageNumber)
    SELECT @UsersID,
           @Article,
           floor(COUNT(ArticleID)/5)
      FROM Articles

I am not sure if the syntax correct..But what I want is to pass the query 2 parameters: Article and UserID..insert both, then insert the page number..Basically get the count of the article ID and divide by 5... I cant get an awkward number. I also want the Number to be a whole number.. (so not 1/5 but 0..not 9/5 , but 1)

UPDATED ABOVE..

It tells me to declare UsersID..syntax error

Upvotes: 0

Views: 496

Answers (2)

Ryan Gross
Ryan Gross

Reputation: 6515

INSERT INTO Articles (UsersID,Article,PageNumber) 
VALUES (@UsersID, @Article, ROUND(SELECT COUNT(ArticleID)/5 FROM Articles),0)

Upvotes: 1

Derek
Derek

Reputation: 23228

I'm not following where PageNumber comes from. But at the very least, the structure of your insert statement is incorrect and should look like this:

insert into Articles(UsersID, Article, PageNumber)
select @UsersID, @Article, count(*) / 5
FROM Articles
WHERE Article = @Article

I took a guess at what you're looking for with the PageNumber, but we'll likely need more information.

Upvotes: 0

Related Questions