当SELECT处于循环中时如何只获得一个输出?

时间:2014-05-16 06:03:54

标签: sql sql-server

这是我的代码,最后你可以找到输出。基本上有2个输出。 那么,我怎样才能在这种情况下得到一个输出:

SQL

DECLARE 
@SubcategoriaId [nvarchar] (15),
@Nombre [nvarchar](50),
@Descripcion [nvarchar](100),
@CantProd [numeric] (4,0)

-- Declaring the cursor
DECLARE XX_Cursor CURSOR
FOR SELECT subcategoria.SubcategoriaId, subcategoria.Nombre, subcategoria.Descripcion
FROM subcategoria 
WHERE subcategoria.Categoria = 'Ca_2E006CC3'


OPEN XX_Cursor

-- I save the first cursor result into the variables @SubcategoriaId, @Nombre, @Descripcion
FETCH NEXT FROM XX_Cursor INTO @SubcategoriaId, @Nombre, @Descripcion


WHILE (@@FETCH_STATUS = 0)
BEGIN

    SET @CantProd = (SELECT count(*) FROM TEST_COLUMN)

   -- with this select I will get a lot of rows but on a different requests because this is in a loop 
   Select @SubcategoriaId AS ID, @Nombre AS Name, @Descripcion AS Description, @CantProd AS Cant

   FETCH NEXT FROM XX_Cursor INTO @SubcategoriaId, @Nombre, @Descripcion
END

CLOSE XX_Cursor
DEALLOCATE XX_Cursor

输出

这是输出:

ID          | Name                            | Description                     | Cant   
Sc_7EAFEDED | Desc Subcategoria desde WEB 2.2 | Desc Subcategoria desde WEB 2.2 |  3

ID          | Name                            | Description                     | Cant   
Sc_7EAFEKMD | Desc Subcategoria desde WEB 1.1 | Desc Subcategoria desde WEB 1.1 |  1

但我需要这样的事情:

ID          | Name                            | Description                     | Cant   
Sc_7EAFEDED | Desc Subcategoria desde WEB 2.2 | Desc Subcategoria desde WEB 2.2 |  3 
Sc_7EAFEKMD | Desc Subcategoria desde WEB 1.1 | Desc Subcategoria desde WEB 1.1 |  1

2 个答案:

答案 0 :(得分:0)

您创建一个变量表并将数据放在其上,然后从temptable中选择内容

您也可以使用#MyTempTable替换临时表的@MyTempTable

DECLARE 
@SubcategoriaId [nvarchar] (15),
@Nombre [nvarchar](50),
@Descripcion [nvarchar](100),
@CantProd [numeric] (4,0)

DECLARE @MyTempTable TABLE (ID varchar(255), Name  varchar(255), Description varchar(255), Cant INT)

-- Declaring the cursor
DECLARE XX_Cursor CURSOR
FOR SELECT subcategoria.SubcategoriaId, subcategoria.Nombre, subcategoria.Descripcion
FROM subcategoria 
WHERE subcategoria.Categoria = 'Ca_2E006CC3'


OPEN XX_Cursor

-- I save the first cursor result into the variables @SubcategoriaId, @Nombre, @Descripcion
FETCH NEXT FROM XX_Cursor INTO @SubcategoriaId, @Nombre, @Descripcion


WHILE (@@FETCH_STATUS = 0)
BEGIN

    SET @CantProd = (SELECT count(*) FROM TEST_COLUMN)

   -- with this select I will get a lot of rows but on a different requests because this is in a loop 
INSERT INTO @MyTempTable (ID, Name, Description, Cant)
   Select @SubcategoriaId AS ID, @Nombre AS Name, @Descripcion AS Description, @CantProd AS Cant

   FETCH NEXT FROM XX_Cursor INTO @SubcategoriaId, @Nombre, @Descripcion
END

CLOSE XX_Cursor
DEALLOCATE XX_Cursor

SELECT * FROM @MyTempTable 

答案 1 :(得分:0)

如何摆脱光标。关于使用或不使用游标存在很多争议,但是不应该使用查询数据游标。以下代码适用于MSSQL 2005和更新版本,但查询也可以用于旧版本:

 SELECT 
     subcategoria.SubcategoriaId
     , subcategoria.Nombre
     , subcategoria.Descripcion
 FROM subcategoria sc
 cross apply (
   SELECT count(*) FROM TEST_COLUMN WHERE ...... (I do not know what table is this but I guess you missed some where clauses)
 )
 WHERE subcategoria.Categoria = 'Ca_2E006CC3'