I am aware that you can assign a value to a variable within a select by going
SELECT @blahBlah = (tbl.ProductGroup)
FROM tbl
Ok, now what I need to do is as follow.
Each account/user has a set number of transactions allowed every day; eg. Account 1 can do 5 transactions and Account 2 can do 3.
So in a select that will determine whether this account has overshot its limit and by how many, we have numerous inSelect calculations to produce column data.
The problem is that we now have about 4 columns that will each do a nested select to get the limits from another table, in order to produce a value for the current one.
What would be ideal is to assign these limits to a @variable in the select statement and to only reference that variable within said statement if needed.
To give a brief example (not actual logic):
SELECT
@Limit = (SELECT Limit From tClient Where tClient.clientId = tbl.ClientId),
(select count(*) from batch where batchItemCount > @limit) AS BadBatches,
....
FROM TBL
You get the picture.
Is there some method to pull this off?
Yes, you can use a Common Table Expression(CTE) to do these sort of things. The following is the basic idea that you can tweak to get it work for your case:
Hope you got the picture.