VBA列限制中的SQL命令

VBA列限制中的SQL命令,sql,sql-server,excel,vba,Sql,Sql Server,Excel,Vba,在VBA代码中,一个sqlcommand中可以引入多少列有限制吗?我的代码相当长,但用于确保我始终从正确的数据库中提取数据。用户在数据库中选择后,m2mdata02将替换为代码目录M2M。要移交的列总数是7个表中的23列 Sqlcommand = “select left(m2mdata02.dbo.jomast.fjobno,5) as job, m2mdata02.dbo.jomast.fjobno, m2mdata02.dbo.jomast.fpartno, m2mdata02.dbo.j

在VBA代码中,一个sqlcommand中可以引入多少列有限制吗?我的代码相当长,但用于确保我始终从正确的数据库中提取数据。用户在数据库中选择后,m2mdata02将替换为代码目录M2M。要移交的列总数是7个表中的23列

Sqlcommand = “select left(m2mdata02.dbo.jomast.fjobno,5) as job, m2mdata02.dbo.jomast.fjobno, m2mdata02.dbo.jomast.fpartno, m2mdata02.dbo.jomast.fstatus, m2mdata02.dbo.jomast.fact_rel, m2mdata02.dbo.jomast.fddue_date, m2mdata02.dbo.jomast.fprodcl, m2mdata02.dbo.jomast.frel_dt, m2mdata02.dbo.jomast.frouting, m2mdata02.dbo.inmastx.fpartno, m2mdata02.dbo.inmastx.fdescript, m2mdata02.dbo.inmastx.fprice, m2mdata02.dbo.somast.fsono, m2mdata02.dbo.somast.fcompany, m2mdata02.dbo.somast.fcustpono, m2mdata02.dbo.aritem.fcinvoice, m2mdata02.dbo.aritem.fprice, m2mdata02.dbo.aritem.fcsono, m2mdata02.dbo.armast.fcinvoice, m2mdata02.dbo.armast.finvdate, m2mdata02.dbo.shmast.fshipdate, m2mdata02.dbo.shmast.fshipno, m2mdata02.dbo.shmast.fcsono
from m2mdata02.dbo.jomast
left join m2mdata02.dbo.inmastx on m2mdata02.dbo.inmastx.fpartno = m2mdata02.dbo.jomast.fpartno
left join m2mdata02.dbo.somast on m2mdata02.dbo.somast.fsono = m2mdata02.dbo.jomast.fsono 
left join m2mdata02.dbo.aritem on m2mdata02.dbo.aritem.FCSONO = m2mdata02.dbo.jomast.fsono
left join m2mdata02.dbo.armast on m2mdata02.dbo.armast.fcinvoice = m2mdata02.dbo.aritem.fcinvoice
left join m2mdata02.dbo.shmast on m2mdata02.dbo.shmast.fcsono = m2mdata02.dbo.somast.fsono
where m2mdata02.dbo.jomast.fprodcl = 'FG11' order by m2mdata02.dbo.jomast.fjobno”
根据,VBA中字符串类型的字符限制约为20亿个2^31 Unicode字符。您的SQL查询没有接近该限制的地方。如果使用ADO将Excel VBA连接到数据库,这里的服务器引擎是SQL server,它决定了列限制,而23又是不可能的

但是,考虑使用表别名来减少字符,避免重复三周期标识符,并全面提高可读性甚至可维护性,因为您可以更方便地调整每个用户选择的表引用。 选择leftj.fjobno,5作为作业 ,j.fjobno ,j.fpartno ,j.fstatus ,j.fact_rel ,j.到期日 ,j.fprodcl ,j.frel_dt ,j.弗罗廷 ,i.fpartno ,i.fdescript ,i.F.价格 ,so.fsono ,so.fcompany ,so.fcustpono ,ari.fcinvoice ,ari.fprice ,ari.fcsono ,arm..FCF发票 ,手臂..finvdate ,sh.fshipdate ,sh.fshipno ,sh.fcsono 来自m2mdata02.dbo.jomast j 左连接m2mdata02.dbo.inmastx i on i.fpartno=j.fpartno 左连接m2mdata02.dbo.somast so on so.fsono=j.fsono 在ari.FCSONO=j.fsono上左连接m2mdata02.dbo.aritem ari 左连接m2mdata02.dbo.armast臂对臂..fcinvoice=ari.fcinvoice 在sh.fcsono=so.fsono上左连接m2mdata02.dbo.shmast sh 其中j.fprodcl='FG11' 由j.fjobno订购 顺便说一句,对于很长的查询,避免在VBA中使用行连接和双引号构建字符串,并直接从格式化的SQL文本文件读取:

作为字符串的Dim strSQL '从文件读取SQL查询 使用CreateObjectScripting.FileSystemObject strSQL=.OpenTextFileC:\path\to\my\SQL\Query.SQL,1.readall 以 '替换字符串中的DB strSQL=ReplacestrSQL,m2mdata02,someotherdb 根据,VBA中字符串类型的字符限制约为20亿个2^31 Unicode字符。您的SQL查询没有接近该限制的地方。如果使用ADO将Excel VBA连接到数据库,这里的服务器引擎是SQL server,它决定了列限制,而23又是不可能的

但是,考虑使用表别名来减少字符,避免重复三周期标识符,并全面提高可读性甚至可维护性,因为您可以更方便地调整每个用户选择的表引用。 选择leftj.fjobno,5作为作业 ,j.fjobno ,j.fpartno ,j.fstatus ,j.fact_rel ,j.到期日 ,j.fprodcl ,j.frel_dt ,j.弗罗廷 ,i.fpartno ,i.fdescript ,i.F.价格 ,so.fsono ,so.fcompany ,so.fcustpono ,ari.fcinvoice ,ari.fprice ,ari.fcsono ,arm..FCF发票 ,手臂..finvdate ,sh.fshipdate ,sh.fshipno ,sh.fcsono 来自m2mdata02.dbo.jomast j 左连接m2mdata02.dbo.inmastx i on i.fpartno=j.fpartno 左连接m2mdata02.dbo.somast so on so.fsono=j.fsono 在ari.FCSONO=j.fsono上左连接m2mdata02.dbo.aritem ari 左连接m2mdata02.dbo.armast臂对臂..fcinvoice=ari.fcinvoice 在sh.fcsono=so.fsono上左连接m2mdata02.dbo.shmast sh 其中j.fprodcl='FG11' 由j.fjobno订购 顺便说一句,对于很长的查询,避免在VBA中使用行连接和双引号构建字符串,并直接从格式化的SQL文本文件读取:

作为字符串的Dim strSQL '从文件读取SQL查询 使用CreateObjectScripting.FileSystemObject strSQL=.OpenTextFileC:\path\to\my\SQL\Query.SQL,1.readall 以 '替换字符串中的DB strSQL=ReplacestrSQL,m2mdata02,someotherdb
SQL Server中每次选择的最大列数为4096。我怀疑VBA中是否存在实际限制。您这样问是因为您遇到了问题吗?SQL Server中每个SELECT的最大列数为4096。我怀疑VBA有实际的局限性。你问这个问题是因为你有问题吗?非常好。我会考虑创建一个视图。与文本文件方法类似,我将把查询存储在专用用户表单的文本框中。这允许我像m2mdata02Query=QueryForm.M2mdata02.Value一样访问它们。使用a也大大提高了可读性。如何将数据库设置为动态数据库?我正在调用一个名为CatalogM2M的公用程序,它是根据用户的输入设置的。用户将选择一种模式,将我的SQL的其余部分定向到特定的数据库。您是否看到底部VBA代码块中的替换行?让用户输入替换其他数据库。我正在代码块中使用该行,但希望引用
将我建立的目录M2M dim作为数据库名称。用户将不知道要引用的正确数据库。我已经尝试了一些还不起作用的事情。你能提出一个新的问题,并提供清楚的、必要的细节和信息吗?不要让未来的读者对标题和内容感到困惑。要点很好。我会考虑创建一个视图。与文本文件方法类似,我将把查询存储在专用用户表单的文本框中。这允许我像m2mdata02Query=QueryForm.M2mdata02.Value一样访问它们。使用a也大大提高了可读性。如何将数据库设置为动态数据库?我正在调用一个名为CatalogM2M的公用程序,它是根据用户的输入设置的。用户将选择一种模式,将我的SQL的其余部分定向到特定的数据库。您是否看到底部VBA代码块中的替换行?让用户输入替换其他数据库。我正在使用代码块中的行,但希望引用已建立的catalogm2m dim作为数据库名称。用户将不知道要引用的正确数据库。我已经尝试了一些还不起作用的事情。你能提出一个新的问题,并提供清楚的、必要的细节和信息吗?不要让未来的读者对标题和内容感到困惑。