Skip to main content
adicionou 147 caracteres ao conteúdo
Fonte Link
Tobias Mesquita
  • 23,1mil
  • 1
  • 32
  • 52

ESTATÍSTICAS DO CLIENTEESTATÍSTICAS DO CLIENTE

  • Avaliação 1: CURSOR
  • Avaliação 2: CURSOR FAST_FORWARD
  • Avaliação 3: CURSOR LOCAL FAST_FORWARD
  • Avaliação 4: WHILE LOOP WITH @TABLE

ESTATÍSTICAS DO CLIENTE

ESTATÍSTICAS DO CLIENTE

  • Avaliação 1: CURSOR
  • Avaliação 2: CURSOR FAST_FORWARD
  • Avaliação 3: CURSOR LOCAL FAST_FORWARD
  • Avaliação 4: WHILE LOOP WITH @TABLE
removeu 32 caracteres do conteúdo
Fonte Link
Tobias Mesquita
  • 23,1mil
  • 1
  • 32
  • 52
DECLARE @coluna1 uniqueidentifier;
DECLARE @coluna2 uniqueidentifier;
DECLARE @coluna3 uniqueidentifier;
DECLARE @indice int
DECLARE @count int
DECLARE @tabela table(
    RowNumber int identity,
    Coluna1 uniqueidentifier not null,
    Coluna2 uniqueidentifier not null,
    Coluna3 uniqueidentifier not null,
    PRIMARY KEY (RowNumber)
);

INSERT INTO @tabela
SELECT Coluna1, Coluna2, Coluna3 FROM CursorTeste

SET @count = (SELECT COUNT(RowNumber) FROM @tabela)
SET @indice = 1;
WHILE (@indice <= @count)
BEGIN
    SELECT 
        @indice = RowNumber + 1,
        @coluna1 = Coluna1, 
        @coluna2 = Coluna2, 
        @coluna3 = Coluna3
    FROM @tabela 
    WHERE RowNumber = @indice

    PRINT cast(@indice as varchar(50)) + ' { ' + cast(@coluna1 as varchar(50)) + ' } - { ' + cast(@coluna2 as varchar(50)) + ' } - { ' + cast(@coluna3 as varchar(50)) + ' }';
END
DECLARE @coluna1 uniqueidentifier;
DECLARE @coluna2 uniqueidentifier;
DECLARE @coluna3 uniqueidentifier;
DECLARE @indice int
DECLARE @count int
DECLARE @tabela table(
    RowNumber int identity,
    Coluna1 uniqueidentifier not null,
    Coluna2 uniqueidentifier not null,
    Coluna3 uniqueidentifier not null,
    PRIMARY KEY (RowNumber)
);

INSERT INTO @tabela
SELECT Coluna1, Coluna2, Coluna3 FROM CursorTeste

SET @count = (SELECT COUNT(RowNumber) FROM @tabela)
SET @indice = 1;
WHILE (@indice <= @count)
BEGIN
    SELECT 
        @indice = RowNumber + 1,
        @coluna1 = Coluna1, 
        @coluna2 = Coluna2, 
        @coluna3 = Coluna3
    FROM @tabela 
    WHERE RowNumber = @indice

    PRINT cast(@indice as varchar(50)) + ' { ' + cast(@coluna1 as varchar(50)) + ' } - { ' + cast(@coluna2 as varchar(50)) + ' } - { ' + cast(@coluna3 as varchar(50)) + ' }';
END
DECLARE @coluna1 uniqueidentifier;
DECLARE @coluna2 uniqueidentifier;
DECLARE @coluna3 uniqueidentifier;
DECLARE @indice int
DECLARE @count int
DECLARE @tabela table(
    RowNumber int identity,
    Coluna1 uniqueidentifier not null,
    Coluna2 uniqueidentifier not null,
    Coluna3 uniqueidentifier not null,
    PRIMARY KEY (RowNumber)
);

INSERT INTO @tabela
SELECT Coluna1, Coluna2, Coluna3 FROM CursorTeste

SET @count = (SELECT COUNT(RowNumber) FROM @tabela)
SET @indice = 1;
WHILE (@indice <= @count)
BEGIN
    SELECT 
        @indice = RowNumber + 1,
        @coluna1 = Coluna1, 
        @coluna2 = Coluna2, 
        @coluna3 = Coluna3
    FROM @tabela 
    WHERE RowNumber = @indice

    PRINT '{ ' + cast(@coluna1 as varchar(50)) + ' } - { ' + cast(@coluna2 as varchar(50)) + ' } - { ' + cast(@coluna3 as varchar(50)) + ' }';
END
adicionou 1148 caracteres ao conteúdo
Fonte Link
Tobias Mesquita
  • 23,1mil
  • 1
  • 32
  • 52

EDIT

Resolvi adicionar um exemplo completo e as estatisticas.

TABELA

CREATE TABLE [dbo].[CursorTeste](
    [CursorTesteID] [int] IDENTITY(1,1) NOT NULL,
    [Coluna1] [uniqueidentifier] NOT NULL,
    [Coluna2] [uniqueidentifier] NOT NULL,
    [Coluna3] [uniqueidentifier] NOT NULL,
 CONSTRAINT [PK_CursorTeste] PRIMARY KEY CLUSTERED 
(
    [CursorTesteID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

INSERT

DECLARE @count int;
SET @count = 0;

WHILE (@count < 10000)
BEGIN
    INSERT INTO CursorTeste VALUES (NEWID(), NEWID(), NEWID());
    SET @count = @count + 1;
END

CURSOR

DECLARE @coluna1 int;uniqueidentifier;
DECLARE @coluna2 int;uniqueidentifier;
DECLARE @coluna2@coluna3 int;uniqueidentifier;
DECLARE @CURSOR_tabela@CURSOR_teste CURSOR;

SELECTSET @CURSOR_tabela@CURSOR_teste = CURSOR LOCAL FAST_FORWARD FOR
SELECT Coluna1, Coluna2, Coluna3 
 FROM Tabela
WHERE Coluna4 = @coluna4;CursorTeste

OPEN @CURSOR_tabela;@CURSOR_teste
WHILE (1 = 1)
BEGIN
    FETCH NEXT @CURSOR_tabela
  FROM @CURSOR_teste INTO @coluna1, @coluna2, @coluna2;
    @coluna3;
    IF (@@FETCH_STATUS <> 0)
    BEGIN
        BREAK;
    END 

    
 PRINT '{ ' + cast(@coluna1 PRINTas 'Executarvarchar(50)) alguma+ operação' aqui';} - { ' + cast(@coluna2 as varchar(50)) + ' } - { ' + cast(@coluna3 as varchar(50)) + ' }';
END

CLOSE @CURSOR_tabela@CURSOR_teste   
DEALLOCATE @CURSOR_tabela@CURSOR_teste
DECLARE @coluna1 int;uniqueidentifier;
DECLARE @coluna2 int;uniqueidentifier;
DECLARE @coluna2@coluna3 int;uniqueidentifier;
DECLARE @rowNumber@indice int;int
DECLARE @count int
DECLARE @tabela table(
    RowNumber int identity,
    Coluna1 intuniqueidentifier not null,
    Coluna2 intuniqueidentifier not null,
    Coluna3 intuniqueidentifier not null,
    PRIMARY KEY (RowNumber)
);

INSERT INTO @tabela
SELECT Coluna1, Coluna2, Coluna3 
 FROM TabelaCursorTeste
WHERE
SET Coluna4@count = @coluna4;
(SELECT COUNT(RowNumber) FROM @tabela)
SET @rowNumber@indice = 1;
WHILE (1@indice =<= 1@count)
BEGIN
    SELECT 
        @rowNumber@indice = RowNumber + 1,
        @coluna1 = Coluna1, 
        @coluna2 = Coluna2, 
        @coluna3 = Coluna3
    FROM 
  @tabela  
    WHERE RowNumber = @rowNumber@indice

    PRINT cast(@indice as varchar(50)) 
 + ' { ' IF+ cast(@coluna1 isas nullvarchar(50)
    BEGIN
 ) + ' } - { ' BREAK;
+ cast(@coluna2 as varchar(50)) END
+ ' } - 
 { ' + cast(@coluna3 PRINTas 'Executarvarchar(50)) alguma+ operação' aqui';}';
END

ESTATÍSTICAS DO CLIENTE

CURSOR

DECLARE @coluna1 int;
DECLARE @coluna2 int;
DECLARE @coluna2 int;
DECLARE @CURSOR_tabela CURSOR;

SELECT @CURSOR_tabela = CURSOR LOCAL FAST_FORWARD FOR
SELECT Coluna1, Coluna2, Coluna3 
 FROM Tabela
WHERE Coluna4 = @coluna4;

OPEN @CURSOR_tabela;
WHILE (1 = 1)
BEGIN
    FETCH NEXT @CURSOR_tabela
    INTO @coluna1, @coluna2, @coluna2;
    
    IF (@@FETCH_STATUS <> 0)
    BEGIN
        BREAK;
    END
    
     PRINT 'Executar alguma operação aqui';
END

CLOSE @CURSOR_tabela
DEALLOCATE @CURSOR_tabela
DECLARE @coluna1 int;
DECLARE @coluna2 int;
DECLARE @coluna2 int;
DECLARE @rowNumber int;
DECLARE @tabela table(
    RowNumber int identity,
    Coluna1 int not null,
    Coluna2 int not null,
    Coluna3 int not null,
    PRIMARY KEY(RowNumber)
);

INSERT INTO @tabela
SELECT Coluna1, Coluna2, Coluna3 
 FROM Tabela
WHERE Coluna4 = @coluna4;

SET @rowNumber = 1;
WHILE (1 = 1)
BEGIN
    SELECT 
        @rowNumber = RowNumber + 1,
        @coluna1 = Coluna1,
        @coluna2 = Coluna2,
        @coluna3 = Coluna3
    FROM 
        RowNumber = @rowNumber
        
     IF (@coluna1 is null)
    BEGIN
        BREAK;
    END
    
     PRINT 'Executar alguma operação aqui';
END

EDIT

Resolvi adicionar um exemplo completo e as estatisticas.

TABELA

CREATE TABLE [dbo].[CursorTeste](
    [CursorTesteID] [int] IDENTITY(1,1) NOT NULL,
    [Coluna1] [uniqueidentifier] NOT NULL,
    [Coluna2] [uniqueidentifier] NOT NULL,
    [Coluna3] [uniqueidentifier] NOT NULL,
 CONSTRAINT [PK_CursorTeste] PRIMARY KEY CLUSTERED 
(
    [CursorTesteID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

INSERT

DECLARE @count int;
SET @count = 0;

WHILE (@count < 10000)
BEGIN
    INSERT INTO CursorTeste VALUES (NEWID(), NEWID(), NEWID());
    SET @count = @count + 1;
END

CURSOR

DECLARE @coluna1 uniqueidentifier;
DECLARE @coluna2 uniqueidentifier;
DECLARE @coluna3 uniqueidentifier;
DECLARE @CURSOR_teste CURSOR;

SET @CURSOR_teste = CURSOR LOCAL FAST_FORWARD FOR
SELECT Coluna1, Coluna2, Coluna3 FROM CursorTeste

OPEN @CURSOR_teste
WHILE (1 = 1)
BEGIN
    FETCH NEXT FROM @CURSOR_teste INTO @coluna1, @coluna2, @coluna3;
    IF (@@FETCH_STATUS <> 0)
    BEGIN
        BREAK;
    END 

    PRINT '{ ' + cast(@coluna1 as varchar(50)) + ' } - { ' + cast(@coluna2 as varchar(50)) + ' } - { ' + cast(@coluna3 as varchar(50)) + ' }';
END

CLOSE @CURSOR_teste   
DEALLOCATE @CURSOR_teste
DECLARE @coluna1 uniqueidentifier;
DECLARE @coluna2 uniqueidentifier;
DECLARE @coluna3 uniqueidentifier;
DECLARE @indice int
DECLARE @count int
DECLARE @tabela table(
    RowNumber int identity,
    Coluna1 uniqueidentifier not null,
    Coluna2 uniqueidentifier not null,
    Coluna3 uniqueidentifier not null,
    PRIMARY KEY (RowNumber)
);

INSERT INTO @tabela
SELECT Coluna1, Coluna2, Coluna3 FROM CursorTeste

SET @count = (SELECT COUNT(RowNumber) FROM @tabela)
SET @indice = 1;
WHILE (@indice <= @count)
BEGIN
    SELECT 
        @indice = RowNumber + 1,
        @coluna1 = Coluna1, 
        @coluna2 = Coluna2, 
        @coluna3 = Coluna3
    FROM @tabela  
    WHERE RowNumber = @indice

    PRINT cast(@indice as varchar(50)) + ' { ' + cast(@coluna1 as varchar(50)) + ' } - { ' + cast(@coluna2 as varchar(50)) + ' } - { ' + cast(@coluna3 as varchar(50)) + ' }';
END

ESTATÍSTICAS DO CLIENTE

Fonte Link
Tobias Mesquita
  • 23,1mil
  • 1
  • 32
  • 52
Carregando