Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Loop Thorugh Column And Rows

Author  Topic 

daniel50096230
Yak Posting Veteran

99 Posts

Posted - 2012-06-15 : 04:52:42
Hi,

I had the following query

(SELECT Payment_ID, Paid_Amount, GST_Amount, Target_Consultant,
Comm_Consultant, Receipt_Status
FROM TRANSACTIONS.RT_Receipt_Detail
Where transaction_ID='TBJ12-01'
)

Insert Into B
([Value])
Values
(Payment_ID])

If the result is 2 rows, how can I loop through the two rows and get the column one by one then insert into the table B one row one column?

For example, the column i get from the query is 6. Then the rows is 2. So the insert statement should run 12 times to insert the column into one row one column.

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-06-15 : 05:17:08
Not sure whether you want a single value (one row one column) or 12 rows one column.
Or what you want in the column(s)

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

daniel50096230
Yak Posting Veteran

99 Posts

Posted - 2012-06-15 : 05:33:03
Hi,

For example, Payment_ID in one row, then Paid_Amount in second row and so on...
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-06-15 : 06:13:06
insert table2 select Payment_ID from table1
inserrt table2 select Paid_Amount frrom table1

You can do a union all for a single statement if you wish.
Before you say that you want them in order of Payment_ID, Paid_Amount, Payment_ID, Paid_Amount, ....
That's a meaningless statement as a table has no inherent order. You need a column value to sort on if you want to order rows.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -