复制代码 代码如下:
DECLARE @temp TABLE
(
[id] INT IDENTITY(1, 1) ,
[Name] VARCHAR(10)
)
DECLARE @tempId INT ,
@tempName VARCHAR(10)
INSERT INTO @temp
VALUES ( 'a' )
INSERT INTO @temp
VALUES ( 'b' )
INSERT INTO @temp
VALUES ( 'c' )
INSERT INTO @temp
VALUES ( 'd' )
INSERT INTO @temp
VALUES ( 'e' )
WHILE EXISTS ( SELECT [id]
FROM @temp )
BEGIN
SET ROWCOUNT 1
SELECT @tempId = [id] ,
@tempName = [Name]
FROM @temp
SET ROWCOUNT 0
--delete from @temp where [id] = @tempId
PRINT 'Name:----' + @tempName
END
复制代码 代码如下:
WHILE EXISTS ( SELECT [id]
FROM @temp )
BEGIN
SELECT TOP 1
@tempId = [id] ,
@tempName = [Name]
FROM @temp
DELETE FROM @temp
WHERE [id] = @tempId
SELECT *
FROM @temp
EXEC('drop table '+)
PRINT 'Name:----' + @tempName
END
复制代码 代码如下:
--定义表变量
DECLARE @temp TABLE
(
[id] INT IDENTITY(1, 1) ,
[Name] VARCHAR(10)
)
DECLARE @tempId INT ,
@tempName VARCHAR(10)
DECLARE test_Cursor CURSOR LOCAL FOR
SELECT [id],[name] FROM @temp
--插入数据值
INSERT INTO @temp
VALUES ( 'a' )
INSERT INTO @temp
VALUES ( 'b' )
INSERT INTO @temp
VALUES ( 'c' )
INSERT INTO @temp
VALUES ( 'd' )
INSERT INTO @temp
VALUES ( 'e' )
--打开游标
OPEN test_Cursor
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM test_Cursor INTO @tempId,@tempname
PRINT 'Name:----' + @tempName
END
CLOSE test_Cursor
DEALLOCATE test_Cursor
新闻热点
疑难解答