user7512602
user7512602

Reputation:

Conditional Query in T-SQL for getting records from multiple databases and tables

I have a clause like this:

Give me all contracts where IsDeleted is 0 AND where UstrojstvenaJedinicaId is equal to procedure parameter (@zavodId)
OR
there is only one UstrojstvenaJedinicaId and that UstrojstvenaJedinicaId is = 'HCPHS'

Example:

Procedure parameter zavodId = 5;

So in this example, I want to get all contracts where UstrojstvenaJedinicaId = 5 and only those contracts because we met the requests in first part of where clause.

If it helps, this is my C# code which is good and works and SQL query should be like this:

.Where(x => x.UstrojstveneJedinice.Any

(y => y.UstrojstvenaJedinicaId == zavodId) ||

x.UstrojstveneJedinice.All(y => y.UstrojstvenaJedinicaId == 10))

Upvotes: 1

Views: 76

Answers (1)

DaniDev
DaniDev

Reputation: 2631

I think this is what you are asking for:

IF ( (SELECT COUNT(table.id)  FROM *Tablename* WHERE UstrojstvenaJedinicaId =  'HCPHS') = 1)  

BEGIN 

(SELECT *  FROM  *Tablename* WHERE UstrojstvenaJedinicaId =  'HCPHS')
END
ELSE

(SELECT * FROM  *Tablename* WHERE  isdeleted = 0  and UstrojstvenaJedinicaId = @zavodId)

Upvotes: 1

Related Questions