将变量传递给系统存储过程

时间:2017-11-16 16:57:58

标签: sql-server tsql

我确信我错过了一些简单的事情。我希望映射一些我的数据对象。我想创建一个过程,插入sp_depends的结果并插入数据名称。

这是我创造的:

create table DatasetMapping 
(
    Name nvarchar(257),
    Type nvarchar(16),
    updated nvarchar(7),
    Selected nvarchar(8),
    Column sysname
);

Insert into DatasetMapping
    exec sp_depends 'dbo.PositionReport_Quote_V3_2016'

这很适合手动。

然后我创建了

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[Repotingdatasets]
AS   
    DECLARE @object NVARCHAR(257)

    INSERT INTO DatasetMapping
        EXEC sp_epends  @objname = @object 

    EXEC [dbo].[Repotingdatasets] @object =' dbo.PositionReport_Quote_V3_2016';

但我只收到错误:

  

Msg 8146,Level 16,State 2,Procedure Repotingdatasets,Line 0 [Batch Start line 48]
  过程Repotingdatasets没有参数和参数提供

2 个答案:

答案 0 :(得分:0)

您只需要更改一些语法:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create PROCEDURE [dbo].[Repotingdatasets]( 
@object     nvarchar(257)
)
as

Insert into DatasetMapping
exec sp_depends  @objname=@object 

exec [dbo].[Repotingdatasets]  @object =' dbo.PositionReport_Quote_V3_2016';

答案 1 :(得分:0)

如果您打算传递变量,则不要声明变量。你正在寻找这个:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Repotingdatasets] @object nvarchar(257)
AS   
BEGIN

INSERT INTO DatasetMapping
EXEC sp_depends  @objname=@object

END

然后..

EXEC [dbo].[Repotingdatasets]  @object =' dbo.PositionReport_Quote_V3_2016';

如果您想要默认值,请将其添加到同一位置:

CREATE PROCEDURE [dbo].[Repotingdatasets] @object nvarchar(257) = 'default'