Wednesday 8 August 2018

How to execute SQL against all DBs on a Server

One option is sp_MSForEachDB. It's undocumented but useful nonetheless

DECLARE @command varchar(1000) 
SELECT @command = 
 'USE [?] SELECT c.name AS ColName, t.name AS TableName FROM sys.columns c JOIN 
sys.tables t ON c.object_id = t.object_id WHERE c.name LIKE ''%BILLG_BIN_ID%''' 
EXEC sp_MSforeachdb @command
NOTES:
  1. ? is replaced in the query as the database name, so structure the query to explicitly define which DB it is to query against

Monday 2 July 2018

LINQ query check if not null then only apply condition

Sometimes, we have requirement like need to pass one parameter but its nullable, and we have to pass this parameter in where clause also need to check only if value is not null then only check condition..then in this case what we can do ..???

well here is one of the solution :

public employee GetEmployee(int id, string appId)
{
      var result= (from e in objcontext.employee
                         where e.Id==id &&
                         (string.IsNullOrEmpty(appId) || e.AppId==appId)  
// Here it will check first if null then it will not go to second condition else it will check where clause for that column  
                          select e).ToList();

}