DECLARE @ID INT
SET @ID = (select top 1 USER_REQ_JOB_ID
from T8504_USER_REQ_JOB
where JOB_GRP_ID = 160
order by LST_UPDT_TS desc)
SELECT INPUT_PARM_VAL_TX
from TBL_RPT_JOB_INPUT_PARAM
where USER_REQ_JOB_ID = @ID
这会返回以下结果:
USA
USCC
6
7
2
这五条记录是我想要分配给存储过程中使用的五个不同变量。
我正在尝试使用这样的表变量:
declare @CID table (
Region Char(3)
,Segment Char(3)
,MasterContractId int
,ctcid int
,templateid int)
insert into @CID (Region,Segment,MasterContractId,ctcid,templateid)
但是如何在这里插入5行?
答案 0 :(得分:0)
这真的很糟糕,但你可能做到的一种方法就是这样(虽然你需要将它应用到你的案例中):
http://sqlfiddle.com/#!6/d41d8/21507
declare @table TABLE (value varchar(50))
INSERT INTO @table
VALUES ('first')
INSERT INTO @table
VALUES ('second')
INSERT INTO @table
VALUES (3)
INSERT INTO @table
VALUES (4)
DECLARE @temp TABLE (id int identity(1,1), value varchar(50))
INSERT INTO @temp
SELECT [value]
FROM @table t
SELECT *
FROM @temp
DECLARE @CID TABLE (Region varchar(50), cont varchar(50), another int, andAnother int)
INSERT INTO @CID
(
Region,
cont,
another,
andAnother
)
VALUES
(
(SELECT value FROM @temp WHERE id = 1), -- Region - varchar
(SELECT value FROM @temp WHERE id = 2), -- cont - varchar
(SELECT value FROM @temp WHERE id = 3), -- another - int
(SELECT value FROM @temp WHERE id = 4) -- andAnother - int
)
SELECT * FROM @cid
请注意,我假设您使用的是mssql,但未指定
答案 1 :(得分:0)
INSERT INTO @CID
select * from
(
select
'Temp' + convert(char(1), row_number() over (order by (select 0))) as columnName,
INPUT_PARM_VAL_TX as Value
from TBL_RPT_JOB_INPUT_PARAM where USER_REQ_JOB_ID = @ID
) d
pivot
(
max(value)
for columnname in (Temp1, Temp2, Temp3, Temp4, Temp5)
) piv;
看看这是否有帮助。 请查看this fiddle示例。
提供者:
Add row number to this T-SQL query
Efficiently convert rows to columns in sql server
编辑:sql添加了一个额外的列来生成行号,以将其用作额外的列,该列以列标题为旋转。