Inquiridor
Como fazer certo número de registros em SQL SERVER.

Pergunta
-
Pessoal. Tudo bem. Estou tentando fazer 4(pode ser 10, 100, 1000) registros em uma tabela, e a cada 4 registro tem q muda a letra.
1 - a
2 - a
3 - a
4 - a
-----
1 - b
2 - b
3 - b
4 - b
E assim por diante. o código esta a serguir.declare
@num_pilotagem int, @cod_pilotagem_max int,
@des_serie_pilotagem char(5), @contador int
select
@cod_pilotagem_max = isnull(tp.cod_pilotagem, 0),
@des_serie_pilotagem = isnull(tp.des_serie_pilotagem, 'A')
from
tb_pilotagem tp with (updlock)
where
tp.cod_pilotagem = (select max(t1.cod_pilotagem) from tb_pilotagem t1)-- Penúltimo Identity
set
@cod_pilotagem_max =
(case
when (@cod_pilotagem_max > 1) then
(select max(t1.cod_pilotagem) from tb_pilotagem t1
where t1.cod_pilotagem <= @cod_pilotagem_max-1)
else 0
end)-- Último Número de Pilotagem do Último Identity
set
@num_pilotagem =
(case
when (@cod_pilotagem_max <> 0) then
(select t1.num_pilotagem from tb_pilotagem t1
where t1.cod_pilotagem = @cod_pilotagem_max)
else 0
end)
if (@num_pilotagem < 4)
begin
update tb_pilotagem set
num_pilotagem = @num_pilotagem + 1,
des_serie_pilotagem = char(ASCII(@des_serie_pilotagem))
from
inserted ti, tb_pilotagem tp
where
tp.cod_pilotagem = ti.cod_pilotagem
if @@error <> 0 goto erro1
end
else begin
update tb_pilotagem set
num_pilotagem = 1,
des_serie_pilotagem = char(ASCII(@des_serie_pilotagem))+1
from
inserted ti, tb_pilotagem tp
where
tp.cod_pilotagem = @cod_pilotagem_max
if @@error <> 0 goto erro1
end
Todas as Respostas
-
Bom Dia,
Não entendi a dúvida, você poderia dar mais detalhes ?
Acredito também que a versão utilizada do SQL Server seja fundamental para ajudá-lo (não foi informada).[ ]s,
Gustavo Maia Aguiar
http://gustavomaiaaguiar.spaces.live.comComo validar os nomes das colunas durante a criação de uma tabela – Parte I
http://gustavomaiaaguiar.spaces.live.com/blog/cns!F4F5C630410B9865!1100.entry
Classifique as respostas. O seu feedback é imprescindível -
Certo, vou explicar melhor esse problema. Estou usando sql server 2005. Na minha trigger de Insert, qdo o usuario cadastrar 5 registros, por exemplo poderia ser 10.000 registros, o quinto regitro tem q ser 1 e a serie muda a cada 4 registros(ou 10, 1000, 10000):
PK nro_pilotagem serie
1 1 A
2 2 A
3 3 A
4 4 A
5 1 B
6 2 B
7 3 B
8 4 B
9 1 C
10 2 C
.....etc -
Michelangelo, segue um exemplo... É preciso matematica, mudança de base, etc, nada dificil, mas se quiser aumentar a quantidade de caracteres, precisa calcular mais um pouco...
Create Function fn_SerieBase(@i int, @Base Int)
Returns @NumeroSerie Table
(Caracter VarChar(2),
Sequencia Int Default 0)Begin
Insert Into @NumeroSerie
Select char(((@i - 1) / (26 * @Base)) % 26 + 65) +
char(((@i - 1) / @Base) % 26 + 65),
Case When (@i % @Base) = 0 Then @Base Else (@i % @Base) EndReturn
End
Select 1, * from dbo.fn_SerieBase(1, 4)
Union
Select 2, * from dbo.fn_SerieBase(2, 4)
Union
Select 3, * from dbo.fn_SerieBase(3, 4)
Union
Select 4, * from dbo.fn_SerieBase(4, 4)
Union
Select 5, * from dbo.fn_SerieBase(5, 4)
Union
Select 6, * from dbo.fn_SerieBase(6, 4)
Union
Select 7, * from dbo.fn_SerieBase(7, 4)
Union
Select 8, * from dbo.fn_SerieBase(8, 4)
Union
Select 9, * from dbo.fn_SerieBase(9, 4)
Union
Select 10, * from dbo.fn_SerieBase(10, 4)
Tks. Fausto Fiorese Branco MCTS, MCITP/DBA 2005 | MCITP/DBA 2008 São Paulo - Brasil * http://www.linkedin.com/in/faustobranco -
Micheangelo,
Como você esta controlando o número do registro em sua table? Existem algum campo que possui o número do registro?
Ou você esta utilizando funções de Ranking, claro se você estiver trabalhando com o SQL Server 2005 ou 2008!!!
Pedro Antonio Galvão Junior [MVP | Microsoft Evangelist | Microsoft Partner | Engenheiro de Softwares | Especialista em Banco de Dados | SorBR.Net | Professor Universitário] -
Desculpe por demorar a responder. Meu chefe consegui resolver, mas o solução ficou limitada...
na trigger de insert ele vez...
-- Pegar Valores
-- Último Cdigo Identity
select
@cod_pilotagem_max = i.cod_pilotagem,
@nro_pilotagem = i.num_pilotagem
from
inserted i
set @des_serie_pilotagem =
isnull((select max(des_serie_pilotagem) from tb_pilotagem),'A')
set @num_pilotagem =
isnull((select max(num_pilotagem) from tb_pilotagem where des_serie_pilotagem = @des_serie_pilotagem), 0)+ 1
if @num_pilotagem > 100000
begin
set @des_serie_pilotagem = char(ASCII(@des_serie_pilotagem) + 1)
set @num_pilotagem = 1
end
if (@nro_pilotagem is null)
begin
update tb_pilotagem set
num_pilotagem = @num_pilotagem,
des_serie_pilotagem = @des_serie_pilotagem
from
inserted ti, tb_pilotagem tp
where
tp.cod_pilotagem = ti.cod_pilotagem
__________________________________________________________________Obrigado a todos.