日期:2014-05-19  浏览次数:20553 次

如何将某列值作为查询内容?
如:SELECT   xt_zlb.dh,   xt_ddb.fwz,   test.c
FROM   xt_zlb   INNER   JOIN
            xt_ddb   ON   xt_zlb.ddid   =   xt_ddb.ddid   INNER   JOIN
            xt_qyb   ON   xt_ddb.qyid   =   xt_qyb.qyid   CROSS   JOIN
            test

列test.c储存的内容为xt_ddb.sx,   xt_qyb.qym

想得到如下的结果:将test.c储存的内容作为查询内容
SELECT   xt_zlb.dh,   xt_ddb.fwz,   xt_ddb.sx,   xt_qyb.qym
FROM   xt_zlb   INNER   JOIN
            xt_ddb   ON   xt_zlb.ddid   =   xt_ddb.ddid   INNER   JOIN
            xt_qyb   ON   xt_ddb.qyid   =   xt_qyb.qyid

------解决方案--------------------
declare @sql varchar(8000)

SELECT @sql = test.c
FROM xt_zlb INNER JOIN
xt_ddb ON xt_zlb.ddid = xt_ddb.ddid INNER JOIN
xt_qyb ON xt_ddb.qyid = xt_qyb.qyid CROSS JOIN
test
set @sql = 'SELECT xt_zlb.dh, xt_ddb.fwz, '+@sql+ 'FROM xt_zlb INNER JOIN
xt_ddb ON xt_zlb.ddid = xt_ddb.ddid INNER JOIN
xt_qyb ON xt_ddb.qyid = xt_qyb.qyid '
exec(@sql)
------解决方案--------------------
没看明
------解决方案--------------------
create function fn (@ddid int,@qyid int)
returns varchar(8000)
as
begin
declare @sql varchar(8000)

SELECT @sql = test.c
FROM xt_zlb INNER JOIN
xt_ddb ON xt_zlb.ddid = xt_ddb.ddid INNER JOIN
xt_qyb ON xt_ddb.qyid = xt_qyb.qyid CROSS JOIN
test where xt_zlb.ddid = @ddid and xt_ddb.qyid = @qyid
return @sql
end
go
declare @sql varchar(8000)

select @sql = 'SELECT xt_zlb.dh, xt_ddb.fwz, '+dbo.fn(xt_zlb.ddid,xt_ddb.qyid)+ 'FROM xt_zlb INNER JOIN
xt_ddb ON xt_zlb.ddid = xt_ddb.ddid INNER JOIN
xt_qyb ON xt_ddb.qyid = xt_qyb.qyid '
FROM xt_zlb INNER JOIN
xt_ddb ON xt_zlb.ddid = xt_ddb.ddid INNER JOIN
xt_qyb ON xt_ddb.qyid = xt_qyb.qyid
exec(@sql)