例如,select * from table where (code = '001' and flag = 1)只需要考虑where后面的串,即输入类似“(code = '001' and flag = 1)”的表达式,需要输出code和flag,这两个显然被认为是字段。

解决方案 »

  1.   

    这个问题其实很麻烦,可以给你一个简单的实现。首先定义一大堆分隔符,如:- + = / * <> >= OR AND等
    然后用这些分隔符切分WHERE字符串(正则)得到这样的数据:code 、 '001' 、 flag 、 1然后剔除数字和字符串即得到可能的字段列表:
    code 、 flag
      

  2.   

    对于这个,你可以将你的sql语句传入存储过程,用存储过程来判断处理。
    因为在存储过程中,除了能够找到表名,除了运算符以及数字以外的所有字段,你还可以通过select来判断该字段是否在表中。
    注意,这个存储过程一定要和你操作的表在同一库中,否则存储过程做不了。
      

  3.   

    有个办法,你把sql执行结果放到一个table里面,所有的字段名都可以得到,然后你想怎么着就怎么着
      

  4.   

    这样的Sql链接拼接查询,
    在程序中使用if 或 switch 拼接做好,
      

  5.   

    /*
    备注:
    这个过程很粗糙,1。没有去掉重复列名,2。对于列名表名的有效性判断还须深化,才能够满足所有的需要,3。去除关键字部分,还有待完善
    就当提个方法吧。
    测试语句:
    USP_StriptColumnName 'select * from lg_in_m m,lg_in_s s where m.bill=s.bill and ware_code=1000'测试结果:
    bill
    bill
    ware_code*/drop proc USP_StriptColumnName
    gocreate proc USP_StriptColumnName
    @sql varchar(8000)
    as
    begin
    declare @spl varchar(8000),
    @ret varchar(8000)select @spl = replace(@sql,'select',',')
    select @spl = replace(@spl,'*',',')
    select @spl = replace(@spl,'from',',')
    select @spl = replace(@spl,'where',',')
    select @spl = replace(@spl,'and',',')
    select @spl = replace(@spl,'union',',')
    select @spl = replace(@spl,'all',',')
    select @spl = replace(@spl,'case',',')
    select @spl = replace(@spl,'when',',')
    select @spl = replace(@spl,'end',',')
    select @spl = replace(@spl,'as',',')
    select @spl = replace(@spl,'order',',')
    select @spl = replace(@spl,'by',',')
    select @spl = replace(@spl,'',',')
    select @spl = replace(@spl,'(',',')
    select @spl = replace(@spl,')',',')
    select @spl = replace(@spl,'table',',')
    select @spl = replace(@spl,'on',',')
    select @spl = replace(@spl,'.',',')
    select @spl = replace(@spl,'=',',')
    select @spl = replace(@spl,'*=',',')
    select @spl = replace(@spl,'=*',',')
    select @spl = replace(@spl,'>',',')
    select @spl = replace(@spl,'>=',',')
    select @spl = replace(@spl,'<',',')
    select @spl = replace(@spl,'<=',',')
    select @spl = replace(@spl,'<>',',')
    select @spl = replace(@spl,'+',',')
    select @spl = replace(@spl,'-',',')
    select @spl = replace(@spl,'asc',',')
    select @spl = replace(@spl,'desc',',')
    select @spl = replace(@spl,' ',',')declare @fc varchar(1)
    select @fc = left(@spl,1)
    while @fc <> ','
    begin
    select @spl = substring(@spl,2,len(@spl)-1)
    select @fc = left(@spl,1)
    endselect @spl = replace(@spl,',',''' union all select ''')if(left(@spl,1)<>'''')
    select @spl = ''''+@spl
    if(right(@spl,1)<>'''')
    select @spl = @spl+''''create table #tb(objectname varchar(100))declare @exec varchar(8000)
    select @exec = ''
    select @exec = 'insert into #tb select '''
    select @exec = @exec+@splexec(@exec)delete from #tb where objectname='' or objectname not in(select name from syscolumns)
    --去除重复的,没做。
    select * from #tbdrop table #tbendgo