2008-09-03

How to analyze without using SQL CURSORs

##First study how to handle data using a CURSOR ##

--Code Start
DECLARE get_Details CURSOR FOR

CREATE TABLE #Temp(Stu_id bigint,
Stu_No nvarchar,
Stu_Name nvarchar)

OPEN get_Details

DECLARE @id bigint
DECLARE @Stu_No nvarchar(64)
DECLARE @Stu_Name nvarchar(100)

FETCH NEXT FROM get_Details INTO @Stu_NO

WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @id =id,@Stu_NO = NO,@Stu_Name = firstname,FROM aca_User WHERE id = @Stu_NO

INSERT INTO #Temp ( Stu_id,Stu_NO,Stu_Name) VALUES (@id,@Stu_NO,@Stu_Name)
FETCH NEXT FROM get_Details INTO @Stu_NO END


CLOSE get_Details
DEALLOCATE get_Details


SELECT * FROM #Temp


--Code is End

##Then know about that the same thing can do without using sql Cursor's
Because the cursor want higher memory usage of the RAM & then it produced more lower performance##


drop table EMPLOYEE2
SELECT ROWID=IDENTITY(int,1,1),userid
INTO EMPLOYEE2 FROM aca_role_assignments WHERE (roleid = @roleid) AND (contextid = @contexid) ORDER BY id

declare @rowcount int,
@counter int
SET @rowcount = (SELECT count(userid) FROM aca_role_assignments
WHERE (roleid =@roleid) AND (contextid = @contexid))

CREATE TABLE #Temp( Stu_id bigint,
Stu_NO nvarchar(100), Stu_Name nvarchar(100))

DECLARE @id bigint
DECLARE @Stu_NO nvarchar(100)
DECLARE @Stu_Name nvarchar(100)

set @counter = 1
while @counter <= @rowcount begin SELECT @id =id, @Stu_NO = firstname, @Stu_Name = lastname, from aca_user where id =(SELECT userid FROM EMPLOYEE2 WHERE ROWID=@counter) INSERT INTO #Temp ( Stu_id, Stu_NO,Stu_Name) VALUES (@id, @Stu_NO,@Stu_Name) set @counter = @counter + 1 end DELETE FROM aca_StudentSpecficCourse SELECT * FROM #Temp

No comments:

SQL Query Stuff

1 . By using this query can search any string contain within stored procedure, function or view SELECT object_name(id) FROM sys.sysc...