MarcusCole6833
declare @table table ( id int identity(1,1), EMPID varchar(5) , [Name] varchar(20), contact Varchar(20) , gender char(10))
insert into @table ( empid, [name], contact, gender) values ('100','John', '55555', 'M')
insert into @table ( empid, [name], contact, gender) values ('200','Kate', '44444', 'F')
insert into @table ( empid, [name], contact, gender) values ('300','Sam', '44444', 'M')
declare @kvpTable table (keyVal varchar(20), val varchar(20) , objectKey int )
declare @min int = 0, @max int = 0
select @min = min(id) , @max = max(id) from @table
declare @name varchar(20)
declare @contact varchar(20)
declare @mf char(1)
declare @emp varchar(8)
while @min <= @max
begin
select @emp= empid, @name = [name], @contact = contact , @mf = gender from @table where id = @min
insert into @kvpTable
select 'EmpID', @emp , @min
union
select 'name', @name, @min
union
select 'contact', @contact, @min
union
select 'gender' ,@mf , @min
set @min = @min + 1
end
select * from @kvpTable
CHill60
Редко, очень редко вам когда-либо нужно использовать цикл в языках, основанных на множествах, таких как SQL