Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

are there loops in t-sql ? is using them a good idea?

Pardon me if I'm asking this, but I'm not a SQL Server nor SQL developer.

i have a CSV that i import into a table let's call it T that i create on the fly in SQL Server 2005.

what i would like to do is to run some queries against other tables based on the data imported into the table T i created. example :

select * 
from TableX 
where customerID = [this should contain the customerID from the table T]

and then if i find it, i need to update the same table T, if not i move along... until the last record in that csv file. Any idea will be appreciated.

like image 408
user1327073 Avatar asked Nov 27 '25 15:11

user1327073


1 Answers

No loop necesary for what you want, it seems that you only need IN:

SELECT *
FROM TableX
WHERE CustomerID IN (SELECT CustomerID FROM TableT)

If you need to update TableX with some mark if exists on TableT, it should be:

UPDATE TableX
SET Mark = 1
WHERE CustomerID IN (SELECT CustomerID FROM TableT)

If you need to update TableX with some value from TableT, it should be something like this:

UPDATE X
SET X.Column = T.Column
FROM TableX X
INNER JOIN TableT T
ON X.CustomerID = T.CustomerID 
like image 60
Lamak Avatar answered Nov 29 '25 03:11

Lamak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!