• SQL当前查询条件数据需要调用其他数据时创建临时表实现


    BEGIN

    IF OBJECT_ID('tempdb..#TB') IS NOT NULL


    DROP TABLE #TB

    CREATE TABLE #TB(
    WorkcenterId CHAR(12),
    MOId CHAR(12),
    MOName NVARCHAR(50),
    WorkcenterName NVARCHAR(50),
    MODescription NVARCHAR(50),
    ProductId CHAR(12),
    ProductDescription NVARCHAR(200)
    )

    INSERT INTO #TB    (
    WorkcenterId,
    MOId,
    MOName,
    WorkcenterName,
    MODescription,
    ProductId,
    ProductDescription
    )
    SELECT m.WorkcenterId,m.MOId,m.MOName,w.WorkcenterName,m.MODescription,m.ProductId,pd.ProductDescription FROM  dbo.MO m 
        RIGHT OUTER JOIN dbo.Product pd ON pd.ProductId=m.ProductId
        RIGHT OUTER JOIN dbo.ProductRoot pr ON  pr.ProductRootId=pd.ProductRootId
        RIGHT OUTER JOIN dbo.Workcenter w ON w.WorkcenterId=m.WorkcenterId
        WHERE 
        --w.WorkcenterName='1030001'
            --MOName like '%'+@I_FiltCondition+'%'
              MOQtyRequired > ISNULL(MOQtyDone,0)
                AND m.MOStates NOT IN ('10','40','31','41','60')
                --AND m.WorkcenterId IN ('WKC1000001IK','WKC1000001IL')
                AND m.WorkcenterId=w.WorkcenterId
                ORDER BY MOId DESC 
    INSERT INTO #TB
    (
        WorkcenterId,
        MOId,
        MOName,
        WorkcenterName,
        MODescription,
        ProductId,
        ProductDescription
    )
    SELECT 'WKC1000001IO',m.MOId,m.MOName,'1030001',m.MODescription,m.ProductId,pd.ProductDescription FROM  dbo.MO m 
        RIGHT OUTER JOIN dbo.Product pd ON pd.ProductId=m.ProductId
        RIGHT OUTER JOIN dbo.ProductRoot pr ON  pr.ProductRootId=pd.ProductRootId
        RIGHT OUTER JOIN dbo.Workcenter w ON w.WorkcenterId=m.WorkcenterId
        WHERE 
        --w.WorkcenterName='1030001'
            --MOName like '%'+@I_FiltCondition+'%'
              MOQtyRequired > ISNULL(MOQtyDone,0)
                AND m.MOStates NOT IN ('10','40','31','41','60')
                AND m.WorkcenterId  ='WKC1000001IM'
                AND m.WorkcenterId=w.WorkcenterId
                ORDER BY MOId DESC 
    INSERT INTO #TB
    (
        WorkcenterId,
        MOId,
        MOName,
        WorkcenterName,
        MODescription,
        ProductId,
        ProductDescription
    )
    SELECT 'WKC1000001IP',m.MOId,m.MOName,'1030002',m.MODescription,m.ProductId,pd.ProductDescription FROM  dbo.MO m 
        RIGHT OUTER JOIN dbo.Product pd ON pd.ProductId=m.ProductId
        RIGHT OUTER JOIN dbo.ProductRoot pr ON  pr.ProductRootId=pd.ProductRootId
        RIGHT OUTER JOIN dbo.Workcenter w ON w.WorkcenterId=m.WorkcenterId
        WHERE 
        --w.WorkcenterName='1030001'
            --MOName like '%'+@I_FiltCondition+'%'
              MOQtyRequired > ISNULL(MOQtyDone,0)
                AND m.MOStates NOT IN ('10','40','31','41','60')
                AND m.WorkcenterId  ='WKC1000001IN'
                AND m.WorkcenterId=w.WorkcenterId
                ORDER BY MOId DESC 

    SELECT * FROM #TB

    DROP TABLE #TB

    END

  • 相关阅读:
    基于springboot秒杀的实现(redis ,mysql)
    可变参数与日志demo
    工控机上安装服务器级虚拟机vmware ESXi6.7
    Ajax基础实例,看了直接掌握学会!
    Web前端教学实录:深入剖析前端技术的奥秘
    「以云为核,无感极速」第五代验证码重磅来袭
    基于Spring Boot的家具销售电商平台设计与实现
    windows系统安装ubuntu22.04虚拟机
    科学中的人工智能:量子、原子和连续体技术概述
    网络安全(黑客)自学
  • 原文地址:https://blog.csdn.net/u013597888/article/details/127124184