Author: Latheesh NK

Exception: “Invalid parameter 1 specified for datediff.” in SQL Server

Problem Statement:

One of the view creation scripts in our deployment failed continuously with the below exception:

Invalid parameter 1 specified for datediff.
Incorrect syntax near the keyword ‘AS’.

The root cause of this error is as below.

FLOOR((DATEDIFF("W",ml.column1,ML.column2)+1)/7) AS aliasname


FLOOR((DATEDIFF(W,ml.column1,ML.column2)+1)/7) AS aliasname

The change is that the datepart has been modified to remove the double quote.

How to identify duplicate lines in a text file using Python

Here is a short program in Python to identify the count of duplicate lines in a text file.

import tkinter as tk
from tkinter import filedialog
from collections import defaultdict
import pandas as pd
import collections
from pathlib import Path
import os

root= tk.Tk()

canvas1 = tk.Canvas(root, width = 800, height = 300)

label1 = tk.Label(root, text='Log Analyser')
label2 = tk.Label(root, text='Import a file...')
label1.config(font=('Arial', 20))
label2.config(font=('Arial', 10))
canvas1.create_window(400, 50, window=label1)
canvas1.create_window(200, 180, window=label2)

def getLogFile ():
      global df

      import_file = filedialog.askopenfilename()
      Counter = 0

      with open(import_file, "r+") as f:
            d = f.readlines()
            entries = Path(import_file)
            fileabspath = os.path.abspath(import_file)
            fw= open(fileabspath.replace(,"Duplicate_Log_Info.txt"),"w+")
            counts = collections.Counter(l.strip() for l in f)
            for line, count in counts.most_common():
                #print (line, "|"+str(count))
                fw.write(line + "|"+str(count) + "\n")
            label3 = tk.Label(root, + ": Import is successful, Please check the output file - "+ + ".")
            label3.config(font=('Arial', 10))
            canvas1.create_window(400, 220, window=label3)

browseButton_Excel = tk.Button(text='Choose a file...', command=getLogFile, bg='green', fg='white', font=('helvetica', 12, 'bold'))
canvas1.create_window(400, 180, window=browseButton_Excel)

button3 = tk.Button (root, text='Close', command=root.destroy, bg='green', font=('helvetica', 11, 'bold'))
canvas1.create_window(500, 180, window=button3)



If you enjoyed this blog post, feel free to share it with your friends!

Error Message: Server is not configured for RPC in SQL Server

Recently one of my colleague reached out to me with an error message as : “Server is not configured for RPC”

This a typical issue with Linked Server. When we configure a linked server, we need to set up the right values for RPC & RPC Out . We can see these values in SSMS -> Right click on Linked Server -> Properties -> Server Options as in the below screen shot.

RPC / RPC OutRPC stands for Remote Procedure Call and allows you to run stored procedures on the Linked Server.  RPC enables Remote Procedure Calls from the specified server and RPC OUT enables Remote Procedure Calls to the specified server.

Hope this helps; If you enjoyed this blog post, feel free to share it with your friends!

Computed columns in SQL Server

What are computed columns in SQL Server

Computed columns are virtual columns that does not physically store the data in SQL Server unless it is marked as PERSISTED column. Computed columns can be defined by expression that can use other columns of the same table or static expressions.


  1. It cannot be part of INSERT/UPDATE statement.
  2. It cannot defined as FOREIGN KEY/DEFAULT or NOT NULL constraint.

How to add a computed column while creating a table?

Create Table SampleComputedTable
	EmpName varchar(50),
	DOB Date,
	Age as Datediff(year,DOB,GETDATE())

--Today is -> '2021-07-25'; for simple explantion, the Age is calculated only year part.
Insert into SampleComputedTable Values ('Latheesh','2019-07-25')

Select * From SampleComputedTable

Drop table SampleComputedTable

How to add a computed column to an existing table?

Alter Table SampleComputedTable Add AgeInMonths as DateDiff(month,DOB,Getdate())

Select * From SampleComputedTable

What are the best practices while creating a computed column?

There is NO way to alter computed column. You will have to drop and recreate it. 

How to find the definition of a computed column?

Select object_name(object_id) TableName, name as ColumnName, definition From sys.computed_columns 
where name='Age' and object_name(object_id) = 'SampleComputedTable'

If you enjoyed this blog post, feel free to share it with your friends!

FOREIGN KEY Constraints in SQL Server

Foreign Key Constraint is a type of constraint in SQL Server to enforce a relation between two tables with a column or a set of columns to ensure a data integrity.

Let us quickly look at how do we create a foreign key in SQL Server

Create Table ParentTable (ParentCol int Primary Key NOT NULL)
Create Table ChildTable (ChildCol int Primary Key NOT NULL, ParentCol int NULL references ParentTable(ParentCol))

The above will create foreign key from ChildTable to ParentTable on column ParentCol. If we carefully look at the definition of ParentCol in ChildTable, we can see its a NULL-able column. If there is a requirement that we should not have NULL value, then we can make the column as NOT NULL.

Cascade Actions:

As we see in the above screenshot, delete_action/update_action are called as cascade actions. This can be used to define the actions on delete/update operations on Primary table. Let us examine with an example as below.

First off, let us try to insert some data in both tables and try to delete from Parent which has reference record in ChildTable.

Insert into ParentTable Select 100
Insert into ParentTable Select 200
Insert into ParentTable Select 300
Insert into ChildTable Select 1, NULL
Insert into ChildTable Select 2, NULL
Insert into ChildTable Select 3,300

Select * From ParentTable
Select * From ChildTable

--Now try to delete a record from ParentTable which has a corresponding record in ChildTable
Delete From ParentTable where ParentCol =300

The DELETE operation fails as there is a record in ChildTable which prevents the DELETE operation on ParentTable.

Now, let us have a CASCADE action on DELETE to define delete on ChildTable as well.

ALTER TABLE ChildTable DROP constraint[FK__ChildTabl__Paren__58D1301D]

ALTER TABLE [dbo].ChildTable  WITH CHECK ADD  CONSTRAINT [FK__ChildTabl__Paren__58D1301D] FOREIGN KEY(ParentCol)
REFERENCES [dbo].ParentTable (ParentCol)

/*Now try to delete a record from ParentTable which has a corresponding record in ChildTable*/
Delete From ParentTable where ParentCol =300

Select * From ParentTable
Select * From ChildTable

Now, we can see the DELETE operation is successful and the Delete action on the table has been changed to Cascade as below.

Points to ponder:

  1. You need to have the column in the Parent Table part of Primary Key
  2. There would not be any indexes created on the Child table for the referenced key by default. Sometimes, indexing of the foreign key column helps a lots on transactional databases/tables.
  3. Foreign key constraints are advised to create when two tables are tightly coupled and if we use these tables in a join condition to check its existence on Parent tables, because the key constraints takes care of it by default.
  4. If we have a CASCADE update, then SQL Server will not allow to create Instead of Trigger on the table.

If you enjoyed this blog post, feel free to share it with your friends!