我正在寻找一种从查询结果创建SQL语句的方法
基于包含列名,rowId和Value的查询结果,我想生成一个包含列名和该行要插入的值的查询。
示例:
Id | Row_Id | Column_Name | Value | Table
50 | 1 | Employee_Name | 'Joel' | Employee
51 | 1 | Employee_Age | '54' | Employee
52 | 1 | Address | '425 Baker Ave' | Employee
53 | 2 | Employee_Name | 'Jaret' | Employee
54 | 2 | Employee_Age | '29' | Employee
55 | 2 | Address | '423 Loma Rd' | Employee
56 | 3 | Employee_Name | 'Jolie' | Employee
57 | 3 | Employee_Age | '37' | Employee
58 | 3 | Address | '896 Baren Blvd' | Employee
59 | 4 | Location | 'Chicago' | Office
60 | 4 | Address | '264 Taler Ave' | Office
61 | 5 | Location | 'Detroit' | Office
62 | 5 | Address | '296 Forest Ln' | Office
预期结果
INSERT INTO Employee (Employee_Name, Employee_Age, Address) VALUES ('Joel', '54', '425 Baker Ave')
INSERT INTO Employee (Employee_Name, Employee_Age, Address) VALUES ('Jaret', '29', '423 Loma Rd')
INSERT INTO Employee (Employee_Name, Employee_Age, Address) VALUES ('Jolie', '37', '896 Baren Blvd')
INSERT INTO Office (Location, Address) VALUES ('Chicago', '264 Taler Ave')
INSERT INTO Office (Location, Address) VALUES ('Detroit', '296 Forest Ln')
答案 0 :(得分:1)
您可以通过一些有趣的窗口功能来做到这一点。这是一个示例。
(#x用作具有您的结构,row_id,columnName,columnValue,tableName的表)
WITH foo
AS (SELECT Id,
Row_Id,
columnName,
columnValue,
tableName,
DENSE_RANK() OVER (ORDER BY Row_Id, tableName) AS sectionNumber,
ROW_NUMBER() OVER (PARTITION BY Row_Id, tableName ORDER BY Id) AS rowNumberInSection
FROM #x)
SELECT 'INSERT INTO ' + tableName + ' (' + SUBSTRING(columnNames, 0, LEN(FEE.columnNames) - 1) + ' ) ' + ' VALUES ('
+ SUBSTRING(columnValues, 0, LEN(FEE.columnValues) - 1) + ' ) '
FROM
(
SELECT tableName,
(
SELECT columnName + ','
FROM foo f2
WHERE f2.sectionNumber = foo.sectionNumber
ORDER BY f2.rowNumberInSection
FOR XML PATH('')
) AS columnNames,
(
SELECT columnValue + ','
FROM foo f3
WHERE f3.sectionNumber = foo.sectionNumber
ORDER BY f3.rowNumberInSection
FOR XML PATH('')
) AS columnValues
FROM foo
WHERE foo.rowNumberInSection = 1
) FEE;
答案 1 :(得分:0)
我不会生成INSERT
语句。只需使用INSERT . . . SELECT
进行条件聚合插入您想要的内容:
INSERT INTO TABLENAME (Employee_Name, Employee_Age, Address)
SELECT MAX(CASE WHEN name = 'Employee_Name' THEN value end),
MAX(CASE WHEN name = 'Employee_Age' THEN value end),
MAX(CASE WHEN name = 'Address' THEN value end)
FROM <query> q
GROUP BY row_id;
答案 2 :(得分:0)
我喜欢这种类型的问题:) 这是我的解决方案(使用动态查询)
select *,cast('' as nvarchar(max)) script into #T from YourTable
declare @Id int,@OldId int =0,@script nvarchar(300),@script2 nvarchar(300)
update #T
set @Id = row_Id,
@script=Case when @Id!=@OldId then 'insert into '+[Table]+' ( '+Column_Name+')' else left(@script,len(@script)-1)+','+Column_Name+')' end,
@script2=Case when @Id!=@OldId then ' Values('''+[Value]+''')' else left(@script2,len(@script2)-1)+','''+[Value]+''')' end,
script = @script+@script2,
@OldId=@Id
select script from #T t join
(select row_Id,max(len(script)) lenScript from #T group by row_Id) X on t.row_Id=x.row_Id and X.lenScript=len(t.script)
drop table #T
答案 3 :(得分:0)
假设ID
列具有唯一值,并且Row_Id
在意义上仅对于一个INSERT INTO
而言是唯一的,则它是相同的,您可以编写查询为:
select distinct
'Insert into ' +[Table] +
'(' +
stuff((
select ',' + t.[Column_Name]
from Base t
where t.Row_Id = t1.Row_Id
order by t.[id]
for xml path('')
),1,1,'')
+') VALUES ('
+
stuff((
select ', ''' + t.[Value] + ''''
from Base t
where t.Row_Id = t1.Row_Id
order by t.[id]
for xml path('')
),1,1,'')
+ ')'
from Base T1