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
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.
You can use 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
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
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
Related
I'm setting up a storekeeping program in which I have 2 tables, one for products and another for materials.
In the products table, each product has several materials. Is there any way to select these rows and decrement materials availability?
I tried to use a foreach loop but I couldn't implement it and store each rows data
CREATE TABLE materials
(
materialID INT PRIMARY KEY IDENTITY,
materialName NVARCHAR(100) NULL,
materialAmount INT NULL,
)
CREATE TABLE productStack
(
ID INT PRIMARY KEY IDENTITY,
productsID INT NULL,
materialID INT NULL,
amount INT NULL
)
GO;
CREATE PROCEDURE updateMaterials
(#ID INT,
#AMOUNT INT)
AS
BEGIN
UPDATE materials
SET materialAmount = (materialAmount - #AMOUNT)
WHERE materialID = #ID
END
You could use a temp table and while loop such as :
SELECT * INTO #TEMP_A FROM PRODUCTSTACK
DECLARE #ID INT,#AMOUNT INT
WHILE EXISTS(SELECT * FROM #TEMP_A)
BEGIN
SET #ID = (SELECT TOP 1 ID FROM PRODUCTSTACK)
SET #AMOUNT = (SELECT TOP 1 AMOUNT FROM PRODUCTSTACK WHERE ID = #ID)
EXEC UPDATEMATERIALS #ID,#AMOUNT
DELETE FROM #TEMP_A WHERE ID = #ID
END
As we have no sample to base this on, this is a guess. Like I said, however, seems like a table-type parameter would do this:
CREATE TYPE dbo.MaterialAmount AS table (ID int, Amount int);
GO
CREATE PROC dbo.UpdateMaterials #Materials dbo.MaterialAmount READONLY AS
BEGIN
UPDATE M
SET materialAmount = MA.Amount
FROM dbo.materials M
JOIN #Materials MA ON M.materialID = MA.ID;
END;
GO
--Example of usage:
DECLARE #Materials dbo.MaterialAmount;
INSERT INTO #Materials
VALUES(1,100),
(5,20);
EXEC dbo.UpdateMaterials #Materials;
Hi I have a store procedure which insert data into a table1 and there is another store procedure which insert data in table2 I need to insert table2.ID in table1 while updating I am calling second store procedure in first and it returns me the Id as an output parameter but it didn't get updated.
SP1 :
ALTER PROCEDURE [dbo].[updateSP]
#prm1 varchar(30),
#prm2 varchar(20),
#prm3 varchar(20),
#prm3 varchar(max)
AS
BEGIN
SET NOCOUNT OFF;
DECLARE #ID int
exec SPInsert #ID OUTPUT, #prm1, #prm2 , #prm3
update tbl1 set status = prm1, updated_by = prm2, updated_date = GETDATE(), reason_id = #ID where student_no = prm3
END
SP2 :
ALTER PROCEDURE [dbo].[SPInsert]
#prm1 varchar(30),
#prm2 varchar(20),
#prm3 varchar(20),
#prm3 varchar(max),
#ID output,
AS
BEGIN
Insert into tbl2 (student_no, reason_txt, created_by, created_date)Values(#prm1 ,#prm2 ,#prm3 , GETDATE())
Select #ID = Scope_Identity()
END
I have two tabs on my front end collecting information for Addresses and Users. Currently, my stored procedures are called on the final submit button. My Addresses table has an ID (primary key), Line1, and Line2 column. My Users table has an ID (primary key) FirstName, LastName and AddressID column (which should map to the ID column in the addresses table). My address insert proc looks like this:
ALTER PROC [dbo].[Addresses_Insert]
#Line1 nvarchar(50),
#Line2 nvarchar (50), = null
#ID = INT output
As
BEGIN
INSERT INTO [dbo].[Addresses]
(
[Line1],
[Line2]
)
VALUES
(#Line1, #Line2)
SET #Id = SCOPE_IDENTITY()
END
My Users update looks like this:
ALTER PROC [dbo].[Users_Update]
#Id int
,#FirstName nvarchar(50) = null
,#LastName nvarchar(50) = null
,#AddressId INT = null
AS
BEGIN
UPDATE dbo.Users
SET
[FirstName] = #FirstName
,[LastName] = #LastName
,[AddressId] = #AddressId --not sure about this
WHERE Id = #Id
I need to get the scope_identity() from the address stored procedure into the AddressID column for my Users table, but I'm not sure how to make that work.
When you add your parameters in your code, set the #Id parameter as an output parameter and check the parameter value after your procedure executes:
SqlParameter x = new SqlParameter();
x.Direction = System.Data.ParameterDirection.Output;
or you can just return the value from your #Id parameter like this:
SET #Id = SCOPE_IDENTITY();
Return #Id
I'm working on a cascading insertion where a stored procedure should return an id of the inserted row. This would not be a problem if the id of the table was an int. However, the id is a varchar and therefore I cannot use SCOPE_IDENTITY().
This is my procedure so far:
CREATE PROCEDURE NEW_ARTICLE
#id varchar(50) OUTPUT,
#name varchar(100),
#articleNr varchar(50),
#gategory varchar(50),
#containerId varchar(50),
#contPerContainer int,
#pictureId varchar(50)
AS
SET NOCOUNT OFF
INSERT INTO nextlabel.[Article] (name, article_nr, category, container_id, count_per_container, picture_id )
VALUES (#name, #articleNr, #gategory, #containerId, #contPerContainer, #pictureId)
SET #id = SCOPE_IDENTITY()
Where the last row is not correct since the column id is a varchar.
How can I return the id?
Try this:
CREATE PROCEDURE NEW_ARTICLE
#id varchar(50) OUTPUT,
#name varchar(100),
#articleNr varchar(50),
#gategory varchar(50),
#containerId varchar(50),
#contPerContainer int,
#pictureId varchar(50)
AS
SET NOCOUNT OFF
SET #id = newid()
INSERT INTO nextlabel.[Article] (id, name, article_nr, category, container_id, count_per_container, picture_id)
VALUES (#id, #name, #articleNr, #gategory, #containerId, #contPerContainer, #pictureId)
GO
I am trying to write a stored procedure that writes the details of a new user (NonMembers table) to a data base and then uses the auto generated id for that user in the next part of the code to register that user to a competition (registration table). My attempt at doing this is as follows but clearly my SQL skills are a long way from where they need to be?
All help would be greatly appreciated.
ALTER PROCEDURE [dbo].[RegisterNonMember]
#CompetitionId INTEGER,
#IsMember BIT,
#FirstName NVARCHAR(50),
#Surname NVARCHAR(50),
#Handicap INTEGER,
#Club NVARCHAR(50),
#CountyId INTEGER,
#CountryId INTEGER,
#PlayersStartTime TIME
AS
BEGIN
DECLARE #NonMember TABLE
(NonMemberId INTEGER,
FirstName NVARCHAR(50),
Surname NVARCHAR(50),
Handicap INTEGER,
Club NVARCHAR(50),
CountyId INTEGER,
CountryId INTEGER
)
INSERT INTO [dbo].[NonMembers]
(
FirstName
,Surname
,[Handicap]
,[Club]
,CountyId
,CountryId
)
VALUES
(#FirstName
,#Surname
,#Handicap
,#Club
,#CountyId
,#CountryId
)
--UPDATE #NonMember
--SET [#NonMember].NonMemberId = [dbo].[NonMembers].[NonMemberId]
--FROM [dbo].[NonMembers]
--JOIN #NonMember ON [#NonMember].NonMemberId = [dbo].[NonMembers].[NonMemberId]
--WHERE #NonMember.FirstName = [dbo].[NonMembers].[FirstName]
--AND #NonMember.Surname = [dbo].[NonMembers].[Surname]
--AND #NonMember.Handicap = [dbo].[NonMembers].[Handicap]
--AND #NonMember.Club = [dbo].[NonMembers].[Club]
DECLARE #Registration TABLE
(CompetitionId INTEGER,
IsMember BIT,
NonMemberId INTEGER,
PlayersStartTime TIME
)
INSERT INTO [dbo].[Registration]
(
[CompetitionId]
,[IsMember]
,[NonMemberId]
,[PlayersStartTime])
VALUES
(#CompetitionId
,#IsMember
,#NonMemberId
,#PlayersStartTime)
END
After your INSERT add:
DECLARE #UserID INT
SELECT #UserID = SCOPE_IDENTITY()