[英]Results from a select passed to an insert in a Stored Proc
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. 我正在尝试制作一个SP,该SP将根据提供的ArticleTypeName从另一个表中找到ArticleType(我忘记在上面放ID的pkey),并将其用于插入Categories表中的新类别中。 Finally the new row's ID is sent back in an output param. 最后,新行的ID在输出参数中发送回。 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
A subquery requires an alias. 子查询需要别名。 For example, this will give an error: 例如,这将产生错误:
select * from (select id from MyTable)
But this will work: 但这将起作用:
select * from (select id from MyTable) as SubQueryAlias
So in your case, add the alias at this point: 因此,在您的情况下,请在此时添加别名:
FROM (
SELECT TOP 1 ArticleType FROM ArticleTypes
WHERE (ArticleTypeName=@ArticleTypeName)) as SubQueryName;
尝试使用SELECT设置标识而不是使用SET
SELECT @NewCategoryID = Scope_Identity()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.