Actualice la tabla usando el procedimiento almacenado

I have a table and I have 50 columns in it. From my code behind at first I am inserting 10 values using a stored procedure, after that in second page based on userid I want to update other 40 columns. So I am updating the table and the userid column of the table is an identity column which is auto incremented so how to get the user id for update stored procedure?

CREATE PROCEDURE sp_update
     (@FormFiledBy varchar(50), @MaritalStatus varchar(50),
      @Height varchar(50), @Religion varchar(50), @Caste varchar(100),
      @MotherTongue varchar(50), @Education varchar(100),
      @Occupation varchar(50), @CountryofResidence varchar(50), 
      @EducationDetails varchar(100), @AnnualIncome varchar(50), 
      @CountryOfBirth varchar(50), @BirthPlace varchar(50), 
      @TimeOfBirth nchar(10), @StarSign varchar(100),
      @Gothram varchar(50), @Rassi varchar(50), @HavinChildren varchar(10),
      @PhysicalStatus varchar (100)
     )
AS
BEGIN      
    UPDATE Profile_Master
    SET FormFiledBy = @FormFiledBy,
        MaritalStatus = @MaritalStatus,
        Height = @Height,
        physicalStatus = @physicalStatus,
        Religion = @Religion,
        Caste = @Caste,
        MotherTongue = @MotherTongue,
        Education = @Education,
        Occupation = @Occupation,
        CountryofResidence = @CountryofResidence,
        EducationDetails = @EducationDetails,
        AnnualIncome = @AnnualIncome,
        CountryOfBirth = @CountryOfBirth,
        BirthPlace = @BirthPlace,
        TimeOfBirth = @TimeOfBirth,
        StarSign = @StarSign,
        Gothram = @Gothram,
        Rassi = @Rassi,
        HavinChildren = @HavinChildren,
        PhysicalStatus = @PhysicalStatus
    WHERE 
        ????
END

preguntado el 31 de julio de 12 a las 14:07

Is there some other unique key you could go by, besides the id column? For example, columns X-Y-and-Z constitute a unique key so you could just go by that instead of the userid column? -

Yes the emailid column has a unique key.... -

3 Respuestas

In your initial procedure, when you insert the data, you should retrieve and return the userid value in an OUTPUT parameter so that you can then supply it to your update procedure.

Puedes usar SCOPE_IDENTITY() for this fairly easily.


As requested, some sample code (simplified, but you should see the pattern):

CREATE PROCEDURE add_row @data1 varchar(20), @data2 varchar(20), @id int OUTPUT
AS
BEGIN
    INSERT INTO Table (Data1, Data2) VALUES (@data1, @data2)

    SELECT @id = SCOPE_IDENTITY()
END
GO

CREATE PROCEDURE update_row @id int, @data3 varchar(20), @data4 varchar(20)
AS
BEGIN
    UPDATE Table SET Data3 = @data3, Data4 = @data4
    WHERE Id = @id
END
GO

Respondido 31 Jul 12, 15:07

can you please show me some code for this?? and how to supply the parameter for the update procedure? - chandra sekhar

Even in my insert procedure i am not inserting userid as it is identity column so it is auto incremented so how to set scope_identity for user id column?? - chandra sekhar

@Chandrasekhar -- Added an example of usage for you. - mwigdahl

@mwigdahl-- but when i insert the record at first page i am getting an exception like-- Procedure or function 'Insertreg' expects parameter '@id', which was not supplied. - chandra sekhar

@Chandrasekhar -- then you are going to need to modify the invocation of the insert procedure so that it provides the additional parameter. Just because it's an OUTPUT parameter doesn't mean you don't need to provide it in the call. The signature of the procedure always needs to match the invocation. - mwigdahl

you don't need update your primary key, but you must pass UserId as parameter of your stored procedure.

Update Profile_Master
Set ....
Where UserId = @UserId --your parameter

Nota : yuo must ensure that your primary key is just UserId

Respondido 31 Jul 12, 14:07

You just have to pass SCOPE_IDENTITY() from fist Stored Procedure as below:

@Id int OUTPUT,

---
SET @Id = SCOPE_IDENTITY()

And in the second Stored Procedure Apply the Id as an argument an Update the record based on that.

@Id VARCHAR(15)
Update Profile_Master
set ....
........
where UserId = @Id

Respondido 31 Jul 12, 14:07

Even in my insert procedure i am not inserting userid as it is identity column so it is auto incremented so how to set scope_identity for user id column?? - chandra sekhar

No tienes que configurar scope_identity explicitly..it will be automatically assigned by the SCOPE_IDENTITY() method...@Chandrasekhar - Vishal Suthar

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.