将选择结果传递到存储过程中的插入
我正在尝试创建一个 SP,它将根据提供的 ArticleTypeName 从另一个表中找到 ArticleType(pkey 我忘了在其上添加 ID),并使用它插入到类别表中的新类别中。最后,新行的 ID 在输出参数中发回。我认为一切都是正确的,但我不断收到错误消息,指出“插入”周围的语法不正确。我确信我错过了一些非常简单的东西!
ALTER PROCEDURE InsertCategory
@ParentCatID int,
@CategoryName varchar(25),
@ArticleTypeName varchar(15),
@NewCategoryID int OUTPUT
AS
BEGIN
DECLARE @ArticleType int
SELECT @ArticleType=ArticleType
FROM (
SELECT TOP 1 ArticleType FROM ArticleTypes
WHERE (ArticleTypeName=@ArticleTypeName))
INSERT INTO Categories (ParentCatID, CategoryName, ArticleType) VALUES (@ParentCatID, @CategoryName, @ArticleType)
SET @NewCategoryID = Scope_Identity()
END
I'm trying to make a SP that will find the ArticleType (pkey I forgot to put ID on it) from another table based on the ArticleTypeName supplied and use that to insert into the new category in the categories table. Finally the new row's ID is sent back in an output param. I thought everything was correct but I keep getting an error stating that the syntax is incorrect around 'Insert'. I'm sure I'm missing something really simple!
ALTER PROCEDURE InsertCategory
@ParentCatID int,
@CategoryName varchar(25),
@ArticleTypeName varchar(15),
@NewCategoryID int OUTPUT
AS
BEGIN
DECLARE @ArticleType int
SELECT @ArticleType=ArticleType
FROM (
SELECT TOP 1 ArticleType FROM ArticleTypes
WHERE (ArticleTypeName=@ArticleTypeName))
INSERT INTO Categories (ParentCatID, CategoryName, ArticleType) VALUES (@ParentCatID, @CategoryName, @ArticleType)
SET @NewCategoryID = Scope_Identity()
END
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
子查询需要别名。例如,这会给出错误:
但这会起作用:
因此,在您的情况下,请在此时添加别名:
A subquery requires an alias. For example, this will give an error:
But this will work:
So in your case, add the alias at this point:
尝试使用 SELECT 来设置标识而不是使用 SET
Try using SELECT to set identity instead of using SET