1

我有一个查询,它比较两个请求并根据比较选择查询中返回的每一列显示是/否。我如何更改它以能够在视图或表值函数中检索相同的结果。当我使用 SQL 创建存储过程但我们的业务对象应用程序无法执行 SQL 时,我得到了结果

下面是我的 SQL

DECLARE @Foo TABLE (requestid int,
receiveddate datetime,
neededby datetime,busneed varchar(1000) ,
rptcomments varchar(1000),
reportfrequency varchar(100),
defineschedule varchar(250),
schedulebasedon varchar(250) ,
clmsrvdtfrom datetime,
clmsrvdtthru datetime ,
clmpddtfrom datetime,
clmpddtthru datetime
)

INSERT @Foo (requestid,receiveddate,neededby,busneed,rptcomments,reportfrequency,defineschedule,schedulebasedon,clmsrvdtfrom,clmsrvdtthru,clmpddtfrom,clmpddtthru)
select requestid,receiveddate,neededby,busneed,rptcomments,reportfrequency,defineschedule,schedulebasedon,clmsrvdtfrom,clmsrvdtthru,clmpddtfrom,clmpddtthru
from report_request where requestid = 1494
INSERT @Foo (requestid,receiveddate,neededby,busneed,rptcomments,reportfrequency,defineschedule,schedulebasedon,clmsrvdtfrom,clmsrvdtthru,clmpddtfrom,clmpddtthru)
select requestid,receiveddate,neededby,busneed,rptcomments,reportfrequency,defineschedule,schedulebasedon,clmsrvdtfrom,clmsrvdtthru,clmpddtfrom,clmpddtthru
from report_request where requestid = 1495


SELECT CAST(requestid AS VARCHAR(MAX)) requestid,
CAST(receiveddate AS VARCHAR(MAX)) AS receiveddate,
CAST(neededby AS VARCHAR(MAX)) AS neededby,
CAST(busneed AS VARCHAR(MAX)) AS busneed,
CAST(rptcomments AS VARCHAR(MAX)) AS rptcomments,
CAST(reportfrequency AS VARCHAR(MAX)) AS reportfrequency,
CAST(defineschedule AS VARCHAR(MAX)) AS defineschedule,
CAST(schedulebasedon AS VARCHAR(MAX)) AS schedulebasedon,
CAST(clmsrvdtfrom AS VARCHAR(MAX)) AS clmsrvdtfrom,
CAST(clmsrvdtthru AS VARCHAR(MAX)) AS clmsrvdtthru,
CAST(clmpddtfrom AS VARCHAR(MAX)) AS clmpddtfrom,
CAST(clmpddtthru AS VARCHAR(MAX)) AS clmpddtthru
from Customer_REQUEST where requestid = 1494

UNION ALL
SELECT CAST(requestid AS VARCHAR(MAX)) requestid,
CAST(receiveddate AS VARCHAR(MAX)) AS receiveddate,
CAST(neededby AS VARCHAR(MAX)) AS neededby,
CAST(busneed AS VARCHAR(MAX)) AS busneed,
CAST(rptcomments AS VARCHAR(MAX)) AS rptcomments,
CAST(reportfrequency AS VARCHAR(MAX)) AS reportfrequency,
CAST(defineschedule AS VARCHAR(MAX)) AS defineschedule,
CAST(schedulebasedon AS VARCHAR(MAX)) AS schedulebasedon,
CAST(clmsrvdtfrom AS VARCHAR(MAX)) AS clmsrvdtfrom,
CAST(clmsrvdtthru AS VARCHAR(MAX)) AS clmsrvdtthru,
CAST(clmpddtfrom AS VARCHAR(MAX)) AS clmpddtfrom,
CAST(clmpddtthru AS VARCHAR(MAX)) AS clmpddtthru
from CUSTOMER_REQUEST where requestid = 1495

UNION ALL


SELECT 
    CASE WHEN A.requestid = B.requestid THEN 'Yes' ELSE 'No'  END as requestid,
    CASE WHEN A.receiveddate = B.receiveddate THEN 'Yes' ELSE 'No' END as receiveddate,
    CASE WHEN A.neededby = B.neededby THEN 'Yes' ELSE 'No' END neededby,
    CASE WHEN A.busneed = B.busneed THEN 'Yes' ELSE 'No' END busneed,
    CASE WHEN A.rptcomments = B.rptcomments THEN 'Yes' ELSE 'No' END rptcomments,
    CASE WHEN A.reportfrequency = B.reportfrequency THEN 'Yes' ELSE 'No' END as reportfrequency,
    CASE WHEN A.defineschedule = B.defineschedule THEN 'Yes' ELSE 'No' END as defineschedule,
    CASE WHEN A.schedulebasedon = B.schedulebasedon THEN 'Yes' ELSE 'No' END as schedulebasedon,
    CASE WHEN A.clmsrvdtfrom = B.clmsrvdtfrom THEN 'Yes' ELSE 'No' END as clmsrvdtfrom,
    CASE WHEN A.clmsrvdtthru = B.clmsrvdtthru THEN 'Yes' ELSE 'No' END as clmsrvdtthru,
    CASE WHEN A.clmpddtfrom = B.clmpddtfrom THEN 'Yes' ELSE 'No' END as clmpddtfrom,
    CASE WHEN A.clmpddtthru = B.clmpddtthru THEN 'Yes' ELSE 'No' END as clmpddtthru

FROM
    (SELECT * FROM @Foo WHERE requestid = 1494) AS A
INNER JOIN
    (SELECT * FROM @Foo WHERE requestid = 1495) AS B

ON 1 = 1

需要帮助请

4

1 回答 1

0

那这个呢:

CREATE VIEW _test
AS
SELECT CAST(requestid AS VARCHAR(MAX)) requestid,
CAST(receiveddate AS VARCHAR(MAX)) AS receiveddate,
CAST(neededby AS VARCHAR(MAX)) AS neededby,
CAST(busneed AS VARCHAR(MAX)) AS busneed,
CAST(rptcomments AS VARCHAR(MAX)) AS rptcomments,
CAST(reportfrequency AS VARCHAR(MAX)) AS reportfrequency,
CAST(defineschedule AS VARCHAR(MAX)) AS defineschedule,
CAST(schedulebasedon AS VARCHAR(MAX)) AS schedulebasedon,
CAST(clmsrvdtfrom AS VARCHAR(MAX)) AS clmsrvdtfrom,
CAST(clmsrvdtthru AS VARCHAR(MAX)) AS clmsrvdtthru,
CAST(clmpddtfrom AS VARCHAR(MAX)) AS clmpddtfrom,
CAST(clmpddtthru AS VARCHAR(MAX)) AS clmpddtthru
from Customer_REQUEST where requestid = 1494

UNION ALL
SELECT CAST(requestid AS VARCHAR(MAX)) requestid,
CAST(receiveddate AS VARCHAR(MAX)) AS receiveddate,
CAST(neededby AS VARCHAR(MAX)) AS neededby,
CAST(busneed AS VARCHAR(MAX)) AS busneed,
CAST(rptcomments AS VARCHAR(MAX)) AS rptcomments,
CAST(reportfrequency AS VARCHAR(MAX)) AS reportfrequency,
CAST(defineschedule AS VARCHAR(MAX)) AS defineschedule,
CAST(schedulebasedon AS VARCHAR(MAX)) AS schedulebasedon,
CAST(clmsrvdtfrom AS VARCHAR(MAX)) AS clmsrvdtfrom,
CAST(clmsrvdtthru AS VARCHAR(MAX)) AS clmsrvdtthru,
CAST(clmpddtfrom AS VARCHAR(MAX)) AS clmpddtfrom,
CAST(clmpddtthru AS VARCHAR(MAX)) AS clmpddtthru
from CUSTOMER_REQUEST where requestid = 1495

UNION ALL


SELECT 
    CASE WHEN A.requestid = B.requestid THEN 'Yes' ELSE 'No'  END as requestid,
    CASE WHEN A.receiveddate = B.receiveddate THEN 'Yes' ELSE 'No' END as receiveddate,
    CASE WHEN A.neededby = B.neededby THEN 'Yes' ELSE 'No' END neededby,
    CASE WHEN A.busneed = B.busneed THEN 'Yes' ELSE 'No' END busneed,
    CASE WHEN A.rptcomments = B.rptcomments THEN 'Yes' ELSE 'No' END rptcomments,
    CASE WHEN A.reportfrequency = B.reportfrequency THEN 'Yes' ELSE 'No' END as reportfrequency,
    CASE WHEN A.defineschedule = B.defineschedule THEN 'Yes' ELSE 'No' END as defineschedule,
    CASE WHEN A.schedulebasedon = B.schedulebasedon THEN 'Yes' ELSE 'No' END as schedulebasedon,
    CASE WHEN A.clmsrvdtfrom = B.clmsrvdtfrom THEN 'Yes' ELSE 'No' END as clmsrvdtfrom,
    CASE WHEN A.clmsrvdtthru = B.clmsrvdtthru THEN 'Yes' ELSE 'No' END as clmsrvdtthru,
    CASE WHEN A.clmpddtfrom = B.clmpddtfrom THEN 'Yes' ELSE 'No' END as clmpddtfrom,
    CASE WHEN A.clmpddtthru = B.clmpddtthru THEN 'Yes' ELSE 'No' END as clmpddtthru

FROM
    (select requestid,receiveddate,neededby,busneed,rptcomments,reportfrequency,defineschedule,schedulebasedon,clmsrvdtfrom,clmsrvdtthru,clmpddtfrom,clmpddtthru
from report_request where requestid = 1494) AS A
INNER JOIN
    (select requestid,receiveddate,neededby,busneed,rptcomments,reportfrequency,defineschedule,schedulebasedon,clmsrvdtfrom,clmsrvdtthru,clmpddtfrom,clmpddtthru
from report_request where requestid = 1495) AS B
ON 1 = 1
于 2013-02-28T01:22:53.500 回答