昨天夜里的一个想法,请教写一个SQL语句,帮忙实现一种效果。(200分)

  • 主题发起人 本公子
  • 开始时间

本公子

Unregistered / Unconfirmed
GUEST, unregistred user!
源于昨天夜里的一个想法,需要写一个SQL语句来实现,但没有成功。<br>即:把表2中的一行数据,作为表1新增的一列显示出来。(行列的数目是对应的)<br><br>举例如下--------<br><br>表1内容:<br>&nbsp;ENFIELD &nbsp; &nbsp;CHNAME<br>&nbsp; A &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;序号1<br>&nbsp; B &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;序号2<br>&nbsp; C &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;序号3<br>&nbsp; D &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;序号4<br>&nbsp; E &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;序号5<br>&nbsp;... &nbsp; &nbsp; &nbsp; &nbsp; ...<br>&nbsp; <br>------------<br><br>表2内容:<br>&nbsp; &nbsp; A &nbsp; &nbsp; &nbsp;B &nbsp; &nbsp; &nbsp; C &nbsp; &nbsp; &nbsp; D &nbsp; &nbsp; E &nbsp; ...... &nbsp;<br>&nbsp; &nbsp;值1 &nbsp; &nbsp;值2 &nbsp; &nbsp; 值3 &nbsp; &nbsp; 值4 &nbsp; 值5 &nbsp;......<br><br>------------<br><br>要求SQL查询得到如下效果:把表2中的一行数据,作为表1新增的一列(VALUES)显示出来。<br><br>&nbsp;ENFIELD &nbsp; &nbsp;CHNAME &nbsp; &nbsp; &nbsp;VALUES<br>&nbsp; A &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;序号1 &nbsp; &nbsp; &nbsp; 值1<br>&nbsp; B &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;序号2 &nbsp; &nbsp; &nbsp; 值2<br>&nbsp; C &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;序号3 &nbsp; &nbsp; &nbsp; 值3<br>&nbsp; D &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;序号4 &nbsp; &nbsp; &nbsp; 值4<br>&nbsp; E &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;序号5 &nbsp; &nbsp; &nbsp; 值5<br>&nbsp;... &nbsp; &nbsp; &nbsp; &nbsp; ... &nbsp; &nbsp; &nbsp; &nbsp; ...<br><br>----------------------<br><br>在此向各位请教。 <br><br>希望最好用SQL_Server和 Oracle都能分别实现该功能。 多谢了!
 
用存储过程实现.遍历列名和值比较,取出value<br>要用到sp_exec...
 
我也想学习!我之前是跟据T2来循环读取T1的的值的!
 
我也来学习下,如果是对应的 就不用比较拉吧是吗??
 
用游标实现!
 
我也想学习!
 
declare @s varchar(8000)<br>set @s='select *, (select case ENFIELD'<br>select @s=@s + ' when '''+ENFIELD+''' then '+ENFIELD from t1<br><br>set @s=@s+' end from t2) [values] from t1'<br><br>exec(@s)
 
表2建立一个视图<br>crater view as V2<br>select 'A' as ENFIELD,A from 表2<br>union all<br>select 'B' as ENFIELD,B from 表2<br>union all<br>...<br>再通过该视图与A表关联。
 
我给你的代码是测试通过的,你试了没有?为什么不给个回话?不理不睬会打击别人回答问题的积极性。
 
我觉得楼上2种方法都可行
 
to kaida:<br>&nbsp; &nbsp;我刚睡醒。<br>&nbsp; &nbsp;测试初步通过。谢谢。<br>&nbsp; &nbsp;我现正在加where条件,还没有通过呢。<br>&nbsp; &nbsp;比如:<br>&nbsp; &nbsp;... where (ENFIELD='A' and VALUES='值1') or (ENFIELD='C' and VALUES='值3') &nbsp;<br><br>&nbsp; &nbsp;根据这个查询条件,能得到这样的结果: <br>&nbsp; &nbsp;------ <br>&nbsp; &nbsp;ENFIELD &nbsp; &nbsp; CHNAME &nbsp; &nbsp; &nbsp;VALUES <br>&nbsp; &nbsp; A &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;序号1 &nbsp; &nbsp; &nbsp; &nbsp; 值1 <br>&nbsp; &nbsp; C &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;序号3 &nbsp; &nbsp; &nbsp; &nbsp; 值3 <br>&nbsp; &nbsp;-----如何拼写这样的SQL呢? 谢谢。 <br><br>----------<br>我现在用的是SQL2000和Oracle9i。<br>我想问,在Oracle下,该如何拼写动态SQL呢??
 
加 where 条件:<br>declare @s varchar(8000)<br>set @s=';with t as (select *, (select case ENFIELD'<br>select @s=@s + ' when '''+ENFIELD+''' then '+ENFIELD from t1<br><br>set @s=@s+' end from t2) [values] from t1)'+<br>&nbsp; &nbsp; &nbsp; &nbsp;' select * from t where (ENFIELD=''A'' and [VALUES]=''值1'') or (ENFIELD=''C'' and [VALUES]=''值3'')'<br><br>exec(@s)<br><br>我没装 Oracle9i,无法在 Oracle9i 上测试。
 
to kaida:<br>加条件这个写法,我在CSDN上也问你了,会出现一个问题,如下描述:<br>&nbsp;set @s=';with t as (select *, (select case ENFIELD'<br><br>是在2005中用的吧? 在2000中运行提示: &nbsp;';' 附近有语法错误。<br><br>------<br>另外,在 Oracle中肯定不是这样拼写的SQL。<br>因为像declare @s varchar(8000)这样的写法,是SQL_Server中才有的。<br>------<br>另外,我注意到在SQL2005和Oracle11g中,有两个东东是非常有用的:pivot和unpivot。<br>可惜在低版本中是没有的。<br><br>因我目前的整个系统是在SQL2000/Oracle9i下的,所以我无法改换数据库。[:(]
 
是 2005.<br>你应该一开始就说明是 SQL2000。<br>SQL2000 要实现同样功能,可能要用临时表。你同意用临时表吗?
 
我在 CSDN 又给你一个答案,你再试试。
 
多谢楼上各位了!<br><br>为了同时适应SQLSERVER和Oracle的需求,最后还是选择了在前台寄主中循环拼写完成SQL。<br><br>采用的是UNION ALL的方式。<br><br>在寄主完成SQL的拼写,我所想到的,大致有两个好处吧:<br><br>1.SQL字符串长度不受限制,能解决诸如SQLSERVQR中的varchar(8000)限制。<br>2.不使用存储过程或函数,这样就能同时适应于SQLSERVER和Oracle的需求,方便系统移植。<br><br>缺点是可能会造成速度/效率降低一些。<br>(我在JAVA中用append,实际运行效果还行吧。^_^)<br><br><br>我所拼写的SQL就是用的楼上kaida提供的方法,可以通用于SQLSERVER和Oracle。<br><br>最终拼写完成后的SQL大致的样子如下:<br><br>Select ENFIELD,CHNAME,VALUE From t1 a INNER JOIN <br>(<br>Select 'A' as ID,(select RTrim(A) from t2 ) as &nbsp;VALUE FROM t2 &nbsp; --此处可以增加where条件<br>union all<br>Select 'B' as ID,(select RTrim(B) from t2 ) as &nbsp;VALUE FROM t2<br>union all<br>Select 'C' as ID,(select RTrim(C) from t2 ) as &nbsp;VALUE FROM t2<br>--如果有,继续拼接<br>......<br>) b <br>on a.ENFIELD=b.ID <br>and ((ENFIELD='A'and VALUE='值1') or (ENFIELD='C'and VALUE= '值3')) --过滤条件。<br><br><br>---------<br><br>再次表示感谢。[:)]
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
902
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
I
回复
0
查看
878
import
I
顶部