[英]How to assign an array to a variable of a sql query
我希望将查询结果(“ A”)作为数组分配到另一个查询(“ B”)的自定义变量中。 我意识到sql中不允许使用数组变量,因此我希望在JSON中进行操作。 这是示例:
Query Result A:
Staff ID | Mariage status | Kids Details |
I022144 | yes | |
I062541 | yes | |
I322411 | yes | |
I445211 | no | |
D235544 | yes | |
Query Result B:
Staff ID | Kids Name | Kids Gender | Kids Age |
I022144 | Pete | M | 3 |
I022144 | Sarah | F | 5 |
I062541 | Don | M | 10 |
I322411 | Keith | M | 9 |
D235544 | John | M | 2 |
D235544 | Nancy | F | 13 |
D235544 | Don | M | 1 |
JSON中的预期结果
Dataset: [
{"Staff ID": I022144, "Mariage status": yes, "Kids Details": [{"Kids Name": Pete, "Kids Gender": M, "Kids Age": 3}, {"Kids Name": Sarah, "Kids Gender": F, "Kids Age": 5}]},
{"Staff ID": I062541, "Mariage status": yes, "Kids Details": [{"Kids Name": Don, "Kids Gender": M, "Kids Age": 10}]},
{"Staff ID": I322411, "Mariage status": yes, "Kids Details": [{"Kids Name": Keith, "Kids Gender": M, "Kids Age": 9}]},
{"Staff ID": I445211, "Mariage status": no, "Kids Details": []},
{"Staff ID": D235544, "Mariage status": yes, "Kids Details": [{"Kids Name": John, "Kids Gender": M, "Kids Age": 2}, {"Kids Name": Nancy, "Kids Gender": F, "Kids Age": 13}, {"Kids Name": Don, "Kids Gender": M, "Kids Age": 1}]}
]
感谢您的指导。 :)
家伙
You actualy can use array variables in SQL, You can do this
DECLARE @TableVariableName TABLE(
IDs VARCHAR(100),
numbers Int
);
Insert into @TableVariableName
-----Do the select you want it to store it into the new table for example:
Select IDs,numbers from tbl1
然后,可以在另一个查询中使用此表,如下所示:
Select tbl2.column1, @TableVariableName.IDs, @TableVariableName.numbers from tbl2
join @TableVariableName ON tbl2.IDs = @TableVariableName.IDs
妳去 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.