关于动态where子句

时间:2012-01-04 08:51:14

标签: sql-server-2005 tsql

这是我的SQL:

DECLARE @tbl_WhereClause AS TABLE (
    SearchField VARCHAR(255),
    Operator VARCHAR(25),
    ConditionData VARCHAR(MAX),
    MatchCase BIT,
    TableName VARCHAR(MAX)   
)

DECLARE @WhereClause_XML XML
SET @WhereClause_XML = 
'<NewDataSet> 
  <param>
   <SearchField>EmployeeID</SearchField>
   <FilterCondition> >= </FilterCondition>
   <ConditionData>201</ConditionData>
   <MatchCase>0</MatchCase>
   <Table>Employee</Table>
 </param>
 <param>  
   <SearchField>DeptID</SearchField>
   <FilterCondition> = </FilterCondition>
   <ConditionData>AC01</ConditionData>
   <MatchCase>1</MatchCase>
   <Table>Department</Table>
 </param>
 <param>  
   <SearchField>Dob</SearchField>
   <FilterCondition> >= </FilterCondition>
   <ConditionData>20120104</ConditionData>
   <MatchCase>0</MatchCase>
   <Table>Employee</Table>
 </param>
</NewDataSet>'

INSERT INTO @tbl_WhereClause (SearchField, Operator, ConditionData,  MatchCase,TableName)
SELECT  A.B.value('(SearchField)[1]', 'VARCHAR(255)' ) SearchField,
    A.B.value('(FilterCondition)[1]', 'VARCHAR(25)' ) Operator,
    A.B.value('(ConditionData)[1]', 'VARCHAR(MAX)' ) ConditionData,
    A.B.value('(MatchCase)[1]', 'BIT' ) MatchCase,
    A.B.value('(Table)[1]', 'VARCHAR(MAX)' ) TableName
FROM    @WhereClause_XML.nodes('/NewDataSet/param') A(B)

SELECT * FROM @tbl_WhereClause

如何为每个字段加入系统表以获取每个字段数据类型并显示。请运行脚本并在SQL中进行必要的更改。感谢

2 个答案:

答案 0 :(得分:2)

尝试此操作以验证表和列,并使其类型具有其属性:

DECLARE @tbl_WhereClause AS TABLE (
SearchField VARCHAR(255),
Operator VARCHAR(25),
ConditionData VARCHAR(MAX),
MatchCase BIT,
TableName VARCHAR(MAX),
Validity VARCHAR(100),
ColumnType VARCHAR(128),
ColumnPrecision INT,
ColumnScale INT,
ColumnNullable bit
)

DECLARE @WhereClause_XML XML
SET @WhereClause_XML = '
<NewDataSet>
<param>
<SearchField>EmployeeID</SearchField>
<FilterCondition> >= </FilterCondition>
<ConditionData>201</ConditionData>
<MatchCase>0</MatchCase>
<Table>Employee</Table>
</param>
<param>  
<SearchField>DeptID</SearchField>
<FilterCondition> = </FilterCondition>
<ConditionData>AC01</ConditionData>
<MatchCase>1</MatchCase>
<Table>Department</Table>
</param>
<param>  
<SearchField>Dob</SearchField>
<FilterCondition> >= </FilterCondition>
<ConditionData>20120104</ConditionData>
<MatchCase>0</MatchCase>
<Table>Employee</Table>
</param>
</NewDataSet>'

INSERT INTO @tbl_WhereClause (SearchField, Operator, ConditionData,  MatchCase,TableName, Validity, ColumnType, ColumnPrecision, ColumnScale, ColumnNullable)
SELECT  A.B.value('(SearchField)[1]', 'VARCHAR(255)' ) SearchField,
    A.B.value('(FilterCondition)[1]', 'VARCHAR(25)' ) Operator,
    A.B.value('(ConditionData)[1]', 'VARCHAR(MAX)' ) ConditionData,
    A.B.value('(MatchCase)[1]', 'BIT' ) MatchCase,
    A.B.value('(Table)[1]', 'VARCHAR(MAX)' ) TableName,
    CASE WHEN t.NAME+c.NAME IS NULL THEN 'invalid' ELSE 'valid' END ,
    ty.NAME,
    c.PRECISION,
    c.Scale,
    c.Is_Nullable
FROM    @WhereClause_XML.nodes('/NewDataSet/param') A(B)
LEFT JOIN sys.tables t ON t.name = A.B.value('(Table)[1]', 'VARCHAR(MAX)' )
LEFT JOIN sys.COLUMNS c ON T.OBJECT_ID = c.OBJECT_ID AND c.name = A.B.value('(SearchField)[1]', 'VARCHAR(255)' )
LEFT JOIN sys.types ty ON c.system_type_id = ty.system_type_id 

SELECT * FROM @tbl_WhereClause

答案 1 :(得分:0)

这是完整的解决方案。

/*        
--XML DATA SAMPLE        
DECLARE @xmlvar xml      
SET @xmlvar='      
<NewDataSet>      
  <param>      
    <SearchField>JID</SearchField>      
    <FilterCondition>%</FilterCondition>      
    <ConditionData>4000</ConditionData>      
    <MatchCase>0</MatchCase>      
    <Table>BBAJobs</Table>      
  </param>      
  <param>      
    <SearchField>Specialist</SearchField>      
    <FilterCondition>=</FilterCondition>      
    <ConditionData>Nigel Graham</ConditionData>      
    <MatchCase>0</MatchCase>      
    <Table>BBAJobs</Table>      
  </param>      
</NewDataSet>'        
exec GenericSearch 'JobAdder','vwAdvSrch',@xmlvar,'AND'      
*/        

--For generic search        

ALTER PROCEDURE [dbo].[GenericSearch]        
(        
 @ModuleName VARCHAR(100),        
 @ViewName VARCHAR(100),        
 @WhereClause_XML XML,        
 @LogicalOperator VARCHAR(3)        
)        
AS        

BEGIN   
DECLARE @pop varchar(max)      
SET @pop='not true'        

DECLARE @SearchField VARCHAR(255),        
@Operator VARCHAR(25),        
@ConditionData VARCHAR(MAX),        
@MatchCase BIT,        
@TableName VARCHAR(MAX),        
@Validity VARCHAR(100),        
@ColumnType VARCHAR(128),        
@ColumnPrecision INT,        
@ColumnScale INT,        
@ColumnNullable bit,        
@WhereClause VARCHAR(MAX),        
@SQL NVARCHAR(MAX)        


DECLARE @tbl_WhereClause AS TABLE         
(        
 SearchField VARCHAR(255),        
 Operator VARCHAR(25),        
 ConditionData VARCHAR(MAX),        
 MatchCase BIT,        
 TableName VARCHAR(MAX),        
 Validity VARCHAR(100),        
 ColumnType VARCHAR(128),        
 ColumnPrecision INT,        
 ColumnScale INT,        
 ColumnNullable bit        
)        


INSERT INTO @tbl_WhereClause (SearchField, Operator, ConditionData,  MatchCase,TableName,         
Validity, ColumnType, ColumnPrecision, ColumnScale, ColumnNullable)        

SELECT  A.B.value('(SearchField)[1]', 'VARCHAR(255)' ) SearchField,        
    A.B.value('(FilterCondition)[1]', 'VARCHAR(25)' ) Operator,        
    A.B.value('(ConditionData)[1]', 'VARCHAR(MAX)' ) ConditionData,        
    A.B.value('(MatchCase)[1]', 'BIT' ) MatchCase,        
    A.B.value('(Table)[1]', 'VARCHAR(MAX)' ) TableName,        
    CASE WHEN t.NAME+c.NAME IS NULL THEN 'invalid' ELSE 'valid' END ,        
    ty.NAME,        
    c.PRECISION,        
    c.Scale,        
    c.Is_Nullable        
 FROM    @WhereClause_XML.nodes('/NewDataSet/param') A(B)        
 LEFT JOIN sys.tables t ON t.name = A.B.value('(Table)[1]', 'VARCHAR(MAX)' )        
 LEFT JOIN sys.COLUMNS c ON T.OBJECT_ID = c.OBJECT_ID AND c.name = A.B.value('(SearchField)[1]', 'VARCHAR(255)' )        
 LEFT JOIN sys.types ty ON c.system_type_id = ty.system_type_id         

SET @WhereClause= ' WHERE 1=1'        
SET @SQL = ''        
DECLARE SearchCursor CURSOR FOR         
SELECT * FROM @tbl_WhereClause        

OPEN SearchCursor        

FETCH NEXT FROM SearchCursor         
INTO @SearchField, @Operator,@ConditionData,@MatchCase,@TableName,@Validity,@ColumnType,@ColumnPrecision,@ColumnScale,@ColumnNullable        

WHILE @@FETCH_STATUS = 0        
BEGIN        

 IF CHARINDEX('INT', UPPER(@ColumnType)) > 0         
 BEGIN  
   IF RTRIM(LTRIM(@Operator)) = '%'        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+' LIKE '+'''%'+REPLACE(@ConditionData, '''', '''''') +'%'''        
   END        
   ELSE IF RTRIM(LTRIM(@Operator)) = 'S%'        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+' LIKE '+''''+REPLACE(@ConditionData, '''', '''''') +'%'''        
   END        
   ELSE IF RTRIM(LTRIM(@Operator)) = '%S'        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+' LIKE '+'''%'+REPLACE(@ConditionData, '''', '''''') +''''        
   END        
   ELSE      
   BEGIN      
 SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+@Operator+space(1)+@ConditionData        
   END      
 END  
 ---new 
 ELSE IF CHARINDEX('NTEXT', UPPER(@ColumnType)) > 0         
 BEGIN  
   IF RTRIM(LTRIM(@Operator)) = '%'        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + CAST(@SearchField as nvarchar(max)) + space(1)+' LIKE '+'''%'+REPLACE(@ConditionData, '''', '''''') +'%'''        
   END        
   ELSE IF RTRIM(LTRIM(@Operator)) = 'S%'        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + CAST(@SearchField as nvarchar(max)) + space(1)+' LIKE '+''''+REPLACE(@ConditionData, '''', '''''') +'%'''        
   END        
   ELSE IF RTRIM(LTRIM(@Operator)) = '%S'        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + CAST(@SearchField as nvarchar(max)) + space(1)+' LIKE '+'''%'+REPLACE(@ConditionData, '''', '''''') +''''        
   END        
   ELSE      
   BEGIN      
     SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + CAST(@SearchField as nvarchar(max)) + space(1)+@Operator+space(1)+@ConditionData        
   END      
 END        
  ELSE IF CHARINDEX('VARCHAR', UPPER(@ColumnType)) > 0         
 BEGIN  
   IF RTRIM(LTRIM(@Operator)) = '%'        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + CAST(@SearchField as varchar(max)) + space(1)+' LIKE '+'''%'+REPLACE(@ConditionData, '''', '''''') +'%'''        
   END        
   ELSE IF RTRIM(LTRIM(@Operator)) = 'S%'        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + CAST(@SearchField as varchar(max)) + space(1)+' LIKE '+''''+REPLACE(@ConditionData, '''', '''''') +'%'''        
   END        
   ELSE IF RTRIM(LTRIM(@Operator)) = '%S'        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + CAST(@SearchField as varchar(max)) + space(1)+' LIKE '+'''%'+REPLACE(@ConditionData, '''', '''''') +''''        
   END        
   ELSE      
   BEGIN      
     SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + CAST(@SearchField as varchar(max)) + space(1)+@Operator+space(1)+@ConditionData        
   END      
 END        
  ELSE IF CHARINDEX('NVARCHAR', UPPER(@ColumnType)) > 0         
 BEGIN  
   IF RTRIM(LTRIM(@Operator)) = '%'        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + CAST(@SearchField as varchar(max)) + space(1)+' LIKE '+'''%'+REPLACE(@ConditionData, '''', '''''') +'%'''        
   END        
   ELSE IF RTRIM(LTRIM(@Operator)) = 'S%'        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + CAST(@SearchField as varchar(max)) + space(1)+' LIKE '+''''+REPLACE(@ConditionData, '''', '''''') +'%'''        
   END        
   ELSE IF RTRIM(LTRIM(@Operator)) = '%S'        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + CAST(@SearchField as varchar(max)) + space(1)+' LIKE '+'''%'+REPLACE(@ConditionData, '''', '''''') +''''        
   END        
   ELSE      
   BEGIN      
     SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + CAST(@SearchField as varchar(max)) + space(1)+@Operator+space(1)+@ConditionData        
   END      
 END        
 ---new      
 ELSE IF CHARINDEX('NUMERIC', UPPER(@ColumnType)) > 0         
 BEGIN  
   IF RTRIM(LTRIM(@Operator)) = '%'        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+' LIKE '+'''%'+REPLACE(@ConditionData, '''', '''''') +'%'''        
   END        
   ELSE IF RTRIM(LTRIM(@Operator)) = 'S%'        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+' LIKE '+''''+REPLACE(@ConditionData, '''', '''''') +'%'''        
   END        
   ELSE IF RTRIM(LTRIM(@Operator)) = '%S'        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+' LIKE '+'''%'+REPLACE(@ConditionData, '''', '''''') +''''        
   END        
   ELSE      
   BEGIN      
     SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+@Operator+space(1)+@ConditionData        
   END      
 END        
 ELSE IF CHARINDEX('BIT', UPPER(@ColumnType)) > 0         
 BEGIN    
  IF CHARINDEX('TRUE', UPPER(@ConditionData)) > 0         
  BEGIN      
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+@Operator+space(1)+'1'        
  END      
  ELSE IF CHARINDEX('FALSE', UPPER(@ConditionData)) > 0         
  BEGIN      
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+@Operator+space(1)+'0'        
  END      
  ELSE      
  BEGIN      
   SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+@Operator+space(1)+@ConditionData        
     END      
 END        
 ELSE IF CHARINDEX('DECIMAL', UPPER(@ColumnType)) > 0  
 BEGIN       
   IF RTRIM(LTRIM(@Operator)) = '%'        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+' LIKE '+'''%'+REPLACE(@ConditionData, '''', '''''') +'%'''        
   END        
   ELSE IF RTRIM(LTRIM(@Operator)) = 'S%'        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+' LIKE '+''''+REPLACE(@ConditionData, '''', '''''') +'%'''        
   END        
   ELSE IF RTRIM(LTRIM(@Operator)) = '%S'        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+' LIKE '+'''%'+REPLACE(@ConditionData, '''', '''''') +''''        
   END        
   ELSE      
   BEGIN        
 SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+@Operator+space(1)+@ConditionData        
   END        
 END      
 ELSE IF CHARINDEX('FLOAT', UPPER(@ColumnType)) > 0         
 BEGIN        
   IF RTRIM(LTRIM(@Operator)) = '%'        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+' LIKE '+'''%'+REPLACE(@ConditionData, '''', '''''') +'%'''        
   END        
   ELSE IF RTRIM(LTRIM(@Operator)) = 'S%'        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+' LIKE '+''''+REPLACE(@ConditionData, '''', '''''') +'%'''        
   END        
   ELSE IF RTRIM(LTRIM(@Operator)) = '%S'        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+' LIKE '+'''%'+REPLACE(@ConditionData, '''', '''''') +''''        
   END        
   ELSE      
   BEGIN        
 SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+@Operator+space(1)+@ConditionData        
   END      
 END        
 ELSE IF CHARINDEX('REAL', UPPER(@ColumnType)) > 0         
 BEGIN 
   IF RTRIM(LTRIM(@Operator)) = '%'        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+' LIKE '+'''%'+REPLACE(@ConditionData, '''', '''''') +'%'''        
   END        
   ELSE IF RTRIM(LTRIM(@Operator)) = 'S%'        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+' LIKE '+''''+REPLACE(@ConditionData, '''', '''''') +'%'''        
   END        
   ELSE IF RTRIM(LTRIM(@Operator)) = '%S'        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+' LIKE '+'''%'+REPLACE(@ConditionData, '''', '''''') +''''        
   END        
   ELSE      
   BEGIN        
 SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+@Operator+space(1)+@ConditionData       
   END       
 END        
 ELSE IF CHARINDEX('MONEY', UPPER(@ColumnType)) > 0         
 BEGIN       
   IF RTRIM(LTRIM(@Operator)) = '%'        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+' LIKE '+'''%'+REPLACE(@ConditionData, '''', '''''') +'%'''        
   END        
   ELSE IF RTRIM(LTRIM(@Operator)) = 'S%'        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+' LIKE '+''''+REPLACE(@ConditionData, '''', '''''') +'%'''        
   END        
   ELSE IF RTRIM(LTRIM(@Operator)) = '%S'        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+' LIKE '+'''%'+REPLACE(@ConditionData, '''', '''''') +''''        
   END        
   ELSE      
   BEGIN        
 SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+@Operator+space(1)+@ConditionData        
   END      
 END        
 ELSE IF CHARINDEX('DATE', UPPER(@ColumnType)) > 0         
 BEGIN  
  SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + 'CONVERT(varchar,'+@SearchField+',112)' + space(1)+@Operator+'CONVERT(varchar,'''+@ConditionData+''',112)'        
 END        
 ELSE IF CHARINDEX('CHAR', UPPER(@ColumnType)) > 0         
 BEGIN       
  IF @MatchCase = 0        
  BEGIN        
   IF RTRIM(LTRIM(@Operator)) = '%'        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+' LIKE '+'''%'+REPLACE(@ConditionData, '''', '''''') +'%'''        
   END        
   ELSE IF RTRIM(LTRIM(@Operator)) = 'S%'        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+' LIKE '+''''+REPLACE(@ConditionData, '''', '''''') +'%'''        
   END        
   ELSE IF RTRIM(LTRIM(@Operator)) = '%S'        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+' LIKE '+'''%'+REPLACE(@ConditionData, '''', '''''') +''''        
   END        
   ELSE        
   BEGIN        
    SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + @SearchField + space(1)+@Operator+'''' + REPLACE(@ConditionData, '''', '''''') + ''''        
   END        
  END        
  ELSE        
  IF @MatchCase = 1        
  BEGIN        
   SET @WhereClause = @WhereClause + space(1)+ @LogicalOperator + space(1) + 'BINARY_CHECKSUM('+@SearchField+')' + space(1)+@Operator+'BINARY_CHECKSUM('''+ REPLACE(@ConditionData, '''', '''''') +''')'        
  END        
 END         

 FETCH NEXT FROM SearchCursor         
 INTO @SearchField, @Operator,@ConditionData,@MatchCase,@TableName,@Validity,@ColumnType,@ColumnPrecision,@ColumnScale,@ColumnNullable        
END        

CLOSE SearchCursor          
DEALLOCATE SearchCursor         

SELECT @SQL = @SQL + FieldName + ' as [' + FieldDesc + '], ' FROM SearchMaster WHERE ModuleName = @ModuleName        
SET @SQL = 'SELECT '+ left(@SQL, len(RTRIM(LTRIM(@SQL)))-1) + ' FROM '+ @ViewName        
SET @SQL = @SQL + @WhereClause     

--exec sp_executesql @SQL        
exec(@SQL)        
--select @SQL  
--PRINT @SQL        
END