这是我的表有2列和5条记录。如何在临时表的2.5行中显示这5条记录。表示从该表中获取2行,并在存储过程中插入1行的临时表,其中包含4列
第二张图片是临时表我想要数据。如果将来我想在3行中显示10条记录,那么临时表中将有3.1行
表名Party_Details
PID PartyName Address
------------------------------------
1 Shadab Bareilly
2 Akbar Moradabad
3 Aqib Bareilly
4 Majid Moradabad
5 Shaan Bareilly
上表有5行。
如何在SQL Server存储过程的临时表的2.5行中显示它?
@GeorgeJoseph
如果我将它们显示为1,则临时表中将有10列,如
PartyName1,Address1,PartyName2,Address2............. PartyName5,Address5
答案 0 :(得分:0)
create procedure SP_Party_Details
as
begin
declare @id int
set @id=(select COUNT(*) from Party_Details)
DECLARE @tempTable TABLE(
PID int primary key identity(1,1),
PartyName varchar(20),
Address varchar(50),
PartyName1 varchar(20),
Address1 varchar(50));
DECLARE @I INT=(select Min(PId) from Party_Details);
START:
If @I%2!=0
insert into @tempTable(PartyName,Address) select p.PartyName, p.Address from Party_Details p where p.PId=@I
else
update t set t.PartyName1=(select PartyName from Party_Details where PId=@I),
t.Address1=(select Address from Party_Details where PId=@I) from @tempTable t where t.PId=(select max(pid) from @tempTable)
SET @I+=1;
IF @I<=@id GOTO START;
SELECT * FROM @tempTable;
end
exec SP_Party_Details
答案 1 :(得分:0)
6列
create procedure [dbo].[SP_Party_Details_Three_Step]
as
begin
declare @id int
set @id=(select COUNT(*) from Party_Details)
DECLARE @tempTable TABLE(
PID int primary key identity(1,1),
PartyName varchar(20),
Address varchar(50),
PartyName1 varchar(20),
Address1 varchar(50),
PartyName2 varchar(20),
Address2 varchar(50));
DECLARE @I INT=(select Min(PId) from Party_Details);
START:
If((select Count(*) from @tempTable where PartyName2 is null)=0)
insert into @tempTable(PartyName,Address) select p.PartyName, p.Address from Party_Details p where p.PId=@I
else if((select Count(*) from @tempTable where PartyName1 is null)>0)
update t set t.PartyName1=(select PartyName from Party_Details where PId=@I),
t.Address1=(select Address from Party_Details where PId=@I) from @tempTable t where t.PId=(select max(pid) from @tempTable)
else
update t set t.PartyName2=(select PartyName from Party_Details where PId=@I),
t.Address2=(select Address from Party_Details where PId=@I) from @tempTable t where t.PId=(select max(pid) from @tempTable)
PRINT @I;
SET @I+=1;
IF @I<=@id GOTO START;
SELECT * FROM @tempTable;
end
exec [SP_Party_Details_Three_Step]