-2

I'm not sure if it is possible through SQL.

I have 2 tables:

  1. (int Contract_ID, int PR_nmb, bool Check)
  2. (int PR_ID, int PR_nmb, date Creation_Date)

This is 1 to many connection, for each Contract I have many PR with the same PR nmb. I want to take pr_nmb from table 1, then check in table 2 if all PR were created in current year and return true or false in Check.

Could I do this in SQL? Or it is better to write this logic on C#? Any advices are welcome.

1
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Samuel Liew
    Oct 28, 2019 at 12:13

1 Answer 1

0

It is always best to prepare the result for the client as much as possible in SQL server if we don't need all the records in client.

Because we will save lot of time in network and SQL servers are always faster. Then if there is any fix, it can be deployed in one place and no need to release the fix to all clients.

I tried this query:

    DECLARE @date   DATE
    DECLARE @pr_nmb INT

    SET @date  = '01-01-2018'
    SET @pr_nmb  =11

    SELECT tbl1.* 
    FROM table1 tbl1 
    WHERE   tbl1.pr_nmb = @pr_nmb and
    YEAR(@date) =  ALL (  SELECT YEAR( Creation_Date)
                            FROM table2 tbl2 
                            WHERE tbl1.pr_nmb = tbl2.PR_nmb  ) 
    DECLARE @date   DATE
    DECLARE @pr_nmb INT

    SET @date  = '01-01-2018'
    SET @pr_nmb  =11

    SELECT tbl1.* 
    FROM table1 tbl1 
    WHERE   tbl1.pr_nmb = @pr_nmb and
    YEAR(@date) =  ALL (  SELECT YEAR( Creation_Date)
                            FROM table2 tbl2 
                            WHERE tbl1.pr_nmb = tbl2.PR_nmb  ) 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.