while語(yǔ)句是SQL中最常見的循環(huán)語(yǔ)句之一,下面就將為您介紹sql中while語(yǔ)句多層循環(huán)的實(shí)例,供您參考,希望對(duì)您學(xué)習(xí)SQL中的循環(huán)語(yǔ)句能夠有所幫助。
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