Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 将sql子查询转换为list或varchar_Sql Server_Sql Server 2005 - Fatal编程技术网

Sql server 将sql子查询转换为list或varchar

Sql server 将sql子查询转换为list或varchar,sql-server,sql-server-2005,Sql Server,Sql Server 2005,我想知道是否可以将子查询结果转换为varchar数据类型中的逗号分隔列表 例如。 如果我有一个产品表。我有一个产品图片表,上面有产品的外键。 现在我想用select查询列出所有产品,该查询应该有一列,列中包含每个产品的productImage表的pk列表 我正在使用SQLServer2005。我们能否以任何方式实现上述目标 不容易,不。我发现做这些事情的最好方法是自己执行子查询,并以编程方式组装逗号分隔的列表 如果确实需要在数据库上执行此操作,可以定义一个标量值函数,为外键提供值,并返回逗号分隔

我想知道是否可以将子查询结果转换为varchar数据类型中的逗号分隔列表

例如。 如果我有一个产品表。我有一个产品图片表,上面有产品的外键。 现在我想用select查询列出所有产品,该查询应该有一列,列中包含每个产品的productImage表的pk列表


我正在使用SQLServer2005。我们能否以任何方式实现上述目标

不容易,不。我发现做这些事情的最好方法是自己执行子查询,并以编程方式组装逗号分隔的列表


如果确实需要在数据库上执行此操作,可以定义一个标量值函数,为外键提供值,并返回逗号分隔的列表。不过,您必须在该函数中使用游标才能实现神奇的效果。

不容易,不。我发现最好的方法是自行执行子查询,并以编程方式组装逗号分隔的列表


如果确实需要在数据库上执行此操作,可以定义一个标量值函数,为外键提供值,并返回逗号分隔的列表。不过,您必须在该函数中使用一个光标,才能实现神奇的效果。

我不确定是否在跟踪您,但您可以尝试添加一个额外的表:

表产品 表格图像 表产品图片

在最后一个表中,您将至少有3列,分别是Product_Images表的PK、Product_FK和 图像。然后就

SELECT Image_FK FROM Product_Images WHERE Product_Images.Product_FK = ##;
您将有一个与产品关联的图像主键列表

希望这能对你有所帮助,
问候。

我不确定我是否在跟踪你,但也许你可以尝试添加一个额外的表:

表产品 表格图像 表产品图片

在最后一个表中,您将至少有3列,分别是Product_Images表的PK、Product_FK和 图像。然后就

SELECT Image_FK FROM Product_Images WHERE Product_Images.Product_FK = ##;
您将有一个与产品关联的图像主键列表

希望这能对你有所帮助, 问候

以下是我用于此查询的测试数据

Declare @Products Table (ProductID int primary key, ProductName varchar(20))        
Declare @ProductImages Table (ProductId int, ImageId int, Primary Key (ProductId, ImageId))

Insert Into @Products 
Select 1, 'Product1' Union all
Select 2, 'Product1' Union all
Select 3, 'Product1' Union all
Select 4, 'Product1' Union all
Select 5, 'Product1' 

Insert Into @ProductImages
Select 1,1 Union all
Select 1,2 Union all
Select 1,3 Union all
Select 2,4 Union all
Select 2,5 Union all
Select 3,1 Union all
Select 4,3 Union all
Select 4,5 
以下是查询结果:

ProductID ImageList
--------- ---------
        1 1,2,3
        2 4,5
        3 1
        4 3,5
如果您想让ProductID 5在图像列表为null的列表中,只需从查询中删除下一行:

Where p.ProductID in (Select ProductID From @ProductImages)
结果中将多出一行(未指定图像):

以下是我用于此查询的测试数据

Declare @Products Table (ProductID int primary key, ProductName varchar(20))        
Declare @ProductImages Table (ProductId int, ImageId int, Primary Key (ProductId, ImageId))

Insert Into @Products 
Select 1, 'Product1' Union all
Select 2, 'Product1' Union all
Select 3, 'Product1' Union all
Select 4, 'Product1' Union all
Select 5, 'Product1' 

Insert Into @ProductImages
Select 1,1 Union all
Select 1,2 Union all
Select 1,3 Union all
Select 2,4 Union all
Select 2,5 Union all
Select 3,1 Union all
Select 4,3 Union all
Select 4,5 
以下是查询结果:

ProductID ImageList
--------- ---------
        1 1,2,3
        2 4,5
        3 1
        4 3,5
如果您想让ProductID 5在图像列表为null的列表中,只需从查询中删除下一行:

Where p.ProductID in (Select ProductID From @ProductImages)
结果中将多出一行(未指定图像):


副本?不管怎样,那条线索有你想要的答案可能是重复的?不管怎样,这个线程有一个答案,你正在寻找的可能的副本,没有必要也没有游标,没有必要也没有游标,没有必要也没有游标,也没有UDFsPerfect。非常感谢你!完美的非常感谢你!