导航

SQL语句的巧用-------以前没这么用过

Posted on 2009-03-09 18:48  菜鸟都不容易  阅读(176)  评论(0编辑  收藏  举报

--SQl 的巧用
--select 常值 as 列名 from 表名    
--在查询出的结果里加一个常量字段
select 'Type' as type from Earn_Money

--select 方法(参数) as 列名        
--查询出此方法的值作为列值 类似select @@version
select sin(90) as Sin值                            

--Update 表名 set 字段=case when 条件1 then 结果1 else 结果2 end where 条件2
--更新表符合条件2的记录的某字段,当符合条件1时候此字段为结果1否则为结果2
Update Oper_PriceBody2
Set FSellMoney = Case When @YMSL<=3000
                  Then (FOpen/4)*@ZHZSS+(@ZSS*FColor)
                  Else FSellPrice*(@ZCSS+@Temp)*@YSTCS End,
    FMoney = Case When @YMSL<=3000
                  Then (FOpen/4)*@ZHZSS+(@ZSS*FColor)
                  Else FPrice*(@ZCSS+@Temp)*@YSTCS End
Where FHead = @BillCode And FItem='印刷'           
                                           
--Update 表1 set 字段1=值1 from 表2 where 条件1 
--更新表1的字段1为值1 其中值1是表2中的字段,表2是用select语句把值1查询出来 注意:这里把表2当做一个数据集来考虑
Update Oper_PriceHead
Set FUnTaxMoney = FSellSum, FBaseMoney = FBaseSum
From (Select Sum(FSellMoney) As FSellSum, Sum(FMoney) As FBaseSum
      From Oper_PriceBody2
      Where FHead = @BillCode) As T1
Where FCode = @BillCode                           

--Insert Into 表1(字段1) select 字段1 from 表2 where 条件1
--与常用的Insert Into 表1(字段1) values(值1) 不同 注意:其实values(值1)和select(字段1)本质上都是记录集的一条记录
Insert Into Mate_Relation(FStorageCode, FPlaceCode, FMaterialCode, FValidDate)
      Select Distinct FStorageCode, FPlaceCode, FMaterialCode, FValidDate
      From Mate_BuyRetBody
      Where FHead = @BillCode 
      And (FStorageCode + FPlaceCode + FMaterialCode + Convert(nvarchar,IsNull(FValidDate,'1899-01-01'))) Not In
          (Select FStorageCode + FPlaceCode + FMaterialCode + Convert(nvarchar,IsNull(FValidDate,'1899-01-01')) From Mate_Relation)