Answers

Question and Answer:

  Home  SQLServer Errors

⟩ In one table there is 10 records without id num, without primary key and it is not sorted then how to extract last 6 record from table?

CREATE TABLE Sample

(

Col1 varchar(5)

)

insert into Sample values('j')

TABLE:

Col1

j

r

c

r

e

f

g

h

x

j

USING CURSOR: ANSWER

DECLARE @COL1 VARCHAR(5)

DECLARE @COUNT VARCHAR(5)

SET @COUNT=0

DECLARE c1 CURSOR

FOR

SELECT COL1 FROM SAMPLE

OPEN C1

FETCH NEXT FROM C1

INTO @COL1

WHILE @@FETCH_STATUS=0

BEGIN

SET @COUNT=@COUNT+1

IF @COUNT>=5

BEGIN

SELECT @COL1

END

FETCH NEXT FROM C1

INTO @COL1

END

CLOSE C1

DEALLOCATE C1

 175 views

More Questions for you: