0
我想從VBScript中查詢SQL Server 2008實例。 我知道我的連接正在工作,因爲當我使用一個簡單的查詢,如下面的它可以正常工作。從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
你真的在應用程序中直接在單行上直接寫入那麼多的sql嗎?這是不可能破譯的。這裏的錯誤信息很清楚。它與sql權限無關,它與你的對象有關。我的猜測是你永遠不會打開你的數據庫連接。 –
你應該將這個SQL封裝到一個存儲過程中,像這樣管理它將是一場噩夢。 – Lankymart
問題在於缺少SET NOCOUNT ON;因此所有的INSERT語句都會產生空的關閉記錄集。在沒有SET NOCOUNT ON的情況下,您必須一直調用Set oSfRs = oSfRs.NextRecordset,直到找到與SQL中的SELECT執行對應的記錄集。 – Lankymart