以前的以前,有个哥们给我说过cursor可以通过sql来进行替代,为什么要替代,终归是性能。无奈那个时候对sql理解有限,现在仍旧有限。但是至少自己还是终于把这个问题理解清楚了。
其实很简单,看完这个例子,你就知道了。我在注视中写明了几个重要的性能指标。希望对您有所帮助
Code
USE SQL441
GO
CREATE TABLE tbl_culsor2sql_pdt
(
sn nvarchar(10) not null,
lbl nvarchar(20) null
)
GO
CREATE TABLE tbl_culsor2sql_data
(
sn nvarchar(10) not null,
lbl nvarchar(20) null
)
GO
INSERT INTO tbl_culsor2sql_pdt
VALUES('1Q2A3A4Z',NULL)
INSERT INTO tbl_culsor2sql_pdt
VALUES('4F5D6R4F',NULL)
INSERT INTO tbl_culsor2sql_pdt
VALUES('9S9X3J7D',NULL)
INSERT INTO tbl_culsor2sql_pdt
VALUES('0X9D8K9X',NULL)
GO
INSERT INTO tbl_culsor2sql_data
VALUES('1Q2A3A4Z','CNDTD7550000001')
INSERT INTO tbl_culsor2sql_data
VALUES('4F5D6R4F','CNDTD7550000002')
INSERT INTO tbl_culsor2sql_data
VALUES('9S9X3J7D','CNDTD7550000003')
INSERT INTO tbl_culsor2sql_data
VALUES('0X9D8K9X','CNDTD7550000004')
GO
SELECT sn,lbl FROM tbl_culsor2sql_pdt
GO
SELECT sn,lbl FROM tbl_culsor2sql_data
GO
/****** Cursor ******/
UPDATE tbl_culsor2sql_pdt SET lbl = NULL
-- Declare a cursor
DECLARE custom_cursor CURSOR
FOR SELECT sn, lbl FROM tbl_culsor2sql_data
-- declare a temporary various
DECLARE @sn varchar(10)
DECLARE @lbl varchar(20)
-- open the cursor
OPEN custom_cursor
--GET THE NEXT RECORD
FETCH NEXT FROM custom_cursor INTO @sn , @lbl
-- GET SUCCESSFULLY
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE tbl_culsor2sql_pdt
SET lbl = @lbl
WHERE SN = @sn
FETCH NEXT FROM custom_cursor INTO @sn, @lbl
END
--DELETE THE CURSOR
DEALLOCATE custom_cursor
/******
10 query plan
5 table scan
5 table update
5 cluster index insert on tempdb
******/
/****** SQL ******/
UPDATE tbl_culsor2sql_pdt SET lbl = NULL
GO
UPDATE tbl_culsor2sql_pdt
SET lbl = DATA.lbl
FROM tbl_culsor2sql_pdt AS PDT
INNER JOIN tbl_culsor2sql_data AS DATA
ON PDT.sn = DATA.sn
/******
1 query plan
2 table scan
1 table update
******/
USE SQL441
GO
CREATE TABLE tbl_culsor2sql_pdt
(
sn nvarchar(10) not null,
lbl nvarchar(20) null
)
GO
CREATE TABLE tbl_culsor2sql_data
(
sn nvarchar(10) not null,
lbl nvarchar(20) null
)
GO
INSERT INTO tbl_culsor2sql_pdt
VALUES('1Q2A3A4Z',NULL)
INSERT INTO tbl_culsor2sql_pdt
VALUES('4F5D6R4F',NULL)
INSERT INTO tbl_culsor2sql_pdt
VALUES('9S9X3J7D',NULL)
INSERT INTO tbl_culsor2sql_pdt
VALUES('0X9D8K9X',NULL)
GO
INSERT INTO tbl_culsor2sql_data
VALUES('1Q2A3A4Z','CNDTD7550000001')
INSERT INTO tbl_culsor2sql_data
VALUES('4F5D6R4F','CNDTD7550000002')
INSERT INTO tbl_culsor2sql_data
VALUES('9S9X3J7D','CNDTD7550000003')
INSERT INTO tbl_culsor2sql_data
VALUES('0X9D8K9X','CNDTD7550000004')
GO
SELECT sn,lbl FROM tbl_culsor2sql_pdt
GO
SELECT sn,lbl FROM tbl_culsor2sql_data
GO
/****** Cursor ******/
UPDATE tbl_culsor2sql_pdt SET lbl = NULL
-- Declare a cursor
DECLARE custom_cursor CURSOR
FOR SELECT sn, lbl FROM tbl_culsor2sql_data
-- declare a temporary various
DECLARE @sn varchar(10)
DECLARE @lbl varchar(20)
-- open the cursor
OPEN custom_cursor
--GET THE NEXT RECORD
FETCH NEXT FROM custom_cursor INTO @sn , @lbl
-- GET SUCCESSFULLY
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE tbl_culsor2sql_pdt
SET lbl = @lbl
WHERE SN = @sn
FETCH NEXT FROM custom_cursor INTO @sn, @lbl
END
--DELETE THE CURSOR
DEALLOCATE custom_cursor
/******
10 query plan
5 table scan
5 table update
5 cluster index insert on tempdb
******/
/****** SQL ******/
UPDATE tbl_culsor2sql_pdt SET lbl = NULL
GO
UPDATE tbl_culsor2sql_pdt
SET lbl = DATA.lbl
FROM tbl_culsor2sql_pdt AS PDT
INNER JOIN tbl_culsor2sql_data AS DATA
ON PDT.sn = DATA.sn
/******
1 query plan
2 table scan
1 table update
******/