Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.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 如何从select语句调用存储过程_Sql_Sql Server_Stored Procedures_Sql Function - Fatal编程技术网

Sql 如何从select语句调用存储过程

Sql 如何从select语句调用存储过程,sql,sql-server,stored-procedures,sql-function,Sql,Sql Server,Stored Procedures,Sql Function,我有一个产品表和一个变量表作为休耕表 Product Table: productId product_Name Product_Price Product_Brand ================================================== 1 Product 200 Brand Variant Table: variantID ProductId Variant_Name

我有一个产品表和一个变量表作为休耕表

Product Table:  

    productId product_Name Product_Price Product_Brand 
    ==================================================
    1         Product      200           Brand

Variant Table:

    variantID ProductId Variant_Name
    ================================
    1         1          Black
    2         1          Blue
    3         2          white
create PROCEDURE [dbo].[get_Variant_String]
(@product Id int, @Char char)
AS
BEGIN
CREATE TABLE #tmp                         
(  
   varient varchar(100)
)
inset into #tmp
select  Variant_Name form  Variant where productID=@ProductId

DECLARE @VarientString varchar(100)
set @VarientString=''
DECLARE @Temp_Varient varchar(100) 
DECLARE Cur_Variant CURSOR for select varient  from #tmp
OPEN Cur_Variant 
WHILE 1 =1
BEGIN
  FETCH NEXT FROM Cur_Variant
  INTO  @Temp_Varient
  IF @@FETCH_STATUS <> 0
  BREAK
    IF @VarientString=''
    BEGIN
      SET @VarientString=@Temp_Varient
    END
    ELSE 
    BEGIN
      SET @VarientString=@VarientString + @Char +@Temp_Varient
    END
  END
CLOSE Cur_Variant
DEALLOCATE Cur_Variant
SELECT @VarientString
END
我必须在结果中列出产品和变量,但每个产品都有4-5个变量,如果我查看,则会由于多个变量而给出大量重复数据

我想要变量字符串作为

Black,Blue,white
为此,我将一个存储过程写为fallow

Product Table:  

    productId product_Name Product_Price Product_Brand 
    ==================================================
    1         Product      200           Brand

Variant Table:

    variantID ProductId Variant_Name
    ================================
    1         1          Black
    2         1          Blue
    3         2          white
create PROCEDURE [dbo].[get_Variant_String]
(@product Id int, @Char char)
AS
BEGIN
CREATE TABLE #tmp                         
(  
   varient varchar(100)
)
inset into #tmp
select  Variant_Name form  Variant where productID=@ProductId

DECLARE @VarientString varchar(100)
set @VarientString=''
DECLARE @Temp_Varient varchar(100) 
DECLARE Cur_Variant CURSOR for select varient  from #tmp
OPEN Cur_Variant 
WHILE 1 =1
BEGIN
  FETCH NEXT FROM Cur_Variant
  INTO  @Temp_Varient
  IF @@FETCH_STATUS <> 0
  BREAK
    IF @VarientString=''
    BEGIN
      SET @VarientString=@Temp_Varient
    END
    ELSE 
    BEGIN
      SET @VarientString=@VarientString + @Char +@Temp_Varient
    END
  END
CLOSE Cur_Variant
DEALLOCATE Cur_Variant
SELECT @VarientString
END
但这是一个错误

请向我建议(每种产品)的输出方式

我也试着写函数,但我有select语句,所以它给我错误

我有一个选择


使用产品表和变量列中的列创建临时表。在该表中,首先从产品表中插入详细信息,然后创建corsor,并使用存储过程获取每个产品的变量,并从此临时表中选择产品,但我有数百个产品,因此在数百次迭代中,我必须避免这种情况。请建议我如何在上的select语句中调用上述存储过程还有其他最好的方法

这样做似乎比编写存储过程容易得多:

select p.*,
       stuff((select ',' + variant_name
              from variant v
              where v.productid = p.productid
              for xml path ('')
             ), 1, 1, ''
            ) as variants
from product p;

从sp中选择的最简单方法是使用
OPENQUERY
,但是服务器应配置为使用
sp\u serveroption
存储过程启用
数据访问。