因此,正如标题所述,我尝试做的是在MS Excel visual basic中我试图在visual basic中编写一个连接到数据库的SQL查询,并且有多个单元格作为参数。现在我专注于只是让查询返回未经过滤的结果。 我找到了一个非常基本的例子......
Awesome query that a fellow stackoverflow member made
我直接复制了他遇到问题的代码并使用我的数据库和一个非常简单的查询对其进行了测试。这一切都很好,花花公子。
我遇到的问题是我需要使用一些更复杂的SQL查询,而且我不太清楚VB中的格式应该如何。
下面列出了我需要运行的SQL查询。我使用的数据库也不是很友好。当我尝试将整个查询放入SQL字符串时,它给出了一个错误,只是说SQL语法错误。当我去调试时,它显示.Refresh行上的错误。我意识到这个查询非常大,所以我将其修剪下来并在VB中创建了多行,每个句子周围都有双引号,并将它们与&组合在一起。 _。 (见最底部)
有人有任何建议吗?
如果有人想要完整的代码,请告诉我,但它与我上面发布的链接非常相似,只是ODBC连接信息不同。
先谢谢。
SELECT DISTINCT S.SAMPLENAME, S.SAMPLE_DESCRIPTION, B.BATCH_NAME, PR.PROC_NAME, S.SAMPLESTATE, S.SAMPLE_QTY,
(SELECT MMNT_NAME FROM apexalpha.CI_AAS_MEASUREMENTUNIT WHERE (MEASUREMENTUNITID = S.SAMPLE_QTY_MEASUREMENTUNITID)) AS SAMP_MMNT_NAME, ACQP_CTABULAR.ASTIME, S.SAMPLE_DATE,
ACQP_CTABULAR.EREAL, ACQP_CTABULAR.ELIVE, TRACER.CERT_NAME AS TRACER_CERT_NAME, S.TRACER_QTYFROM apexalpha.CI_AAD_BATCH AS B INNER JOIN
apexalpha.CI_AAD_SAMPLE AS S ON S.BATCHID = B.BATCHID LEFT OUTER JOIN
apexalpha.CI_AAS_PROCEDURE AS PR ON B.PROCEDUREID = PR.PROCEDUREID AND B.REVISION = PR.REVISION LEFT OUTER JOIN
apexalpha.CI_AAD_SAMPLE_ANALYSIS AS A ON S.APPROVED_ANALYSISID = A.SAMPLE_ANALYSISID LEFT OUTER JOIN
apexalpha.CI_AAR_CAM_ACQP1_RECORD AS ACQP1 ON A.SAMPLE_ANALYSISID = ACQP1.SAMPLE_ANALYSISID LEFT OUTER JOIN
apexalpha.CI_AAR_CAM_PROC1_COMMON AS PROC1 ON A.SAMPLE_ANALYSISID = PROC1.SAMPLE_ANALYSISID LEFT OUTER JOIN
apexalpha.CI_AAR_CAM_GEOM_RECORD AS GEOM ON A.SAMPLE_ANALYSISID = GEOM.SAMPLE_ANALYSISID LEFT OUTER JOIN
apexalpha.CI_AAR_CAM_ACQP_CTABULAR AS ACQP_CTABULAR ON A.SAMPLE_ANALYSISID = ACQP_CTABULAR.SAMPLE_ANALYSISID LEFT OUTER JOIN
apexalpha.CI_AAD_SAMPLE_RUN AS RUN ON RUN.CURRENT_ANALYSISID = S.APPROVED_ANALYSISID LEFT OUTER JOIN
apexalpha.CI_AAS_CHAMBER AS C ON RUN.CHAMBERID = C.CHAMBERID LEFT OUTER JOIN
apexalpha.CI_AAR_CAM_ANALCNTL1_COMMON AS ANCNTL ON A.SAMPLE_ANALYSISID = ANCNTL.SAMPLE_ANALYSISID LEFT OUTER JOIN
apexalpha.CI_AAR_CAM_ANALCNTL2_COMMON AS ANCNTL2 ON A.SAMPLE_ANALYSISID = ANCNTL2.SAMPLE_ANALYSISID LEFT OUTER JOIN
apexalpha.CI_AAR_CAM_NUCL_RTABULAR AS NUCL_TAB ON A.SAMPLE_ANALYSISID = NUCL_TAB.SAMPLE_ANALYSISID LEFT OUTER JOIN
apexalpha.CI_AAR_CAM_NUCL_COMMON AS NUCL ON A.SAMPLE_ANALYSISID = NUCL.SAMPLE_ANALYSISID LEFT OUTER JOIN
apexalpha.CI_AAR_CAM_NUCL_RECORD AS NUCL_REC ON A.SAMPLE_ANALYSISID = NUCL_REC.SAMPLE_ANALYSISID AND
NUCL_REC.CAMRECORD = NUCL_TAB.CAMRECORD LEFT OUTER JOIN
apexalpha.CI_AAR_CAM_PEAK_RECORD AS PEAK ON A.SAMPLE_ANALYSISID = PEAK.SAMPLE_ANALYSISID AND
NUCL_TAB.NLPEAK = PEAK.CAMRECORD LEFT OUTER JOIN
apexalpha.CI_AAR_CAM_PEAK_COMMON AS PEAK_COMMON ON A.SAMPLE_ANALYSISID = PEAK_COMMON.SAMPLE_ANALYSISID LEFT OUTER JOIN
apexalpha.CI_AAS_ELEMENT_REV AS EL_REV ON PR.ELEMENTID = EL_REV.ELEMENTID AND B.ELEMENT_REVID = EL_REV.ELEMENT_REVID LEFT OUTER JOIN
apexalpha.CI_AAS_CERTIFICATE AS TRACER ON EL_REV.TRACER_CERTIFICATEID = TRACER.CERTIFICATEID LEFT OUTER JOIN
apexalpha.CI_AAS_CERTIFICATE AS CONTROL ON EL_REV.CONTROL_CERTIFICATEID = CONTROL.CERTIFICATEID LEFT OUTER JOIN
dbo.CI_SHL_USER AS APPUSR1 ON S.APPROVAL1_USERID = APPUSR1.USERID LEFT OUTER JOIN
dbo.CI_SHL_USER AS APPUSR2 ON S.APPROVAL2_USERID = APPUSR2.USERID LEFT OUTER JOIN
apexalpha.CI_AAR_CAM_SPECIAL_RECORD AS SPEC_REC2 ON A.SAMPLE_ANALYSISID = SPEC_REC2.SAMPLE_ANALYSISID AND
SPEC_REC2.CAMRECORD = 2 LEFT OUTER JOIN
apexalpha.CI_AAR_CAM_SPECIAL_RECORD AS SPEC_REC3 ON A.SAMPLE_ANALYSISID = SPEC_REC3.SAMPLE_ANALYSISID AND
SPEC_REC3.CAMRECORD = 3 LEFT OUTER JOIN
apexalpha.CI_AAR_CAM_SAMP_COMMON AS SAMP_COM ON A.SAMPLE_ANALYSISID=SAMP_COM.SAMPLE_ANALYSISID
WHERE (S.SAMPLE_DESCRIPTION = '2014-246')
这是VB中的SQL代码块..
'--build SQL statement
sSQL = "SELECT DISTINCT S.SAMPLENAME, S.SAMPLE_DESCRIPTION, B.BATCH_NAME, PR.PROC_NAME, S.SAMPLESTATE, S.SAMPLE_QTY, (SELECT MMNT_NAME FROM apexalpha.CI_AAS_MEASUREMENTUNIT WHERE (MEASUREMENTUNITID = S.SAMPLE_QTY_MEASUREMENTUNITID)) AS SAMP_MMNT_NAME, ACQP_CTABULAR.ASTIME, S.SAMPLE_DATE, ACQP_CTABULAR.EREAL, ACQP_CTABULAR.ELIVE, TRACER.CERT_NAME AS TRACER_CERT_NAME, S.TRACER_QTY " & _
"FROM apexalpha.CI_AAD_BATCH AS B INNER JOIN apexalpha.CI_AAD_SAMPLE AS S ON S.BATCHID = B.BATCHID LEFT OUTER JOIN apexalpha.CI_AAS_PROCEDURE AS PR ON B.PROCEDUREID = PR.PROCEDUREID AND B.REVISION = PR.REVISION " & _
"LEFT OUTER JOIN apexalpha.CI_AAD_SAMPLE_ANALYSIS AS A ON S.APPROVED_ANALYSISID = A.SAMPLE_ANALYSISID LEFT OUTER JOIN apexalpha.CI_AAR_CAM_ACQP1_RECORD AS ACQP1 ON A.SAMPLE_ANALYSISID = ACQP1.SAMPLE_ANALYSISID LEFT OUTER JOIN apexalpha.CI_AAR_CAM_PROC1_COMMON AS PROC1 ON A.SAMPLE_ANALYSISID = PROC1.SAMPLE_ANALYSISID LEFT OUTER JOIN apexalpha.CI_AAR_CAM_GEOM_RECORD AS GEOM ON A.SAMPLE_ANALYSISID = GEOM.SAMPLE_ANALYSISID " & _
"LEFT OUTER JOIN apexalpha.CI_AAR_CAM_ACQP_CTABULAR AS ACQP_CTABULAR ON A.SAMPLE_ANALYSISID = ACQP_CTABULAR.SAMPLE_ANALYSISID LEFT OUTER JOIN apexalpha.CI_AAD_SAMPLE_RUN AS RUN ON RUN.CURRENT_ANALYSISID = S.APPROVED_ANALYSISID LEFT OUTER JOIN apexalpha.CI_AAS_CHAMBER AS C ON RUN.CHAMBERID = C.CHAMBERID LEFT OUTER JOIN apexalpha.CI_AAR_CAM_ANALCNTL1_COMMON AS ANCNTL ON A.SAMPLE_ANALYSISID = ANCNTL.SAMPLE_ANALYSISID " & _
"LEFT OUTER JOIN apexalpha.CI_AAR_CAM_ANALCNTL2_COMMON AS ANCNTL2 ON A.SAMPLE_ANALYSISID = ANCNTL2.SAMPLE_ANALYSISID LEFT OUTER JOIN apexalpha.CI_AAR_CAM_NUCL_RTABULAR AS NUCL_TAB ON A.SAMPLE_ANALYSISID = NUCL_TAB.SAMPLE_ANALYSISID LEFT OUTER JOIN apexalpha.CI_AAR_CAM_NUCL_COMMON AS NUCL ON A.SAMPLE_ANALYSISID = NUCL.SAMPLE_ANALYSISID " & _
"LEFT OUTER JOIN apexalpha.CI_AAR_CAM_NUCL_RECORD AS NUCL_REC ON A.SAMPLE_ANALYSISID = NUCL_REC.SAMPLE_ANALYSISID AND NUCL_REC.CAMRECORD = NUCL_TAB.CAMRECORD LEFT OUTER JOIN apexalpha.CI_AAR_CAM_PEAK_RECORD AS PEAK ON A.SAMPLE_ANALYSISID = PEAK.SAMPLE_ANALYSISID AND NUCL_TAB.NLPEAK = PEAK.CAMRECORD LEFT OUTER JOIN apexalpha.CI_AAR_CAM_PEAK_COMMON AS PEAK_COMMON ON A.SAMPLE_ANALYSISID = PEAK_COMMON.SAMPLE_ANALYSISID LEFT OUTER JOIN apexalpha.CI_AAS_ELEMENT_REV AS EL_REV ON PR.ELEMENTID = EL_REV.ELEMENTID AND B.ELEMENT_REVID = EL_REV.ELEMENT_REVID " & _
"LEFT OUTER JOIN apexalpha.CI_AAS_CERTIFICATE AS TRACER ON EL_REV.TRACER_CERTIFICATEID = TRACER.CERTIFICATEID LEFT OUTER JOIN apexalpha.CI_AAS_CERTIFICATE AS CONTROL ON EL_REV.CONTROL_CERTIFICATEID = CONTROL.CERTIFICATEID LEFT OUTER JOIN dbo.CI_SHL_USER AS APPUSR1 ON S.APPROVAL1_USERID = APPUSR1.USERID LEFT OUTER JOIN dbo.CI_SHL_USER AS APPUSR2 ON S.APPROVAL2_USERID = APPUSR2.USERID LEFT OUTER JOIN apexalpha.CI_AAR_CAM_SPECIAL_RECORD AS SPEC_REC2 ON A.SAMPLE_ANALYSISID = SPEC_REC2.SAMPLE_ANALYSISID AND SPEC_REC2.CAMRECORD = 2 " & _
"LEFT OUTER JOIN apexalpha.CI_AAR_CAM_SPECIAL_RECORD AS SPEC_REC3 ON A.SAMPLE_ANALYSISID = SPEC_REC3.SAMPLE_ANALYSISID AND SPEC_REC3.CAMRECORD = 3 LEFT OUTER JOIN apexalpha.CI_AAR_CAM_SAMP_COMMON AS SAMP_COM ON A.SAMPLE_ANALYSISID = SAMP_COM.SAMPLE_ANALYSISID " & _
" WHERE SAMPLE_DESCRIPTION = ?;"
答案 0 :(得分:0)
我记得有一些限制你可以通过&和_。这是约28行。
解决方案是创建两个字符串变量,分配它们的值,记住行的限制并生成查询字符串query = str1 & str2
。 GL!