How to declare a variable in SQL Server and use it in the same Stored Procedure

后端 未结 4 1044
再見小時候
再見小時候 2021-02-03 17:57

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          


        
4条回答
  •  时光说笑
    2021-02-03 18:36

    CREATE PROCEDURE AddBrand
    @BrandName nvarchar(50) = null,
    @CategoryID int = null
    AS    
    BEGIN
    
    DECLARE @BrandID int = null
    SELECT @BrandID = BrandID FROM tblBrand 
    WHERE BrandName = @BrandName
    
    INSERT INTO tblBrandinCategory (CategoryID, BrandID) 
           VALUES (@CategoryID, @BrandID)
    
    END
    
    EXEC AddBrand @BrandName = 'BMW', @CategoryId = 1
    

提交回复
热议问题