将存储过程结果集插入 Temp table 并查询 temp table

Insert stored procedure result set into Temp table and query temp table

我有以下存储过程正在尝试:

  1. 执行系统存储过程([=3​​0=])并将结果集放入临时table.
  2. SELECT 从这个临时 table 并添加 2 个自定义列(SOURCESERVER 和 CollectionTime)
  3. 这个最终结果集将通过 jdbc 作业被引入 Logstash。

我目前正在使用 SAP ASE 16 (sybase),我在关键字 'exec' 处遇到语法错误。我不确定我是否必须为存储过程添加前缀或什么,但我目前很困惑,感谢任何帮助。

USE db
GO
    CREATE PROCEDURE sp_active_con_ratio.sql AS
    DECLARE @servername varchar(32) DECLARE @collecttime DATETIME DECLARE @procparam varchar(32)
select
    @servername = @@servername
select
    @collecttime = getdate()
select
    @procparam = 'number of user connections' CREATE TABLE #TempUserConnections
    (
        TempName varchar(35),
        FreeConnections int,
        ActiveConnections int,
        PercentActive char(6),
        MaxUsed int,
        Reuse_cnt int,
        Instance_Name varchar(30) NULL
    )
INSERT INTO
    #TempUserConnections (TempName, FreeConnections, ActiveConnections, PercentActive, MaxUsed, Reuse_cnt, Instance_Name)
    exec sp_monitorconfig @procparam  **ERROR HERE**
SELECT
    @servername AS 'SOURCESERVER',
    FreeConnections,
    ActiveConnections,
    PercentActive,
    MaxUsed,
    @collecttime AS 'CollectionTime'
FROM
    #TempUserConnections
    DROP TABLE #TempUserConnections
    RETURN
GO

谢谢!

你可以这样做;

USE db
GO
CREATE PROCEDURE usp_active_con_ratio.sql AS
BEGIN

    DECLARE @servername varchar(32) = (select @@servername)
    DECLARE @collecttime DATETIME   = (select getdate())
    DECLARE @procparam varchar(32)  = (select 'number of user connections')

    
    CREATE TABLE #TempUserConnections
    (
        TempName varchar(35),
        FreeConnections int,
        ActiveConnections int,
        PercentActive char(6),
        MaxUsed int,
        Reuse_cnt int,
        Instance_Name varchar(30) NULL
    )

    INSERT INTO #TempUserConnections 
    (
        TempName, 
        FreeConnections, 
        ActiveConnections, 
        PercentActive, 
        MaxUsed, 
        Reuse_cnt, 
        Instance_Name
    )

    -- Add the semi-colon to terminate the statement
    EXEC sp_monitorconfig @procparam;

    SELECT
        @servername AS 'SOURCESERVER',
        FreeConnections,
        ActiveConnections,
        PercentActive,
        MaxUsed,
        @collecttime AS 'CollectionTime'
    FROM
        #TempUserConnections
        DROP TABLE #TempUserConnections
END

GO

@larnu 所述,您不应使用前缀 sp,我认为更好的前缀是 usp_.

确保您调用的存储过程 (sp_monitorconfig) 具有 RETURN

我忘记了 sp_monitorconfig 有一个可选的输入参数 (@result_tbl_name),它允许操作员指定一个 table 来插入结果。

来自 sp_monitorconfig 的文档,示例 #8 ...

首先创建 table 来保存结果;虽然 table 名称可能会有所不同,但您需要保持列 names/datatypes 的定义:

create table sample_table
(Name            varchar(35),
 Config_val      int,
 System_val      int,
 Total_val       int,
 Num_free        int,
 Num_active      int,
 Pct_act         char(6),
 Max_Used        int,
 Reuse_cnt       int,
 Date            varchar(30),
 Instance_Name   varchar(35))

要捕获一些指标:

exec sp_monitorconfig "locks",            sample_table
exec sp_monitorconfig "number of alarms", sample_table

显示指标:

-- select * from sample_table
exec sp_autoformat sample_data
go
 sp_autoformat sample_table
2> go
 Name             Config_val System_val Total_val Num_free Num_active Pct_act Max_Used Reuse_cnt Date                Instance_Name
 ---------------- ---------- ---------- --------- -------- ---------- ------- -------- --------- ------------------- -------------
 number of locks       10000        942     10000     9717        283   2.83       308         0 Aug 16 2020 12:26PM              
 number of alarms        400          0       400      386         14   3.50        14         0 Aug 16 2020 12:26PM