On 01-07-2010 17:52, Arne Vajh j wrote:
On 01-07-2010 11:39, Paul wrote:
I have the next stored procedure in Sql Server 2008:
CREATE PROCEDURE [dbo].[GetNumTiquet]
@IdEmpresa int,
@NumTiquet int OUTPUT
AS
BEGIN
SET NOCOUNT ON;
UPDATE ConfGen
SET @NumTiquet=NumTiquet = (SELECT MAX(NumTiquet) +1 FROM ConfGen)
WHERE Id = @IdEmpresa
END
If i execute this stored procedure with Sql Server 2008 all is ok.
But how can i call this stored procedure with c#?
Something like (untested):
SqlCommand cmd = new SqlCommand("dbo.GetNumTiquet", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter prm1 = new SqlParameter("@IdEmpresa", SqlDbType.Int);
cmd.Parameters.Add(prm1);
SqlParameter prm2 = new SqlParameter("@NumTiquet", SqlDbType.Int);
prm2.Direction = ParameterDirection.Output;
cmd.Parameters.Add(prm2);
cmd.Parameters["@IdEmpresa"].Value = v;
cmd.ExecuteNonQuuery();
int res = (int)cmd.Parameters["@NumTiquet"].Value);
Note that the use of:
SELECT MAX()+1
usually is the wrong approach and instead SCOPE_IDENTITY() should
be used in the following SQL statements.
I don't understant.
What do you menan with SCOPE_INDENTITY() with this example?
Instead of:
SELECT MAX(id)+1 AS newid FROM table
INSERT INTO table VALUES(newid,val1,val2)
INSERT INTO othertable VALUES(valz,newid)
then if id is a IDENTITY column you can use:
INSERT INTO table(fld1,fld2) VALUES(val1,val2)
INSERT INTO othertable VALUES(valz,SCOPE_IDENTITY())
The problem with SELECT MAX()+1 is that it returns
the correct value at the time of SELECT, but unless
you use transactions and a very high transaction isolation
level, then it may no longer be a correct value when
you use it, because another thread/user/app may have
inserted a new record in between.
Arne- Ocultar texto de la cita -
- Mostrar texto de la cita -