当前位置: 代码迷 >> Sql Server >> 存储过程中的SQL话语
  详细解决方案

存储过程中的SQL话语

热度:62   发布时间:2016-04-27 11:53:41.0
存储过程中的SQL语句
SQL code
USE [Warehouse]GO/****** Object:  StoredProcedure [dbo].[SP_T_Operation_SelectAll]    Script Date: 07/19/2012 14:21:07 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO--------------------------------------用途:模糊查询 --项目名称:--说明:--时间:2012-7-17 15:00:36------------------------------------ALTER PROCEDURE [dbo].[SP_T_Operation_SelectAll_InOut](    @SheetID varchar(20),    @date1 varchar(25),    @date2 varchar(25),    @SheetOwner varchar(10),    @CostCenterCode varchar(10),    @ProductName nvarchar(100))ASBEGIN    DECLARE @sql nvarchar(MAX)    SET @sql ='            select             a.SheetID,a.OperationDate,a.OriginalSheetID,                        c.ProductName,a.ReturnReason,            case            when a.OriginalSheetID="" or a.OriginalSheetID is NULL then b.Quantity            end as outs,            case            when a.OriginalSheetID<>"" or a.OriginalSheetID is not NULL then b.Quantity            end as ins,            b.UnitPrice,            d.UserName,b.CostCenterCode            from T_Operation a left join             T_OperationDetail b on a.SheetID=b.SheetID             left join T_Product c on b.ProductID=c.ProductID            left join T_User d on a. SheetOwner=d.UserID            where 1=1            '                IF @SheetType IS NOT NULL AND @SheetType <>''        SET @[email protected]+' and A.SheetType [email protected];            IF @date1 IS NOT NULL AND @date1<>'' and @date2 IS NOT NULL AND @date2<>''        SET @[email protected]+' and A.OperationDate >='''+ CONVERT(varchar(100), @date1, 120)+''' and A.OperationDate <='''+CONVERT(varchar(100), @date2, 120)+'''';            IF @SheetOwner IS NOT NULL AND @SheetOwner<>''        SET @[email protected]+' and d.UserName LIKE([email protected]+'%'')';            IF @CostCenterCode IS NOT NULL AND @CostCenterCode<>''        SET @[email protected]+' and B.CostCenterCode LIKE([email protected]+'%'')';            IF @ProductName IS NOT NULL AND @ProductName<>''        SET @[email protected]+' and C.ProductName LIKE([email protected]+'%'')';        PRINT (@sql)        EXEC (@sql)    END

主要是这一段
SQL code
casewhen a.OriginalSheetID="" or a.OriginalSheetID is NULL then b.Quantityend as outs,casewhen a.OriginalSheetID<>"" or a.OriginalSheetID is not NULL then b.Quantityend as ins,

想实现当OriginalSheetID为空,则将Quantity当做OUTS输出
当OriginalSheetID不为空,则将Quantity当做ins输出
应该怎么写呢?

------解决方案--------------------
SET @sql ='
select 
a.SheetID,a.OperationDate,a.OriginalSheetID,
c.ProductName,a.ReturnReason,
case
when a.OriginalSheetID='''' or a.OriginalSheetID is NULL then b.Quantity
end as outs,
case
when a.OriginalSheetID<>'''' or a.OriginalSheetID is not NULL then b.Quantity
end as ins,
b.UnitPrice,
d.UserName,b.CostCenterCode
from T_Operation a left join 
T_OperationDetail b on a.SheetID=b.SheetID 
left join T_Product c on b.ProductID=c.ProductID
left join T_User d on a. SheetOwner=d.UserID
where 1=1
'
  相关解决方案