SQL Server – How to read the SQL Server Error log files using TSQL

There is undocumented system stored procedure sp_readerrorlog which allows us to read the SQL Server error log files directly using T-SQL. This procedure has total 7 parameters as given below: Parameter Values First Parameter Value of error log file you want to read: 0 = current, 1 = Archive #1, 2 = Archive #2, etc. Second Parameter Log file type : 1 – Reads SQL Server error logs, 2 – Reads SQL Server Agent error logs Third Parameter Search string

» Read more

SQL Server – How Like operator work with datetime

Today, my one of the friend shared a very interesting fact about like operator. With the help of this post I am sharing with you all. We need to convert it to character data while comparing with the like operator. Let‘s check with example Like operator with datetime Like Operator with datetime using convert function If anybody have any suggestion please share as comment.

» Read more

SQL Server – How to add column dynamically in where clause

In this post, we will learn with example how to add the column dynamically in where clause. Example: For demo we have table as shown in the snapshot Basically we want to execute following script (i.e. Script: 1) but column name (i.e. FIRST_NAME) added dynamically in where clause. Script: 1 SELECT * FROM EMPLOYEE WHERE FIRST_NAME = 'VARINDER' Created a table and insert the column name value (i.e. FIRST_NAME) as shown in the snapshot Now we will execute the Script:

» Read more

SQL Server – The multi-part identifier could not be bound State – Msg 4104 – Fix

Error Msg Server: Msg 4104, Level 16, State 1, Line 1 The multi-part identifier could not be bound. Cause of this Error Msg: Case: 1 This error usually occurs when an alias is used when referencing a column in a SELECT statement and the alias used is not defined anywhere in the FROM clause. Case: 2 This error occurs if we are using the tables from the two different databases in the join statement and not using the table alias

» Read more

SQL Server – Generate Row Number in Select and Select into

How to Generate the Row Number in Select and Select into script? Using IDENTITY for Select Into SELECT RowNumber=IDENTITY(int,1,1),FIRST_NAME,LAST_NAME into EMPLOYEE_backup FROM EMPLOYEE Using Row_Number() for Select SELECT ROW_NUMBER() OVER (ORDER BY ID ASC) RowNumber, FIRST_NAME,LAST_NAME FROM EMPLOYEE Using Row_Number() for Select into SELECT ROW_NUMBER() OVER (ORDER BY ID ASC) RowNumber , FIRST_NAME,LAST_NAME into EMPLOYEE_backup1 FROM EMPLOYEE

» Read more

SQL Server 2008 – How to copy a Table

Lets see how to copy an existing table to new table in SQL Server. There are two options. Copy the table with Data. Copy the table without data (i.e.Table Structure Only) — Create Table CREATE TABLE EMPLOYEE ( ID INT NOT NULL, FIRST_NAME VARCHAR(10), LAST_NAME VARCHAR(10) );   — INSERT SOME VALUES INTO TABLES INSERT INTO EMPLOYEE (ID,FIRST_NAME,LAST_NAME) VALUES (1,'VARINDER','SANDHU'); INSERT INTO EMPLOYEE (ID,FIRST_NAME,LAST_NAME) VALUES (2,'DINESH','SHARMA'); INSERT INTO EMPLOYEE (ID,FIRST_NAME,LAST_NAME) VALUES (3,'RANJOYT','SINGH'); INSERT INTO EMPLOYEE (ID,FIRST_NAME,LAST_NAME) VALUES (4,'VIKRAM','SINGH');   –CREATE

» Read more

SQL Server – NEWID () vs. NEWSEQUENTIALID ()

NEWID () NEWID () creates a unique value of type uniqueidentifier. NEWID () can be referenced in the queries. Example: NEWSEQUENTIALID () NEWSEQUENTIALID () creates a GUID that is greater than any GUID previously generated by this function on a specified computer since Windows was started. After restarting Windows, the GUID can start again from a lower range, but is still globally unique. NEWSEQUENTIALID () cannot be referenced in queries. Example: Example: Using table Imp Note: Each GUID generated by

» Read more

SQL Server 2008 – Get the next Identity Value

Question: How to get the next Identity Value in SQL Server 2008? Answer: It is very simple to the get the identity value in SQL Server. Let’s see with the example below Lets‘s create some dummy data as mentioned below … In SQ Server, there are two functions IDENT_CURRENT and IDENT_INCR. With the help of these funtions, we can get next value Identity. IDENT_CURRENT: It returns the last identity value generated for a specified table or view. The last identity

» Read more

SQL SERVER – 2008 – Inline Variable Assignment – Declare and Assign Variable

Inline variable assignment is available in SQL Server 2008 or higher version. Beside this is very simple but very useful feature. Many of the programming languages have this feature. But now SQL Server 2008 or higher version has this feature. Example: —- SQL Server 2005 or earlier versions DECLARE @var1 INT SET @var1 = 100 SELECT @var1 AS myVar GO —- SQL Server 2008 or Higher Version (Inline Variable Assignment) DECLARE @var1 INT = 100 SELECT @var1 AS myVar GO

» Read more

SQL Server 2008 – Database Backup Compression

Backup compression was introduced in SQL Server 2008 Enterprise. At installation, backup compression default is set to 0, which makes backup compression off by default. To change the default to COMPRESSION, set backup compression default to 1. To revert the default to NO_COMPRESSION, set backup compression default back to 0. Backup Compress at Server Level (Thru Script) USE master; GO EXEC sp_configure 'show advanced option', '1'; RECONFIGURE GO EXEC sp_configure 'backup compression default', '1'; RECONFIGURE WITH OVERRIDE; GO EXEC sp_configure

» Read more

SQL Server 2008 – Get the List of Stored Procedures update/modify in last N number days

With the help of the given procedure one can get the list of Stored Procedures update or modify in last N number days. Script: — Get the List of Stored Procedures update/modify in last N number days CREATE PROCEDURE GetListModifySP ( @days INT ) AS SELECT name AS SP_Name, modify_date as Modfiy_Date FROM sys.objects WHERE TYPE = 'P' AND DATEDIFF(DD,modify_date, GETDATE()) < @days –Excecute the GetListModifySP –You can give the N of days as per requirement EXEC GetListModifySP 10 Hope

» Read more

SQL Server – Last Execution Date/Time of a Stored Procedure

If you want to get the  Last Execution Date/Time of a Stored Procedure then here is solution. Sys.dm_exec_procedure_stats it’s a system dynamic view that returns aggregate performance statistics for cached stored procedures. This view has been introduced from SQL Server 2008. The view returns one row for each cached stored procedure plan, and the lifetime of the row is as long as the stored procedure remains cached. When a stored procedure is removed from the cache, the corresponding row is

» Read more
1 2 3 4