高分求解:如何从一个SQL语句中提取出表示字段的字符串 例如,select * from table where (code = '001' and flag = 1)只需要考虑where后面的串,即输入类似“(code = '001' and flag = 1)”的表达式,需要输出code和flag,这两个显然被认为是字段。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个问题其实很麻烦,可以给你一个简单的实现。首先定义一大堆分隔符,如:- + = / * <> >= OR AND等然后用这些分隔符切分WHERE字符串(正则)得到这样的数据:code 、 '001' 、 flag 、 1然后剔除数字和字符串即得到可能的字段列表:code 、 flag 对于这个,你可以将你的sql语句传入存储过程,用存储过程来判断处理。因为在存储过程中,除了能够找到表名,除了运算符以及数字以外的所有字段,你还可以通过select来判断该字段是否在表中。注意,这个存储过程一定要和你操作的表在同一库中,否则存储过程做不了。 有个办法,你把sql执行结果放到一个table里面,所有的字段名都可以得到,然后你想怎么着就怎么着 这样的Sql链接拼接查询,在程序中使用if 或 switch 拼接做好, /*备注:这个过程很粗糙,1。没有去掉重复列名,2。对于列名表名的有效性判断还须深化,才能够满足所有的需要,3。去除关键字部分,还有待完善就当提个方法吧。测试语句:USP_StriptColumnName 'select * from lg_in_m m,lg_in_s s where m.bill=s.bill and ware_code=1000'测试结果:billbillware_code*/drop proc USP_StriptColumnNamegocreate proc USP_StriptColumnName@sql varchar(8000)asbegindeclare @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 = ''''+@splif(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 C# winform 向Excel中插入图片问题 想做一个这样的界面,求类似源码 关于在WINDOW域用户下使用Process.Start GPS通讯接收问题 获得鼠标点击控件的名称 VS2010专业版,怎么没有安装部署项目 求助:如何判断datatable中某一列有空值? 急救啊,关于存储过程 现在大学里的生活费每月一般是多少? 难!急!object变量的再生成问题! (winform)为什么点在DataGrid的Header以及表格外的边框地带,DataGrid的CurrentCell都是Cell[0,0]啊 高分求教:怎样替控件增加一个MouseDown和MouseUp事件?
然后用这些分隔符切分WHERE字符串(正则)得到这样的数据:code 、 '001' 、 flag 、 1然后剔除数字和字符串即得到可能的字段列表:
code 、 flag
因为在存储过程中,除了能够找到表名,除了运算符以及数字以外的所有字段,你还可以通过select来判断该字段是否在表中。
注意,这个存储过程一定要和你操作的表在同一库中,否则存储过程做不了。
在程序中使用if 或 switch 拼接做好,
备注:
这个过程很粗糙,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