Sql部分过滤

时间:2014-08-27 09:10:25

标签: sql sql-server

我的sql表看起来像这样

Slno   HeadName    Bankcode
1      Abc         Cash
2      Contra      Bank1
3      Contra      Cash 
4      Xyz         Cash
5      Contra      Bank2
6      Contra      Cash
7      Pqr         Bank1

我的要求是过滤table.i需要从HeadName中删除'Contra'并且BankCode不等于='现金' 恩。删除slno no 2,5 我要追溯的所有其他行

我的表结构和样本数据如下所示

CREATE TABLE [dbo].[sampletable2](
    ID INT PRIMARY KEY ,
    [HeadName] [nvarchar](50),
    [BankCode] [nvarchar](50)
)



Insert Into sampletable2 Values(1,'Abc','Cash'),
Insert Into sampletable2 Values(2,'Contra','Bank1')
Insert Into sampletable2 Values(3,'Contra','Cash')
Insert Into sampletable2 Values(4,'Xyz','Cash')
Insert Into sampletable2 Values(5,'Contra','Bank2')
Insert Into sampletable2 Values(6,'Contra','Cash')
Insert Into sampletable2 Values(7,'Pqr' ,'Bank1')

Select * from sampletable2

1 个答案:

答案 0 :(得分:0)

像这样:

UPDATE sampletable2
SET HeadName = ''
WHERE Headname = 'Contra' AND BankCode != 'Cash'

或删除Datarow:

DELETE FROM sampletable2 
WHERE Headname = 'Contra' AND BankCode != 'Cash'

- >将删除Datarow 2和5