【问题标题】:Insert stored procedure result set into Temp table and query temp table将存储过程结果集插入临时表并查询临时表
【发布时间】:2020-12-05 08:25:35
【问题描述】:

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

  1. 执行系统存储过程 (sp_monitorconfig) 并将结果集放入临时表中。
  2. 从这个临时表中选择并添加 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

谢谢!

【问题讨论】:

  • 你没有告诉我们什么那个错误是什么。此外,sp_ 是 Microsoft 保留的前缀,不应用于用户过程。
  • @Larnu 感谢您的评论。抱歉,错误只是“关键字 exec 附近的语法不正确”。另外,我更改了 db 名称和 sp 名称,因为它们都包含公司信息 :)
  • @basement 此代码在我的环境中运行没有错误!。
  • insert / exec 在 Sybase ASE 中不受支持;虽然你可以use a proxy table to insert proc output into a table 这有点令人费解;你最好的选择是从sp_monitorconfig 的源中提取所需的查询并构建你自己/自定义的 sql 来做你想做的事

标签: sql tsql stored-procedures sybase sap-ase


【解决方案1】:

你可以这样做;

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

【讨论】:

    【解决方案2】:

    我忘记了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
    

    【讨论】:

    • 这正是我想要的。非常感谢!
    猜你喜欢
    • 2020-01-26
    • 2018-07-05
    • 1970-01-01
    • 2015-11-12
    • 2015-03-05
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多