Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

How can i use for each statement with condition in sql server

$
0
0

USE [TEST]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[JV](
[JvHeader_ID] [int] NULL,
[Debit] [float] NULL,
[credit] [float] NULL,
[AC_Code_No] [int] NULL,
[cal] [float] NULL,
[PlantID] [int] NULL
) ON [PRIMARY]

GO

INSERT DATA

insert into JV (JvHeader_ID , Debit , credit , AC_Code_No , cal ,PlantID) Values (2 ,76089,0,129,0.934579439252337,1)
insert into JV (JvHeader_ID , Debit , credit , AC_Code_No , cal ,PlantID) Values (2, 5326.23,0,17,0.0654205607476635,1)
insert into JV (JvHeader_ID , Debit , credit , AC_Code_No , cal ,PlantID) Values (2, 0,81415.23,48,1 ,1)

----------------------------------------------------------------------------------------

SELECT        JvHeader_ID, Debit, credit, Sum(Debit) over() As SumDebit , AC_Code_No, cal, PlantID FROM  JV


I want to loop all the row from select query above by check that if current row Debit or Credit = 0 then find the row that opposite  (Debit or Credit > 0)

This is Example  

For each row from (SELECT  JvHeader_ID, Debit, credit, Sum(Debit) over() As SumDebit , AC_Code_No, cal, PlantID FROM  JV)

if @Debit_of_currentrow = 0 

SELECT @currentrow, Debit, credit FROM JV WHERE Debit > 0

insert into some table

elseif Credit_of_currentrow = 0 

SELECT @currentrow , Debit, credit FROM  JV WHERE Credit> 0

insert into some table

next

Thanks


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>