开发者

calculate value based on 2 fields in SQL database

I can do this with linq easy but i got a situation where i have to create a stored procedure to return true or false based on 2 fields(minrange,maxran开发者_JAVA百科ge) in table B. So, the goal is given an id from table A, i select the range value from table A and compare this value to the 2 ranges in table B. If the value is within range(minrange,maxrange) return true. Thanks.


I'm assuming you have a field that allows you to join records from Table A to Table B. I'll call it "CategoryID". Try this:

SELECT 
    CASE WHEN TableA.Value BETWEEN TableB.MinValue AND TableB.MaxValue 
         THEN 1 ELSE 0 END
FROM TableA 
    INNER JOIN TableB ON TableA.CategoryID = TableB.CategoryID
WHERE TableA.ID = "TheID"

Good luck!

-Michael


Without knowing specifics (as to foreign keys), this syntax should work.

SELECT 
    CAST((CASE
            WHEN tableAValue < tableB.maxRange and tableAValue > tableB.minRange 
              THEN 1
            ELSE 
              0 
          END) AS BIT) 
FROM TableA
  INNER JOIN TableB
    ON TableA.ID = TableB.TableAID
WHERE TableA.ID = @yourID
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜