帮忙 帮忙 SQL SERVER语句 不知道怎么Join
帮忙 帮忙 SQL SERVER语句 不知道怎么Join
语句一:
SELECT id, nd,yd,lb,xhddm,KMDM, je,shbj FROM TZ_KHZB
where nd=? and yd=? and xhddm=? and lb='sj'
语句二:
SELECT ID, KMDM, KMMC, DW, ZKMDM, ERPDM, ZGDW, ZGLD, KHDW, LRR, RKRQ, jldw
FROM SYS_KHZB WHERE charindex(?,KHDW)>0
?代表参数变量,加入语句一是表A , 语句二是表B ,
我想实现, select a.id, * from a right join b on a.kmdm = b.kmdm
这种动态的 join on 语句咋写,讲解思路、解决方案都要,谢谢各位!
[解决办法]
select a.id, *
from (SELECT id, nd,yd,lb,xhddm,KMDM, je,shbj
FROM TZ_KHZB
where nd=@nd and yd=@yd and xhddm=@xhddm and lb='sj' )a
right join (SELECT ID, KMDM, KMMC, DW, ZKMDM, ERPDM, ZGDW, ZGLD, KHDW, LRR, RKRQ, jldw
FROM SYS_KHZB
WHERE charindex(@KHDW,KHDW)>0)b on a.kmdm = b.kmdm
select a.id, *
from
(
SELECT id, nd,yd,lb,xhddm,KMDM, je,shbj FROM TZ_KHZB
where nd=? and yd=? and xhddm=? and lb='sj'
)a
right join b
(
SELECT ID, KMDM, KMMC, DW, ZKMDM, ERPDM, ZGDW, ZGLD, KHDW, LRR, RKRQ, jldw
FROM SYS_KHZB WHERE charindex(?,KHDW)>0
)b
on a.kmdm = b.kmdm
select a.id,* from
TZ_KHZB a right join SYS_KHZB b on a.kmdm = b.kmdm
where a.nd=? and a.yd=? and a.xhddm=? and a.lb='sj' and charindex(?,b.KHDW)>0
declare @sql varchar(max)
set @sql = '
select * from
(
SELECT id, nd,yd,lb,xhddm,KMDM, je,shbj FROM TZ_KHZB
where nd='+?+' and yd='+?+' and xhddm=? and lb='sj'
) a
right join
(
SELECT ID, KMDM, KMMC, DW, ZKMDM, ERPDM, ZGDW, ZGLD, KHDW, LRR, RKRQ, jldw
FROM SYS_KHZB WHERE charindex('+?+',KHDW)>0
) b on a.kmdm = b.kmdm'
exec (@sql)