How do I split a string so I can access item x? - the solutions have used CTE
and WHILE
. I told my request was not using WHILE because this is not good performance, it delays the speed.. same goes for CURSOR
I am using SQL Server 2012. I will get a string of ids (@IDs
) that needs to be tested against a value (in this case: 1).
IF (SELECT CycleId
FROM TABLE1
WHERE ID in (@IDs) ) = '1'
BEGIN
-- ABORT TRANSACTION AND FINISH THE QUERY
END
ELSE
BEGIN
-- START TRANSACTION FOR THE Id....
END
This does not work. I was told that using a WHILE
or CURSOR
should be avoided.
I need to run the several values present in @Ids (it can be 100 values or just 5 values separated by comma , ). If, at least one, of these Ids have CycleId=1 it should stop completely the operation. Maybe it could be...
BEGIN
RAISERROR ('It is not possible to complete the transaction.',20,1)
BREAK
END
1st case:
@IDs = 121, 434, 543, 345
Id CycleId
---------------
121 1
434 2
543 1
345 1
in this case the operation should stop at @Id 121 (it should not read/test any other Ids and it should throw an error like I have shown before)
2nd case
@IDs = 121, 434, 543, 345
Id CycleId
--------------
121 3
434 2
543 5
345 6
In this case the operation should do the transaction.
Thanks!