本文介绍了SQSERVER 2008 EXP中的动态WHERE子句.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的存储过程中生成动态的where子句.

=== @population nvarchar(max)

declare    @chvSelect varchar(max),
             @chvFrom varchar(max),
             @chvWhere varchar(max),
             @chvOrderby varchar(max),
             @chvSQL varchar(max)
BEGIN


--if @Population is not null

set @chvSelect=''SELECT Localities.Name,FormFields.FieldName, FormInstanceData.Value''
set @chvFrom='' FROM   FormInstances INNER JOIN
                      FormInstanceData ON FormInstances.Id = FormInstanceData.FormInstanceId INNER JOIN
                      Communities INNER JOIN
                      Localities ON Communities.Id = Localities.Id INNER JOIN
                      WCData ON Localities.Id = WCData.LocalityId ON FormInstances.Id = WCData.FormInstanceId INNER JOIN
                      FormFields ON FormInstanceData.FieldId = FormFields.Id ''
set @chvWhere='' WHERE FormFields.FieldName ='' + @Population
--if @Population is not null

--set @chvWhere=@chvWhere + ''FormFields.FieldName ='' + CONVERT(varchar,@Population )
set @chvSQL = @chvSelect+ @chvFrom + @chvWhere
select @chvSQL Query
  --if @debug = 0
        exec (@chvSQL)
  --   else
   --    select @chvSQL

END


当我执行此操作时,我得到无效的列名(``我提供给@population的任何值''实际上是formfields .fieldname是该列,我提供的值应该是该列包含的值.) h2_lin>解决方案


看起来错误在这里...查询格式不正确.您有多个没有联接条件的背靠背的INNER JOINS.

此外,您如何知道WHERE子句中的无效列错误.出于调试目的(捕获错误行),建议您使用打印每组设置的行以形成查询.使用打印输出,将其连接起来,然后检查它到底在哪里显示出错误(如果您可以完全通过裸眼看!)




i am trying to generate a dynamic where clause in my store proc.

=== @population nvarchar(max)

declare    @chvSelect varchar(max),
             @chvFrom varchar(max),
             @chvWhere varchar(max),
             @chvOrderby varchar(max),
             @chvSQL varchar(max)
BEGIN


--if @Population is not null

set @chvSelect=''SELECT Localities.Name,FormFields.FieldName, FormInstanceData.Value''
set @chvFrom='' FROM   FormInstances INNER JOIN
                      FormInstanceData ON FormInstances.Id = FormInstanceData.FormInstanceId INNER JOIN
                      Communities INNER JOIN
                      Localities ON Communities.Id = Localities.Id INNER JOIN
                      WCData ON Localities.Id = WCData.LocalityId ON FormInstances.Id = WCData.FormInstanceId INNER JOIN
                      FormFields ON FormInstanceData.FieldId = FormFields.Id ''
set @chvWhere='' WHERE FormFields.FieldName ='' + @Population
--if @Population is not null

--set @chvWhere=@chvWhere + ''FormFields.FieldName ='' + CONVERT(varchar,@Population )
set @chvSQL = @chvSelect+ @chvFrom + @chvWhere
select @chvSQL Query
  --if @debug = 0
        exec (@chvSQL)
  --   else
   --    select @chvSQL

END


when i exec this i get invalid Column name (''whatever value i supplied to the @population'' actually the formfields .fieldname is the column and the values i supply are supposes be values contained by that column.)

解决方案


Looks like error is here... Query is not properly formed. You have multiple INNER JOINS back to back without the join condition.

Furhter, how do you know the the invalid column error is in WHERE clause. For debug purpose (to catch the error line) Would suggest you to use print each line set by you for forming query. Use the print outputs, concatenate it and check where exactly it shows you wrong (if you can get through naked eyes well enough!)




这篇关于SQSERVER 2008 EXP中的动态WHERE子句.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 02:59