我正在尝试编写一个包含一些级联条件逻辑的 SQL 语句。我有几个需要返回的字段,但它们的值取决于先前确定的字段。例如,buysell_flag 依赖于 spotfwd_flag 值,而 counter_amt 和 given_amt 依赖于 buysell_flag 值,依此类推。
我很快发现我无法将数据检索查询和变量分配查询组合在同一个空间中,所以我尝试了各种方法,但无法返回我想要的数据。这是客户的模板,他们将编辑此 SQL 并将其粘贴到程序中。它并不总是相同的,因此我无法处理程序或存储过程或 UDF 中的逻辑。我尝试使用表格,但是在插入表格并为其选择内容时,我遇到了同样的问题。您可以在下面看到我的尝试,但如果无法完成,请告诉我 - 否则,我愿意接受任何和所有建议!谢谢!
DECLARE @trans_type nvarchar(255), @trade_date nvarchar(255), @settle_date nvarchar(255),
@order_id nvarchar(255), @oaexecbroker nvarchar(255), @exec_broker nvarchar(255),
@ostatus nvarchar(255), @trade_id nvarchar(255), @reference_price nvarchar(255),
@given_ccy nvarchar(255), @spotfwd_flag nvarchar(255), @buysell_flag nvarchar(255),
@counter_ccy nvarchar(255), @counter_amt nvarchar(255), @given_amt nvarchar(255),
@oacct_cd nvarchar(255)
SELECT
@trans_type = o.trans_type,
@settle_date = o.settle_date,
@order_id = o.order_id,
@oaexecbroker = oa.exec_broker,
@exec_broker = o.exec_broker,
@ostatus = o.status,
@trade_id = oa.trade_id,
@reference_price = o.exec_price,
@given_ccy = o.target_crrncy,
--o.trans_type AS trans_type,
-- o.trade_date AS trade_date,
-- o.settle_date AS settle_date,
-- o.order_id AS order_id,
-- oa.exec_broker AS oaexecbroker,
-- o.exec_broker AS exec_broker,
-- o.status AS ostatus,
-- oa.trade_id AS trade_id,
-- o.exec_price AS reference_price,
-- o.target_crrncy as given_ccy,
-- for spot/fwd
@spotfwd_flag = CASE
WHEN LEFT(o.inv_class_cd,1) = 'F' THEN ('FWD')
WHEN LEFT(o.inv_class_cd,2) = 'CU' THEN ('SPOT')
ELSE 'SPOT'
END, -- must be spot or fwd
--for buysell
@buysell_flag = CASE
WHEN @spotfwd_flag = 'FWD' THEN
CASE
WHEN o.target_crrncy = o.to_crrncy THEN 'BUY'
ELSE 'SELL'
END
ELSE
CASE
WHEN o.trans_type = 'BUYL' THEN 'BUY'
ELSE 'SELL'
END
END, -- must be buy or sell
@counter_amt = CASE
WHEN @buysell_flag = 'BUY' Then oa.exec_amt
ELSE oa.exec_qty
END,
@given_amt = CASE
WHEN @buysell_flag = 'SELL' THEN oa.exec_amt
ELSE oa.exec_qty
END,
@counter_ccy = CASE
WHEN o.target_crrncy = o.to_crrncy THEN o.from_crrncy
ELSE o.to_crrncy
END,
@oacct_cd = CASE
WHEN f.udf_char6 = 'Y' THEN (oa.acct_cd + oa.custodian)
ELSE oa.acct_cd
END
FROM ts_order_alloc oa
LEFT OUTER JOIN ts_order o
ON oa.order_id = o.order_id
LEFT OUTER JOIN cs_fund f
ON oa.acct_cd = f.acct_cd
-- LEFT OUTER JOIN csm_security s
-- ON o.sec_id = s.sec_id
--WHERE s.sec_typ_cd IN ( 'CFWD', 'CURR' )
-- AND o.status IN ( 'READY','ACCT' )
-- AND oa.usr_class_cd_2 = 'GTSSREADY'
select @trans_type as trans_type,
@trade_date AS trade_date,
@settle_date AS settle_date,
@order_id AS order_id,
@oaexecbroker AS oaexecbroker,
@exec_broker AS exec_broker,
@ostatus AS ostatus,
@trade_id AS trade_id,
@reference_price AS reference_price,
@given_ccy as given_ccy,
@buysell_flag as buysell_flag,
@spotfwd_flag as spotfwd_flag,
@given_ccy as given_ccy,
@counter_ccy as counter_ccy,
@given_amt as given_amt,
@counter_amt as counter_amt
编辑:
我期望根据给定的变量取出行。我有这么多变量的原因是我试图让它符合 SQL 错误消息。
应该像这样出来:
trans_type trade_date settle_date order_id oaexecbroker exec_broker ostatus trade_id reference_price given_ccy buysell_flag spotfwd_flag given_ccy counter_ccy given_amt counter_amt
BUYL NULL 2010-04-06 00:00:00.000 1442084139 3PCITI 3PCITI Ready 1389278710 1.50705 GBP BUY SPOT GBP USD 604292 910699