Skip to main content

Posts

Learn Microsoft Azure

Take you career to the next level. Learn Microsoft Azure: API Management, Microsoft Service Fabric, CosmosDB, Dockers, Containers, .NET Core etc. Learn from the professional who has more than 18 years of experience in Consulting and Solution Architecture. Contact: dhandrohit@gmail.com | dhandrohit@hotmail.com

SQL query to show all records if the parameter is NULL

Sometimes we have to use a parameter that will form the basis of the condition in the Query. For e.g. if the parameter has a value, then we want to use it in the condition, otherwise we want to retrieve all the records. This can be achieved by: declare @vEname nvarchar(60) set @vEname = null Select * from Emp where Ename = isnull(@vEname,Ename) In the above script, if @vEname has some value, then the query will run based on that condition, otherwise it will run on the condition of "Ename = Ename" column, which will always return true and hence will return all the records. Happy Coding.!

Creating Knex migration to include Foreign Key

In case we have created a table and forget to include a foreign key, we can do it by creating a new migration and then running it. The following code shows how to do it: knex.schema.table('employees', function (table) { table.bigInteger('deptno').references('id').inTable('Depts') }); The above code adds a column deptno as a foreign key in the table named "Employees", that refers a column "ID" from the Dept table. Happy Coding!!!.

Migrations with Knex

Migrations are the best way of creating and maintaining database stuff. Through Knex we can easily create migrations and then finally run them against the database. After installing the "knex" package, we can use the following command: knex migrate:make setup The above command helps to create directory (migrations) and place a migration file in that folder. Now edit the contents of the file to include the following stuff: exports.up = function(knex, Promise) {     return Promise.all([     knex.schema.createTable('employees', function(table){       table.string('empname');       table.string('job');       table.timestamps();     })   ]) }; exports.down = function(knex, Promise) {     return Promise.all([     knex.schema.dropTable('employees');   ]) }; The above code has got two important concepts: a. UP b. DOWN UP is a function used when the mi...

How to do an Update through KNEX

In case we want to update a record within a table through KNEX, we can use the following code: updateRecord(); function updateRecord(){   knex('depts')   .where("deptno","40")   .update({     dname: 'TEST DEPARTMENT'   })   .then(count=>{     console.log("record updated"+count);   }); } The above code calls a function updateRecord that updates the deptno = 40's department name to 'TEST DEPARTMENT'. Also in the Promise '.then', it prints out number of records that were updated. Happy Coding !!!

How to Select through KNEX ORM

Select is the only command available in the whole SQL to retrieve data. KNEX provides an implementation of the "Select" Query. The following code illustrates: knex.select("*").from("depts") .then(function (dept) {     dept.forEach(function(value){       console.log(value.deptno);     });   }).catch(function(err) {     // All the error can be checked in this piece of code     console.log(err);   }).finally(function() {     // To close the connection pool     knex.destroy();   }); } The above code has following important observations: a. Its Selecting data from a table named "Depts". We are selecting all the columns and rows. b. Once select query has fetched rows, it will run a promise of .then c. "dept" in the .then function is a collection of all records fetched from the table. d. We can access individual columns like "value.deptno", "value.dname" within forEach loop. Happy ...