日期:2014-05-17 浏览次数:20755 次
CREATE TABLE A (FLOW_ID INT,[督办人id]INT,[审批人] INT) INSERT INTO a select 23,5567,4432 CREATE TABLE b (FLOW_ID INT,main_id INT,sentence NVARCHAR(320)) INSERT INTO B SELECT 23,5567,N'请尽快办理,同意' UNION SELECT 23,4432,N'不同意,其中XX有问题' CREATE VIEW V_VIEW AS SELECT A.*,B.sentence FROM A JOIN B ON A.FLOW_ID=B.FLOW_ID AND ([督办人id]=MAIN_ID OR [审批人]=MAIN_ID) /* FLOW_ID 督办人id 审批人 sentence 23 5567 4432 请尽快办理,同意 23 5567 4432 不同意,其中XX有问题 */
------解决方案--------------------
-->测试数据 IF OBJECT_ID('tb1')IS NOT NULL DROP TABLE tb1 GO CREATE TABLE tb1 ( flow_id INT, [督办人id] INT, [审批人] INT) INSERT INTO tb1 SELECT 23, 5567, 4432 IF OBJECT_ID('tb2')IS NOT NULL DROP TABLE tb2 GO CREATE TABLE tb2 ( flow_id INT, main_id INT, sentence NVARCHAR(320)) INSERT INTO tb2 SELECT 23, 5567, N'请尽快办理,同意' UNION ALL SELECT 23, 4432, N'不同意,其中XX有问题' -->测试查询 SELECT a.*, b.sentence FROM tb1 a LEFT JOIN tb2 b ON a.flow_id = b.flow_id AND ([督办人id] = main_id OR [审批人] = main_id) ---------------------- /* FLOW_ID 督办人id 审批人 sentence 23 5567 4432 不同意,其中XX有问题 23 5567 4432 请尽快办理,同意 */