将存储过程结果集插入到临时表并查询临时表

问题描述

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

  1. 执行系统存储过程(sp_monitorconfig),并将结果集放入临时表中。
  2. 从此临时表中选择并添加2个自定义列(SOURCESERVER和CollectionTime)
  3. 此最终结果集将通过jdbc作业提取到Logstash中。

我当前正在使用SAP ASE 16(sybase),并且我在关键字'exec'上遇到了不正确的语法错误。我不确定是否必须为存储的proc或其他内容添加前缀,但是我目前很困惑,希望能获得任何帮助。

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',@collecttime AS 'CollectionTime'
FROM
    #TempuserConnections
    DROP TABLE #TempuserConnections
    RETURN
GO

谢谢!

解决方法

我忘记了sp_monitorconfig有一个可选的输入参数(@result_tbl_name),该参数允许操作员指定要在其中插入结果的表。

摘自sp_monitorconfig上的文档,示例#8 ...

首先创建表以保存结果;尽管表名可能会有所不同,但您需要将列名/数据类型保持定义:

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
,

您可以执行以下操作;

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',@collecttime AS 'CollectionTime'
    FROM
        #TempUserConnections
        DROP TABLE #TempUserConnections
END

GO

正如@larnu所述,您不应使用前缀sp,在我看来,更好的前缀是usp_

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