如何在 SQL Server 中声明变量并在同一存储过程中使用它

发布于 2024-08-31 23:11:50 字数 355 浏览 5 评论 0原文

我试图从一个表中的 BrandID 获取值并将其添加到另一个表中。但我无法让它发挥作用。有人知道如何正确做吗?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萤火眠眠 2024-09-07 23:11:50

我可以看到该 SP 存在以下问题,这些问题可能与您的问题相关,也可能无关:

  • 您的 SELECT@BrandName 之后有一个无关的 ) code> (最后)
  • 您没有将 @CategoryID@BrandName 设置为任何地方的任何内容(它们是局部变量,但您不为它们赋值)

在评论中您说过,修复 ) 后您会收到错误:

过程 AddBrand 没有参数,但已提供参数。

这表明您尚未为 SP 声明任何参数,但您是使用参数来调用它的。根据您对 @CategoryID 的回复,我猜您希望它是一个参数而不是局部变量。试试这个:

CREATE PROCEDURE AddBrand
   @BrandName nvarchar(50), -- These are the
   @CategoryID int          -- parameter declarations
AS
BEGIN
   DECLARE @BrandID int

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

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

然后您可以这样称呼它:

EXEC AddBrand 'Gucci', 23

或这样:

EXEC AddBrand @BrandName = 'Gucci', @CategoryID = 23

...假设品牌名称是“Gucci”,类别 ID 是 23。

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)

In a comment you've said that after fixing the ) you get the error:

Procedure AddBrand has no parameters and arguments were supplied.

That's telling you that you haven't declared any parameters for the SP, but you called it with parameters. Based on your reply about @CategoryID, I'm guessing you wanted it to be a parameter rather than a local variable. Try this:

CREATE PROCEDURE AddBrand
   @BrandName nvarchar(50), -- These are the
   @CategoryID int          -- parameter declarations
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

or this:

EXEC AddBrand @BrandName = 'Gucci', @CategoryID = 23

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

国际总奸 2024-09-07 23:11:50
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
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
裂开嘴轻声笑有多痛 2024-09-07 23:11:50

在 sql 2012(也许早在 2005 年)中,你应该这样做:

EXEC AddBrand @BrandName = 'Gucci', @CategoryId = 23

In sql 2012 (and maybe as far back as 2005), you should do this:

EXEC AddBrand @BrandName = 'Gucci', @CategoryId = 23
木落 2024-09-07 23:11:50

上述方法都不适合我,所以我按照我的方式发布

DELIMITER $
CREATE PROCEDURE AddBrand()
BEGIN 

DECLARE BrandName varchar(50);
DECLARE CategoryID,BrandID  int;
SELECT BrandID = BrandID FROM tblBrand 
WHERE BrandName = BrandName;

INSERT INTO tblBrandinCategory (CategoryID, BrandID) 
       VALUES (CategoryID, BrandID);
END$

None of the above methods worked for me so i'm posting the way i did

DELIMITER $
CREATE PROCEDURE AddBrand()
BEGIN 

DECLARE BrandName varchar(50);
DECLARE CategoryID,BrandID  int;
SELECT BrandID = BrandID FROM tblBrand 
WHERE BrandName = BrandName;

INSERT INTO tblBrandinCategory (CategoryID, BrandID) 
       VALUES (CategoryID, BrandID);
END$
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文