while语句是SQL中最常见的循环语句之一,下面就将为您介绍sql中while语句多层循环的实例,供您参考,希望对您学习SQL中的循环语句能够有所帮助。
DECLARE @Name nvarchar(20);
DECLARE @Type varchar(20);
DECLARE @Count int ;
DECLARE @IsCommend bit ;
DECLARE @IsStatic bit ;
set @Name ='asp,php,asp.net,jsp'
set @Type ='asp,php,asp.net,jsp'
set @Count =0
set @IsCommend ='false'
set @IsStatic ='true'
DECLARE @Names nvarchar(500);
DECLARE @Types nvarchar(100);
DECLARE @t1 nvarchar(10);
DECLARE @n1 nvarchar(10);
set @Names = @Name+ ','
set @Types = @Type+ ','
declare @i int;
set @i=0
BEGIN TRY
--BEGIN TRAN
while CHARINDEX(',',@Types)>0
begin
SELECT @t1=LEFT(@Types,CHARINDEX(',',@Types+',')-1),@Types=STUFF(@Types,1,CHARINDEX(',',@Types+','),'')
while CHARINDEX(',',@Names)>0
begin
SELECT @n1=LEFT(@Names,CHARINDEX(',',@Names+',')-1),@Names=STUFF(@Names,1,CHARINDEX(',',@Names+','),'')
INSERT INTO W_Keyword ([Name], [Type], [Count], IsCommend, IsStatic,EditDate)
VALUES (@n1,@t1, @Count, @IsCommend, @IsStatic,GetDate())
end
set @Names = @Name+ ','
end
--COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
DECLARE @MESSAGE NVARCHAR(4000)
SELECT @MESSAGE = ERROR_MESSAGE()
RAISERROR(@MESSAGE, 16, 1)
END CATCH