My knowledge in SQL not so far, please help me with my issue:
我的SQL知识到目前为止还没有,请帮助我解决我的问题:
I have a script that generates some data. I need to insert this data into a table with following structure:
我有一个生成数据的脚本。我需要将这些数据插入到一个具有以下结构的表中:
Variable_Sample (idsample, idvariable, value), idsample and idvariable - PK.
Here is the script:
这是脚本:
Declare params cursor for
Select distinct id_variable from [UIR_DB].[dbo].[Variable_Values]
open params
Declare @idparam int
Declare @csql nvarchar(max) = ''
Declare @csql2 nvarchar(max) = ''
Declare @i int = 1
fetch next from params into @idparam
while @@FETCH_STATUS = 0
begin
Select @csql2 = @csql2 + ', param' +LTrim(Str(@i))
Select @csql = @csql + ' (Select value as param'+LTrim(Str(@i))+' from [UIR_DB].[dbo].[Variable_Values] where id_variable = '+LTrim(Str(@idparam))+') a'+LTrim(Str(@i))+' cross join'
Set @i = @i+1
fetch next from params into @idparam
end
Select @csql = '
SELECT id, CAST(SubString(Param,6,LEN(Param)-5) as int) param_id, param_val
FROM
(Select ROW_Number() over(order by '+SubString(@csql2,2,LEN(@csql2)-1)+') id, '+SubString(@csql2,2,LEN(@csql2)-1)+' from '+SubString(@csql,1,LEN(@csql)-11) + ') p
UNPIVOT
(param_val FOR Param IN
('+SubString(@csql2,2,LEN(@csql2)-1)+')
)AS unpvt
'
print @csql
exec sp_executesql @csql
close params
deallocate params
Here is script result:
这是脚本的结果:
.
。
So I need to put script result into table Variable_Sample(idsample, idvariable, value)
, where (idsample, idvariable
) are the primary key of the table.
因此,我需要将脚本结果放入表Variable_Sample(idsample、idvariable、value)中,其中(idsample、idvariable)是表的主键。
2 个解决方案
#1
1
I think all you need to is: Add an insert to @csql
我认为您需要做的就是:向@csql添加一个insert
e.g.
如。
Insert into variable_sample(idsample, idvariable, value)
SELECT id, CAST(SubString(Param,6,LEN(Param)-5) as int) param_id, param_val
FROM
(Select ROW_Number() over(order by '+SubString(@csql2,2,LEN(@csql2)-1)+') id, '+SubString(@csql2,2,LEN(@csql2)-1)+' from '+SubString(@csql,1,LEN(@csql)-11) + ') p
UNPIVOT
(param_val FOR Param IN
('+SubString(@csql2,2,LEN(@csql2)-1)+')
)AS unpvt
That's assuming that table already exsists.
这是假设该表已经存在。
#2
2
You can add this to the end of the script, instead of the EXEC sp_executesql @csql
:
您可以将其添加到脚本末尾,而不是EXEC sp_executesql @csql:
CREATE TABLE #temptable (idsample INT, idvariable INT, value INT)
INSERT INTO #temptable EXEC @csql
SELECT idsample, idvariable, value FROM #temptable
DROP TABLE #temptable
#1
1
I think all you need to is: Add an insert to @csql
我认为您需要做的就是:向@csql添加一个insert
e.g.
如。
Insert into variable_sample(idsample, idvariable, value)
SELECT id, CAST(SubString(Param,6,LEN(Param)-5) as int) param_id, param_val
FROM
(Select ROW_Number() over(order by '+SubString(@csql2,2,LEN(@csql2)-1)+') id, '+SubString(@csql2,2,LEN(@csql2)-1)+' from '+SubString(@csql,1,LEN(@csql)-11) + ') p
UNPIVOT
(param_val FOR Param IN
('+SubString(@csql2,2,LEN(@csql2)-1)+')
)AS unpvt
That's assuming that table already exsists.
这是假设该表已经存在。
#2
2
You can add this to the end of the script, instead of the EXEC sp_executesql @csql
:
您可以将其添加到脚本末尾,而不是EXEC sp_executesql @csql:
CREATE TABLE #temptable (idsample INT, idvariable INT, value INT)
INSERT INTO #temptable EXEC @csql
SELECT idsample, idvariable, value FROM #temptable
DROP TABLE #temptable