Hi Richa,
Please refer the below sql query and modify as per your requirement.
Sql
DECLARE @Table TABLE(Id INT,Col1 VARCHAR(10), Col2 VARCHAR(10),col3 VARCHAR(10), col4 VARCHAR(10));
INSERT INTO @Table VALUES(1,'abc','xyz','pqr','');
INSERT INTO @Table VALUES(1,'',NULL,'aaa','');
INSERT INTO @Table VALUES(1,'uvw',NULL,'','');
INSERT INTO @Table VALUES(2,'ttt','','rrr','');
INSERT INTO @Table VALUES(2,'','www',NULL,'');
DECLARE @Id INT
,@Col1 VARCHAR(10)
,@Col2 VARCHAR(10)
,@col3 VARCHAR(10)
,@col4 VARCHAR(10)
DECLARE @Counter INT
SET @Counter = 1
DECLARE PrintRecord CURSOR READ_ONLY
FOR
SELECT Id,col1,col2,col3,col4 FROM @Table
OPEN PrintRecord
FETCH NEXT FROM PrintRecord INTO
@Id,@Col1,@Col2,@Col3,@Col4
WHILE @@FETCH_STATUS = 0
BEGIN
IF @Counter = 1
BEGIN
PRINT 'ID' + CHAR(9) + 'Col1' + CHAR(9) + 'col2' + CHAR(9) + 'col3' + CHAR(9) + 'Col4'
PRINT '------------------------------------'
END
PRINT CAST(@Id AS VARCHAR(10)) + CHAR(9) +
CASE WHEN @Col1='' THEN 'No Record' ELSE @Col1 END + CHAR(9)+ CHAR(9) +
CASE WHEN @Col2='' THEN 'No Record' ELSE @Col2 END + CHAR(9) + CHAR(9) +
CASE WHEN @Col3='' THEN 'No Record' ELSE @Col3 END + CHAR(9) + CHAR(9) +
CASE WHEN @Col4='' THEN 'No Record' ELSE @Col4 END + CHAR(9)
SET @Counter = @Counter + 1
FETCH NEXT FROM PrintRecord INTO
@Id, @Col1, @Col2,@Col3,@Col4
END
CLOSE PrintRecord
DEALLOCATE PrintRecord
Output
Id col1 col2 col3 col4
1 abc xyz pqr No Record
2 ttt No Record rrr No Record