Friday, March 9, 2012

Retrieving @@Identity

Hi,
I have the following proc:
CREATE PROCEDURE SP_CadastraPessoaFisica
(
@.Email Varchar(60) = '',
@.Senha Varchar(10) = '',
@.Nome Varchar(50) = '',
@.Sobrenome Varchar(50) = '',
@.DataNascimento DateTime,
@.Sexo Char(1),
@.CPF Varchar(12)
)
As
Set Nocount On
Insert Into Usuario(Email,Senha) Values(@.Email,@.Senha)
GO
Simple.Isn't it ? So...I need to retrieve the ID from this insert command
when inserted. Should I use @.@.Identity ? How do i do ? I don't know how to
use @.@.Identity!
Thanks in advance,
Daniel GrohDaniel
If you are using SQL Server 2000 you can use SCOPE_IDENITY() function
INSERT INTO Table (col) VALUES (1)
SELECT SCOPE_IDENITY()
Note : there is a difference between @.@.IDENTITY and SCOPE_IDENTITY() which
may bring unexpected result
See BOL for details
"Daniel Groh" <newsgroupms@.gmail.com> wrote in message
news:u4mnT$8TFHA.3308@.TK2MSFTNGP14.phx.gbl...
> Hi,
> I have the following proc:
> CREATE PROCEDURE SP_CadastraPessoaFisica
> (
> @.Email Varchar(60) = '',
> @.Senha Varchar(10) = '',
> @.Nome Varchar(50) = '',
> @.Sobrenome Varchar(50) = '',
> @.DataNascimento DateTime,
> @.Sexo Char(1),
> @.CPF Varchar(12)
> )
> As
> Set Nocount On
> Insert Into Usuario(Email,Senha) Values(@.Email,@.Senha)
> GO
> Simple.Isn't it ? So...I need to retrieve the ID from this insert command
> when inserted. Should I use @.@.Identity ? How do i do ? I don't know how to
> use @.@.Identity!
> --
> Thanks in advance,
> Daniel Groh
>|||Please don't multipost ,it is answered in .programming
"Daniel Groh" <newsgroupms@.gmail.com> wrote in message
news:u4mnT$8TFHA.3308@.TK2MSFTNGP14.phx.gbl...
> Hi,
> I have the following proc:
> CREATE PROCEDURE SP_CadastraPessoaFisica
> (
> @.Email Varchar(60) = '',
> @.Senha Varchar(10) = '',
> @.Nome Varchar(50) = '',
> @.Sobrenome Varchar(50) = '',
> @.DataNascimento DateTime,
> @.Sexo Char(1),
> @.CPF Varchar(12)
> )
> As
> Set Nocount On
> Insert Into Usuario(Email,Senha) Values(@.Email,@.Senha)
> GO
> Simple.Isn't it ? So...I need to retrieve the ID from this insert command
> when inserted. Should I use @.@.Identity ? How do i do ? I don't know how to
> use @.@.Identity!
> --
> Thanks in advance,
> Daniel Groh
>|||Hi
You can use this:
SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]
This will help you solve the problem
thanks and regards
Chandra
"Daniel Groh" wrote:

> Hi,
> I have the following proc:
> CREATE PROCEDURE SP_CadastraPessoaFisica
> (
> @.Email Varchar(60) = '',
> @.Senha Varchar(10) = '',
> @.Nome Varchar(50) = '',
> @.Sobrenome Varchar(50) = '',
> @.DataNascimento DateTime,
> @.Sexo Char(1),
> @.CPF Varchar(12)
> )
> As
> Set Nocount On
> Insert Into Usuario(Email,Senha) Values(@.Email,@.Senha)
> GO
> Simple.Isn't it ? So...I need to retrieve the ID from this insert command
> when inserted. Should I use @.@.Identity ? How do i do ? I don't know how to
> use @.@.Identity!
> --
> Thanks in advance,
> Daniel Groh
>
>|||Hi, I tryed
CREATE PROCEDURE SP_CadastraPessoaFisica
(
@.Email Varchar(60) = '',
@.Senha Varchar(10) = '',
@.Nome Varchar(50) = ''
)
As
Declare @.CdUsuario Int
Set Nocount On
Insert Into Usuario(Email,Senha) Values(@.Email,@.Senha)
Select @.CdUsuario = @.@.Identity
Insert Into Teste(CdUsuario,Nome) Values(@.CdUsuario,'Daniel Groh')
GO
I have this error now:
Server: Msg 8152, Level 16, State 9, Procedure SP_CadastraPessoaFisica, Line
15
String or binary data would be truncated.
The statement has been terminated.
Atenciosamente,
Daniel Groh
CTF Technologies do Brasil Ltda.
Analista Programador
Fone: 11 3837-4203
E-mail: dgroh@.ctf.com.br
"Chandra" <Chandra@.discussions.microsoft.com> escreveu na mensagem
news:2A563E0C-350B-44CE-9C5D-4B1DD1C2F42F@.microsoft.com...
> Hi
> You can use this:
> SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]
> This will help you solve the problem
> thanks and regards
> Chandra
>
> "Daniel Groh" wrote:
>|||Hi
Just see the data capacity for
Teste(CdUsuario, Nome)
Is the column: Nome (Name) more than 11 chars '
thanks and regards
Chandra
"Daniel Groh" wrote:

> Hi, I tryed
> CREATE PROCEDURE SP_CadastraPessoaFisica
> (
> @.Email Varchar(60) = '',
> @.Senha Varchar(10) = '',
> @.Nome Varchar(50) = ''
> )
> As
> Declare @.CdUsuario Int
> Set Nocount On
> Insert Into Usuario(Email,Senha) Values(@.Email,@.Senha)
> Select @.CdUsuario = @.@.Identity
> Insert Into Teste(CdUsuario,Nome) Values(@.CdUsuario,'Daniel Groh')
> GO
>
> I have this error now:
> Server: Msg 8152, Level 16, State 9, Procedure SP_CadastraPessoaFisica, Li
ne
> 15
> String or binary data would be truncated.
> The statement has been terminated.
> --
> Atenciosamente,
> Daniel Groh
> CTF Technologies do Brasil Ltda.
> Analista Programador
> Fone: 11 3837-4203
> E-mail: dgroh@.ctf.com.br
> "Chandra" <Chandra@.discussions.microsoft.com> escreveu na mensagem
> news:2A563E0C-350B-44CE-9C5D-4B1DD1C2F42F@.microsoft.com...
>
>|||Yes, That was the problem
Thanks in advance!
Atenciosamente,
Daniel Groh
CTF Technologies do Brasil Ltda.
Analista Programador
Fone: 11 3837-4203
E-mail: dgroh@.ctf.com.br
"Chandra" <Chandra@.discussions.microsoft.com> escreveu na mensagem
news:5B01A3DD-F70E-49E8-A5C0-C077C0E45223@.microsoft.com...
> Hi
> Just see the data capacity for
> Teste(CdUsuario, Nome)
> Is the column: Nome (Name) more than 11 chars '
> thanks and regards
> Chandra
> "Daniel Groh" wrote:
>

No comments:

Post a Comment