在 SQL 服务器存储过程中插入数据

Insert data in SQL Server stored procedure

我正在尝试使用 SQL 服务器存储过程在单个 table 中添加数据,但出现错误

procedure or function x has too many argument specified

另外,我如何获取新创建的记录 ID,因为我需要使用这些 ID

更新多个 table

我正在使用 SQL Server 2012

非常感谢

ALTER PROCEDURE [dbo].[CreateNewFunctionsNavigation]
   @FunctionName nvarchar(250),
   @Hierarchy_Level int
AS
BEGIN
   SET NOCOUNT ON;

   INSERT [dbo].[Navigation_Functions] ([FunctionName], [Hierarchy_Level])
   VALUES(@FunctionName, @Hierarchy_Level)
END

存储过程的执行:

DECLARE @return_value int

EXEC    @return_value = [dbo].[CreateNewFunctionsNavigation]
        @FunctionName = N'DSD',
        @Hierarchy_Level = 3

SELECT  'Return Value' = @return_value
GO

我的 SQL 函数 table

USE [MySolution01_DB]
GO

/****** Object:  Table [dbo].[Navigation_Functions]    Script Date: 06/01/2015 15:58:57 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Navigation_Functions](
 [Function_ID] [int] IDENTITY(1,1) NOT NULL,
 [FunctionName] [nvarchar](250) NOT NULL,
 [Hierarchy_Level] [int] NOT NULL,
CONSTRAINT [PK_Functions] PRIMARY KEY CLUSTERED 
(
  [Function_ID] ASC
 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,   ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

  GO

应该是

INSERT INTO [dbo].[Navigation_Functions] ([FunctionName], [Hierarchy_Level])

我创建了一个样本table

CREATE TABLE [dbo].[Navigation_Functions](
    [NavigationFunctionId] [int] IDENTITY(1,1) NOT NULL,
    [FunctionName] [nvarchar](250) NULL,
    [Hierarchy_Level] [int] NULL,
 CONSTRAINT [PK_Navigation_Functions] PRIMARY KEY CLUSTERED 
(
    [NavigationFunctionId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

您可以通过以下方式获取最后一个 ID:SELECT SCOPE_IDENTITY();

USE [MySolution01_DB]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[CreateNewFunctionsNavigation]
  @FunctionName nvarchar(250),
  @Hierarchy_Level INT,
  @Function_identity INT OUTPUT
AS
BEGIN
SET NOCOUNT ON;

INSERT INTO [dbo].[Navigation_Functions] ([FunctionName],[Hierarchy_Level] ) 
VALUES(@FunctionName, @Hierarchy_Level);

   SET @Function_identity=SCOPE_IDENTITY()
   RETURN @Function_identity

RETURN
END