views:

122

answers:

1

Im trying to get the value from BrandID in one table and add it to another table. But I can't get it to work. Anybody know how to do it right?

CREATE PROCEDURE AddBrand
AS

DECLARE 
@BrandName nvarchar(50),
@CategoryID int,
@BrandID int

SELECT @BrandID = BrandID FROM tblBrand 
WHERE BrandName = @BrandName

INSERT INTO tblBrandinCategory (CategoryID, BrandID) 
       VALUES (@CategoryID, @BrandID) 

RETURN
+1  A: 

What's going wrong with what you have? What error do you get, or what result do or don't you get that doesn't match your expectations?

I can see the following issues with that SP, which may or may not relate to your problem:

  • You have an extraneous ) after @BrandName in your SELECT (at the end)
  • You're not setting @CategoryID or @BrandName to anything anywhere (they're local variables, but you don't assign values to them)

Edit Responding to your comment: The error is telling you that you haven't declared any parameters for the SP (and you haven't), but you called it with parameters. Based on your reply about @CategoryID, I'm guessing you wanted it to be a paramter rather than a local variable. Try this:

CREATE PROCEDURE AddBrand
   @BrandName nvarchar(50),
   @CategoryID int
AS
BEGIN
   DECLARE @BrandID int

   SELECT @BrandID = BrandID FROM tblBrand WHERE BrandName = @BrandName

   INSERT INTO tblBrandinCategory (CategoryID, BrandID) VALUES (@CategoryID, @BrandID)
END

You would then call this like this:

EXEC AddBrand 'Gucci', 23

...assuming the the brand name was 'Gucci' and category ID was 23.

T.J. Crowder
I get the following error: Procedure AddBrand has no parameters and arguments were supplied.I Corrected the ")"I dont know what you mean about the CategoryID, I have a value on my aspx page that I bring in to the SP and would like to add to tblBrandinCategory
Nicklas
Works great! 10 hours of headache, and you solve it for me on 5 minutes, thanks!
Nicklas
@Nicklas: LOL, no worries, glad that was it. Don't forget to accept the answer if it solves the problem for you. :-) (I mention this because you're new here.)
T.J. Crowder
Im not sure how to do that, I pressed link under your answer, was that correct or how do I do it?
Nicklas
@Nicklas: click the checkmark
Hao
@Nicklas: At the top of my answer, just to the left, there's a check mark. Just click on it. Pictures are worth 1,000 words, so: http://i39.tinypic.com/5evj1e.jpg (from http://meta.stackoverflow.com/questions/5234/how-does-accepting-an-answer-work).
T.J. Crowder
Great found it! :) Thanks again!
Nicklas
@Nicklas: No worries. :-) Welcome to StackOverflow!
T.J. Crowder