Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
motilok
Starting Member
24 Posts |
Posted - 2010-08-16 : 12:26:50
|
Hi, I'm working on creating a small database for keeping track of some of our guests and customers info. I have a procedure that inserts info into 2 tables, and now I need to write a procedure that will have an option to update most or some data in the columns, where Guest_ID and Trip_ID cannot be updated, and Customer_ID can be added if it is null at the time being. I'm new to stored procedures and have no idea where to start. This Procedure will be linked to Microsoft Visual Studio 8, I don't know if that matters in the creation proccess. Create PROCEDURE GuestInfoUpdate-- Add the parameters for the stored procedure here(--Guest_ID, Int, PK - auto# not for update,--Trip_ID, int, PK, auto# - not for update,@Customer_ID int, --update if null@FirstName char(50),@LastName char(50),@Phone nchar(10) ,@UserName char(20) , --not for update@TripDate_Time datetime ,@Info1 char(50),@Info2 nchar(10) ,@Info3 char(50),@Info4 char(50),)ASBEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; Insert Into Guests --Insert statements for procedure here SELECT --Guest_ID, Int, PK - auto# not for update @Customer_ID, --update if null @FirstName, @LastName, @Phone Insert Into TripInfo -- Insert statements for procedure here SELECT --Trip_ID, int, PK, auto# - not for update@Customer_ID, --update if null@UserName, -- not for update@TripDate_Time,@Info1,@Info2,@Info3,@Info4,max(Guest_ID) from Guests --not for update end |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-16 : 12:38:46
|
sorry didnt get what you're looking for. can you elaborate?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
motilok
Starting Member
24 Posts |
Posted - 2010-08-16 : 13:32:13
|
I need a procedure that will pull data for a specific Trip_ID, and will give me an option to update most or some of the fields. Lets say I have following info:table GuestsGuest_ID =2Customer_ID = nullFirstName = JillLastName = SmithPhone = 4412325698Table TripInfoTrip_ID = 10Guest_ID =2Customer_ID = nullUserName = motilok (--username of person who created a record)TripDate_Time = 08/31/2010 07:15pmInfo1 = nullInfo2 = nullInfo3 = nullInfo4 = nullNow I need to change and add some of the info. Lets say that phone number was typed in wrong and needs to be updated to 4412325689, Customer_ID should be 34254 and Info1 (Info1=Refferal_1) = Scott SmithIn this case I only want to update 3 columns and leave others as they are. In other times, I might need to change other columns too. So how do I set it up so that I have an option to update all columns (except Trip_ID and Guest_ID), but so that query does not update the columns which I am going to leave blank a the time of update (keeps them w/ original data)? Does this makes sence? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-16 : 13:41:34
|
yup. make all the parameters to update as optional like@info1 varchar(10)= NULL,@Info2 int=NULL...then in update do likeUPDATE tab;eSET Info1=COALESCE(@Info1,Info1),Info2=COALESCE(@Info2,Info2)...WHERE @Info1 IS NOT NULLOR @Info2 IS NOT NULLOR... then pass values only for those fields you need to change.the COALESCE will make sure only fields passed are updated and others retain their original value------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
X002548
Not Just a Number
15586 Posts |
|
motilok
Starting Member
24 Posts |
Posted - 2010-08-16 : 14:34:27
|
quote: Originally posted by visakh16 yup. make all the parameters to update as optional like@info1 varchar(10)= NULL,@Info2 int=NULL...then in update do likeUPDATE tab;eSET Info1=COALESCE(@Info1,Info1),Info2=COALESCE(@Info2,Info2)...WHERE @Info1 IS NOT NULLOR @Info2 IS NOT NULLOR... then pass values only for those fields you need to change.the COALESCE will make sure only fields passed are updated and others retain their original value------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
YOU ARE A GENIOUS!!! You made my life so much easier, and my boss will be happy with the results. Thank you. |
|
|
X002548
Not Just a Number
15586 Posts |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-17 : 11:27:47
|
quote: Originally posted by motilok
quote: Originally posted by visakh16 yup. make all the parameters to update as optional like@info1 varchar(10)= NULL,@Info2 int=NULL...then in update do likeUPDATE tab;eSET Info1=COALESCE(@Info1,Info1),Info2=COALESCE(@Info2,Info2)...WHERE @Info1 IS NOT NULLOR @Info2 IS NOT NULLOR... then pass values only for those fields you need to change.the COALESCE will make sure only fields passed are updated and others retain their original value------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
YOU ARE A GENIOUS!!! You made my life so much easier, and my boss will be happy with the results. Thank you.
Welcome ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
motilok
Starting Member
24 Posts |
Posted - 2010-08-18 : 08:48:42
|
set ANSI_NULLS ONset QUOTED_IDENTIFIER ONgo-- =============================================-- Author: <Author,,Name>-- Create date: <Create Date,,>-- Description: <Description,,>-- =============================================ALTER PROCEDURE GuestsTrips_UpdateInfo -- Add the parameters for the stored procedure here (@Guest_ID int =null,@Trip_ID int =null,@Customer_ID int=null,@FirstName char(20) =null,@LastName char(20) =null,@Phone char(21) =null,@TripDate_Time datetime =null,@Info1 char(50) =null,@Info2 nchar(10) =null,@Info3 nchar(10) =null,@Info4 nchar(10) =null,@Info5 nchar(10) =null,@Info6 datetime =null,@DInfo7 char(50) =null,@Info8 char(50) =null)ASBEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure hereUPDATE Guests set Customer_ID =coalesce(@Customer_ID,Customer_ID), FirstName=coalesce(@FirstName,FirstName), LastName=coalesce(@LastName,LastName), Phone=coalesce(@Phone, Phone)where guest_id = @guest_IDUpdate Customersset TripDate_Time=coalesce(@TripDate_Time, TripDate_Time),Info1=coalesce(@Info1,Info1),Info2=coalesce(@Info2, Info2),Info3=coalesce(@Info3,Info3),Info4=coalesce(@Info4, Info4),Info5=coalesce(@Info5,Info5),Info6=coalesce(@Info6,Info6),Info7=coalesce(@Info7,Info7),Info8=coalesce(@Info8, Info8)where (Trip_id = @Trip_ID and guest_ID=@guest_ID)endAbove is my stored procedures as is. Works ok until I make a typo and put Trip_ID that does not go with Guest_ID. At that point I get Guest table updated according to Guest_ID and new info, but my TripInfo table stays untouched. What can I change above to make shoot an error if Trip_ID and Guest_ID does not go together, before any changes are made to any of the tables? |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2010-08-18 : 10:03:15
|
[code]BEGIN TRANDECLARE @err_msg varchar(8000), @Error int, @RowCount intIF NOT EXISTS (SELECT * FROM Guests WHERE guest_id = @guest_ID) BEGIN SET @err_msg = 'GuestID: ' + CONVERT(varchar(20),@guest_ID) + ' Does not Exist in the Guests table' GOTO Sproc_Error END IF NOT EXISTS (SELECT * FROM Customers WHERE guest_id = @guest_ID AND Trip_id = @Trip_ID) BEGIN SET @err_msg = 'GuestID: ' + CONVERT(varchar(20),@guest_ID) + ' And Trip_ID: ' + CONVERT(varchar(20),@Trip_ID) + ' Do not Exist in the Customers table' GOTO Sproc_Error END UPDATE Guests set Customer_ID =coalesce(@Customer_ID,Customer_ID), FirstName=coalesce(@FirstName,FirstName), LastName=coalesce(@LastName,LastName), Phone=coalesce(@Phone, Phone) where guest_id = @guest_ID SELECT @Error = @@ERROR, @RowCount = @@ROWCOUNTIF @Error <> 0 BEGIN SET @err_msg = 'System Error on Guest Update' GOTO Sproc_Error END IF @RowCount = 0 BEGIN SET @err_msg = 'Guest Update Did not Modify any rows' GOTO Sproc_Error END Update Customers set TripDate_Time=coalesce(@TripDate_Time, TripDate_Time), Info1=coalesce(@Info1,Info1), Info2=coalesce(@Info2, Info2), Info3=coalesce(@Info3,Info3), Info4=coalesce(@Info4, Info4), Info5=coalesce(@Info5,Info5), Info6=coalesce(@Info6,Info6), Info7=coalesce(@Info7,Info7), Info8=coalesce(@Info8, Info8) where (C= @Trip_ID and guest_ID=@guest_ID) SELECT @Error = @@ERROR, @RowCount = @@ROWCOUNTIF @Error <> 0 BEGIN SET @err_msg = 'System Error on Customers Update' GOTO Sproc_Error END IF @RowCount = 0 BEGIN SET @err_msg = 'Customers Update Did not Modify any rows' GOTO Sproc_Error END COMMIT TRANSproc_Exit: ReturnSproc_Error: PRINT @err_msg ROLLBACK TRAN GOTO Sproc_Exit[/code]Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
|
|
motilok
Starting Member
24 Posts |
Posted - 2010-08-18 : 11:15:00
|
One step closer :) It does not update anything if Trip_ID does not correspond to Guest_ID. However, I do not get error message when it does not update info, it just runs without updating fields. I link my stored procedure to Microsof Visual Studio, Do I need to do anything there to make those errors to pop up? I have two screens per one transaction. Screen 1: to enter info and preview it. It also links to Screen 2, which is the actual procedure and inserts new data to my tables. Not sure if it really matters in this case. |
|
|
motilok
Starting Member
24 Posts |
Posted - 2010-08-19 : 15:46:44
|
Got it! Thanks! |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-08-20 : 01:40:50
|
What are you colour-coding your examples with? I like the look of that |
|
|
motilok
Starting Member
24 Posts |
Posted - 2010-08-20 : 10:33:52
|
when you click reply to Topic, there are options to code your text in a simple way that regular people use: color, bolt, italic.. etc... or you can just put [blue ] YOUR TEXT HERE [/blue ] (no space in brackets, to add color to text)see FORUM CODE on your left of the text box for more info.. :) |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-08-21 : 04:07:02
|
Hehehehe .. .OK I know about them. You put them all in MANUALLY? You're a star! ... I was assuming you had some slinky tool that pretty-printed SQL code with forum-compatible-colour tags. |
|
|
|
|
|
|
|