从VBScript在SQL Server 2008中创建临时表

时间:2017-01-12 14:34:03

标签: sql-server sql-server-2008 vbscript temp-tables

我正在尝试从VBScript查询SQL Server 2008实例。 我知道我的连接正在工作,因为当我使用下面的简单查询时,它工作正常。

sfQuery2 = "SELECT TOP 10 * FROM [DB].[schema].[table]"

我不确定的一件事是我连接的用户是否具有只读权限。不确定用户标识是否能够创建临时表(如果它们是只读的)。我确实使用此用户从SQL Server运行相同的查询,并且查询工作正常。但是当我尝试使用vbscript中的相同用户运行此操作时,当我尝试读取记录集时,我得到的错误是.....

  

ADODB.Recordset:关闭对象时不允许操作。

sfQuery2 = "CREATE TABLE #Temp1  ([LOGNAME] [nvarchar](20) NULL,    [MESSAGE_TYPE] [int] NULL,  [COMPONENT] [nvarchar](50) NULL,    [LOGTIME] [nvarchar](17) NOT NULL,  [SUB_SYSTEM] [nvarchar](40) NULL,   [STACK_ID] [nvarchar](120) NULL,    [SUBSTACK_ID] [int] NULL,   [MESSAGE] [nvarchar](1800) NULL,    [DETAIL] [nvarchar](1800) NULL )  INSERT INTO #Temp1 SELECT DISTINCT [LOGNAME], [MESSAGE_TYPE],                 [COMPONENT], LOGTIME,               [SUB_SYSTEM], [STACK_ID],               [SUBSTACK_ID], [MESSAGE],               [DETAIL]                FROM [DB].[schema].[table]  WHERE cast(LEFT(LOGTIME,8) as date) = cast(getdate() -1 as date)    AND [MESSAGE] LIKE '%Exporter->Archive' CREATE TABLE #Temp2  ([LOGNAME] [nvarchar](20) NULL,    [MESSAGE_TYPE] [int] NULL,  [COMPONENT] [nvarchar](50) NULL,    [LOGTIME] [nvarchar](17) NOT NULL,  [SUB_SYSTEM] [nvarchar](40) NULL,   [STACK_ID] [nvarchar](120) NULL,    [SUBSTACK_ID] [int] NULL,   [MESSAGE] [nvarchar](1800) NULL,    [DETAIL] [nvarchar](1800) NULL )  INSERT INTO #Temp2 SELECT DISTINCT [LOGNAME], [MESSAGE_TYPE],                 [COMPONENT],cast(LEFT(LOGTIME,8) as date) AS YesterdayDate,                 [SUB_SYSTEM], [STACK_ID],               [SUBSTACK_ID], [MESSAGE],               [DETAIL]                FROM [DB].[schema].[table]  WHERE cast(LEFT(LOGTIME,8) as date) = cast(getdate() -1 as date)    AND [DETAIL] LIKE 'USER:%' SELECT * FROM (  SELECT RIGHT(b.DETAIL, 7) AS AXAID, cast(LEFT(a.LOGTIME,8) as date) AS [DATE], b.STACK_ID,  ROW_NUMBER() OVER(PARTITION by b.STACK_ID ORDER BY a.LOGTIME DESC) rn   FROM #Temp1 AS a    INNER JOIN #Temp2 AS b  ON a.STACK_ID = b.STACK_ID  WHERE a.[MESSAGE] LIKE '%Exporter->Archive'     ) a WHERE rn = 1 "
oSfRs.Open sfQuery2, oSfCn

if osfrs.BOF then 
Debug "There are NO results"
else
Debug "There is data in the Record Set"
end if

我不确定它是否是我的SQL的语法错误,或者是否因为用户的权限而存在错误。任何帮助将是欣赏。 如果需要更多信息,请告诉我。

对于那些努力阅读SQL的人来说,这是一个格式化的版本

CREATE TABLE #Temp1  (
    [LOGNAME] [nvarchar](20) NULL,
    [MESSAGE_TYPE] [int] NULL,  
    [COMPONENT] [nvarchar](50) NULL,    
    [LOGTIME] [nvarchar](17) NOT NULL,  
    [SUB_SYSTEM] [nvarchar](40) NULL,   
    [STACK_ID] [nvarchar](120) NULL,    
    [SUBSTACK_ID] [int] NULL,   
    [MESSAGE] [nvarchar](1800) NULL,    
    [DETAIL] [nvarchar](1800) NULL 
)  

INSERT INTO #Temp1 
SELECT DISTINCT [LOGNAME], [MESSAGE_TYPE], [COMPONENT], LOGTIME, [SUB_SYSTEM]
, [STACK_ID], [SUBSTACK_ID], [MESSAGE], [DETAIL]
FROM [DB].[schema].[table]  
WHERE cast(LEFT(LOGTIME,8) as date) = cast(getdate() -1 as date)    
AND [MESSAGE] LIKE '%Exporter->Archive' 

CREATE TABLE #Temp2  (
    [LOGNAME] [nvarchar](20) NULL,    
    [MESSAGE_TYPE] [int] NULL,  
    [COMPONENT] [nvarchar](50) NULL,    
    [LOGTIME] [nvarchar](17) NOT NULL,  
    [SUB_SYSTEM] [nvarchar](40) NULL,   
    [STACK_ID] [nvarchar](120) NULL,    
    [SUBSTACK_ID] [int] NULL,   
    [MESSAGE] [nvarchar](1800) NULL,    
    [DETAIL] [nvarchar](1800) NULL 
)  

INSERT INTO #Temp2 SELECT DISTINCT [LOGNAME], [MESSAGE_TYPE], [COMPONENT]
, cast(LEFT(LOGTIME,8) as date) AS YesterdayDate
, [SUB_SYSTEM], [STACK_ID], [SUBSTACK_ID], [MESSAGE], [DETAIL]
FROM [DB].[schema].[table]  
WHERE cast(LEFT(LOGTIME,8) as date) = cast(getdate() -1 as date)    
AND [DETAIL] LIKE 'USER:%' 

SELECT * 
FROM (  
    SELECT RIGHT(b.DETAIL, 7) AS AXAID, cast(LEFT(a.LOGTIME,8) as date) AS [DATE]
    , b.STACK_ID,  ROW_NUMBER() OVER(PARTITION by b.STACK_ID ORDER BY a.LOGTIME DESC) rn   
    FROM #Temp1 AS a    
    INNER JOIN #Temp2 AS b  ON a.STACK_ID = b.STACK_ID  
    WHERE a.[MESSAGE] LIKE '%Exporter->Archive'     
) a WHERE rn = 1

1 个答案:

答案 0 :(得分:1)

Lankymart发表评论解决。 解决方法是将SET NOCOUNT ON添加到查询的开头。