簡體   English   中英

將存儲過程結果集插入臨時表並查詢臨時表

[英]Insert stored procedure result set into Temp table and query temp table

我有以下存儲的過程,它試圖:

  1. 執行系統存儲過程(sp_monitorconfig)並將結果集放入臨時表中。
  2. SELECT 從這個臨時表中添加 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_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',
        FreeConnections,
        ActiveConnections,
        PercentActive,
        MaxUsed,
        @collecttime AS 'CollectionTime'
    FROM
        #TempUserConnections
        DROP TABLE #TempUserConnections
END

GO

正如@larnu提到的,您不應該使用前綴sp ,我認為更好的前綴是usp_

確保您正在調用的存儲過程 ( sp_monitorconfig ) 具有RETURN

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM