1

我正在将 Excel 报表从访问 SQLAnywhere 数据库中的数据转换为 SQL Server 2008R2 Express 数据库,并且在运行它时不断出现错误。用户输入一些他们想要比较销售动态的项目,然后按下按钮来运行宏。宏获取项目,创建 SQL 查询字符串,获取结果并显示它们。

这与旧数据库完美配合。现在使用 SQL Server,如果我运行宏,我会收到一个无用的“400”错误。如果我单步执行代码,则在“With ActiveSheets.QueryTables.Add(...)”段的末尾会抛出“运行时错误'1004'”。我猜我的语法有些东西不受 SQL Server 或 OLE DB 连接的支持,但如果我能看到它是什么,那该死的。

我已经完成了生成的 SQL 查询的 Debug.Print,如果我将它复制/粘贴到 SQL Server Studio 中的查询窗口,它可以完美运行。如果我将查询替换为“从 DEPT_TAB 中选择 *”之类的简单内容,则它可以在宏中正常运行。创建一个表也可以正常运行,但是添加一个 INSERT 语句的一些事情会把它扔掉。

这是宏代码:

Sub SalesHistory()
' Runs SQL from sheet and places into report
' declare method variables
Dim sSQL As String
Dim sConn As String
Dim N1 As Integer
Dim LR As Integer
' create connection string for SQL Server Express
sConn = "OLEDB;Provider=SQLOLEDB;" & _
    "Data Source=192.168.0.29\SQLEXPRESS;" & _
    "Initial Catalog=STORESQL;" & _
    "User ID=xxxxx;Password=xxxxx;Trusted_Connection=False;"

' stop screen updating while running
Application.ScreenUpdating = False

' select SQL sheet and get number of rows in column A
Sheets("SQL").Select
LR = Range("A" & Rows.Count).End(xlUp).Row

' loop through column A and create SQL command string
For N1 = 1 To LR
sSQL = sSQL & " " & Range("A" & N1).Value
Next N1

' test print sSQL
Debug.Print sSQL

' select Main sheet, clear report, and activate for new results
Sheets("Main").Select
Range("$A$2:$D$51").ClearContents
Sheets("Main").Activate

' use QueryTables.Add() to get data from server
With ActiveSheet.QueryTables.Add(Connection:=sConn, Destination:=Range("A2))
    .CommandText = sSQL
    .Name = "Movement"
    .FieldNames = False
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlOverwriteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    .Refresh BackgroundQuery:=False
End With  
' HERE IS WHERE THE ERROR IS THROWN

' restart ScreenUpdating
Application.ScreenUpdating = True

End Sub

这是工作表中的查询:

-- declare variables
DECLARE @StartDate DATE;
DECLARE @Location CHAR(3);
SET @StartDate = '07/29/2012';
SET @Location = 'PAL';
-- create temp table for holding results
CREATE TABLE #tmp_ItemMove (
UPC CHAR(13) NULL,
Brand CHAR(32) NULL,
Descriptor CHAR(32) NULL,
Sales INT NULL);
-- insert zero values for each item so that they always show up in final report
INSERT INTO #tmp_ItemMove 
SELECT OBJ.F01, OBJ.F155, OBJ.F29, 0
FROM OBJ_TAB OBJ
WHERE OBJ.F01 LIKE '' OR
OBJ.F01 LIKE '' OR
OBJ.F01 LIKE '' OR
OBJ.F01 LIKE '' OR
OBJ.F01 LIKE '';
-- insert sales information into temp table
INSERT INTO #tmp_ItemMove
SELECT OBJ.F01, OBJ.F155, OBJ.F29, 
CASE WHEN TRS.F67=0 THEN TRS.F64 ELSE TRS.F67 END
FROM
RPT_ITM_D TRS JOIN OBJ_TAB OBJ ON TRS.F01=OBJ.F01
JOIN TLZ_TAB TLZ ON TRS.F1034=TLZ.F1034
JOIN LNK_TAB LNK ON TRS.F1056=LNK.F1056 AND TRS.F1057=LNK.F1057 AND LNK.F1000=@Location
WHERE (OBJ.F01 LIKE '' OR
OBJ.F01 LIKE '' OR
OBJ.F01 LIKE '' OR
OBJ.F01 LIKE '' OR
OBJ.F01 LIKE '') AND
TRS.F254 >= @StartDate AND
TRS.F1034 BETWEEN 3 AND 4;
-- get final report
-- sum sales history for each item and display
SELECT MAX(UPC),
MAX(Brand),
MAX(Descriptor),
SUM(Sales)
FROM #tmp_ItemMove
GROUP BY UPC, Brand, Descriptor
ORDER BY SUM(Sales) DESC, Brand, UPC;
DROP TABLE #tmp_ItemMove;

任何帮助表示赞赏,谢谢。

4

1 回答 1

0

您是否尝试在应用程序中运行查询时运行跟踪?运行将显示所有错误和异常的跟踪。这应该向您显示确切的故障点。如果有很多交易正在进行,您将需要应用一些过滤器。这可能会有所帮助:http ://www.bbdevnetwork.com/blogs/debugging-t-sql-and-sql-profiler/但请确保打开错误。

这些表是否具有适当的权限?如果您将其粘贴到 SSMS 窗口中,您可能在该窗口中以 dbo 或 sa 身份运行,这将允许它在 excel 中的用户可能具有受限权限时运行。

你真的应该把那个查询变成一个存储过程并执行它,因为运行这样一个查询可能有几个失败点,而且许多驱动程序不会为你提供任何有用的错误信息。

于 2012-08-28T17:04:31.413 回答