| Author |
Topic |
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-03-03 : 09:43:26
|
| My tables and row as follow,tPickPointHidx | tcoutcd----------------1 iph /*idx is smallint identity(1,1) primary key clusteredtcoutcd is varchar(20) and uniquetPickPointH is a parent*/tPickPointDidx | tpphidx | desn---------------------------------------1 1 sungai rokam2 1 gunung rapat/*idx is smallint identity(1,1) primary key clusteredtpphidx is a foreign key refer to tPickPointH(idx)combination of tpphidx and desn is uniquetPickPointD is a child*/Let's say i want to update the record. 1. idx=1 2. declare @data xmlset @data='<data><pickpoints><pickpoint>gopeng</pickpoint></pickpoints><pickpoints><pickpoint>sungai rokam</pickpoint></pickpoints></data>'How my T-SQL look's like, then the result as follow,tPickPointHidx | tcoutcd----------------1 iphtPickPointDidx | tpphidx | desn---------------------------------------1 1 sungai rokam3 1 gopeng |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-03 : 09:47:59
|
| which field you want to update? based on what relation?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-03-03 : 10:01:29
|
quote: Originally posted by visakh16 which field you want to update? based on what relation?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
I want to update1. tPickPointD(desn) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-03 : 10:06:13
|
| based on what value & relationship?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-03-03 : 10:26:57
|
quote: Originally posted by visakh16 based on what value & relationship?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
So far i've as follow,declare @idx intset @idx=1declare @cout varchar(20)set @cout='iph'declare @data xmlset @data='<data><pickpoints><pickpoint>gopeng</pickpoint></pickpoints><pickpoints><pickpoint>sungai rokam</pickpoint></pickpoints></data>'BEGIN TRYbegin transactionupdate tPickPointH set tcoutcd=@coutwhere idx=@idx;delete from tPickPointD where tpphidx=@idx;if @@rowcount>0begininsert into tPickPointD(tpphidx,desn)select @idx,a.b.value('pickpoint[1]','varchar(50)')from @data.nodes('/data/pickpoints') a(b);endcommitEND TRYBEGIN CATCHIF @@TRANCOUNT > 0ROLLBACKDECLARE @ErrMsg varchar(4000), @ErrSeverity intSELECT @ErrMsg = ERROR_MESSAGE(),@ErrSeverity = ERROR_SEVERITY()RAISERROR(@ErrMsg, @ErrSeverity, 1)END CATCHAs you can see, to update tPickPointD with new row, i need to delete all row first, then perform insert.I'm thinking, this is not the best practice |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-03 : 10:43:31
|
| you need to insert same id value for all?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-03-03 : 10:46:30
|
quote: Originally posted by visakh16 you need to insert same id value for all?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
no it's not. idx is a identity field |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-03 : 10:52:25
|
| nope i mean tcpphidx------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-03-03 : 10:55:53
|
| tcpphidx is typoactually, tpphidx is a foreign key refer to tPickPointH(idx)now our transaction id (idx)=1declare @idx intset @idx=1 |
 |
|
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-03-03 : 11:00:53
|
quote: Originally posted by Delinda
quote: Originally posted by visakh16 based on what value & relationship?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
So far i've as follow,declare @idx intset @idx=1declare @cout varchar(20)set @cout='iph'declare @data xmlset @data='<data><pickpoints><pickpoint>gopeng</pickpoint></pickpoints><pickpoints><pickpoint>sungai rokam</pickpoint></pickpoints></data>'BEGIN TRYbegin transactionupdate tPickPointH set tcoutcd=@coutwhere idx=@idx;delete from tPickPointD where tpphidx=@idx;if @@rowcount>0begininsert into tPickPointD(tpphidx,desn)select @idx,a.b.value('pickpoint[1]','varchar(50)')from @data.nodes('/data/pickpoints') a(b);endcommitEND TRYBEGIN CATCHIF @@TRANCOUNT > 0ROLLBACKDECLARE @ErrMsg varchar(4000), @ErrSeverity intSELECT @ErrMsg = ERROR_MESSAGE(),@ErrSeverity = ERROR_SEVERITY()RAISERROR(@ErrMsg, @ErrSeverity, 1)END CATCHAs you can see, to update tPickPointD with new row, i need to delete all row first, then perform insert.I'm thinking, this is not the best practice
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-03 : 11:06:29
|
| actually why do delete? is it that your table should always contain only data based on latest xml passed?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-03 : 11:21:44
|
| then how can you avoid delete? each time your @xml value changes definitely you might need to clear out earlier records. b/w is the second table used for some temporary purposes?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-03-03 : 11:24:58
|
Current record as follow,tPickPointDidx | tpphidx | desn---------------------------------------1 1 sungai rokam2 1 gunung rapatafter execute,declare @idx intset @idx=1declare @cout varchar(20)set @cout='iph'declare @data xmlset @data='<data><pickpoints><pickpoint>gopeng</pickpoint></pickpoints><pickpoints><pickpoint>sungai rokam</pickpoint></pickpoints></data>'BEGIN TRYbegin transactionupdate tPickPointH set tcoutcd=@coutwhere idx=@idx;delete from tPickPointD where tpphidx=@idx;if @@rowcount>0begininsert into tPickPointD(tpphidx,desn)select @idx,a.b.value('pickpoint[1]','varchar(50)')from @data.nodes('/data/pickpoints') a(b);endcommitEND TRYBEGIN CATCHIF @@TRANCOUNT > 0ROLLBACKDECLARE @ErrMsg varchar(4000), @ErrSeverity intSELECT @ErrMsg = ERROR_MESSAGE(),@ErrSeverity = ERROR_SEVERITY()RAISERROR(@ErrMsg, @ErrSeverity, 1)END CATCHthe latest result as follow,tPickPointDidx | tpphidx | desn---------------------------------------1 1 sungai rokam2 1 gopengcombination of tpphidx and desn is uniquei dont care the value of idxCan you show me, how to adjust my code instead of delete all row?second table is not for temporary purposes |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-03 : 11:31:41
|
no need of delete. just use update asupdate tset t.desn=t1.desnfrom tPickPointD tjoin (select @idx AS tpphidx,a.b.value('pickpoint[1]','varchar(50)') AS desn from @data.nodes('/data/pickpoints') a(b))t1on t.tpphidx=t1.tpphidx------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-03-03 : 11:56:22
|
quote: Originally posted by visakh16 no need of delete. just use update asupdate tset t.desn=t1.desnfrom tPickPointD tjoin (select @idx AS tpphidx,a.b.value('pickpoint[1]','varchar(50)') AS desn from @data.nodes('/data/pickpoints') a(b))t1on t.tpphidx=t1.tpphidx------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
I'm detail it as follow,CREATE TABLE [dbo].[tPickPointH]( [idx] [smallint] IDENTITY(1,1) NOT NULL, [tcoutcd] [varchar](20) NOT NULL, CONSTRAINT [PK_tPickPointH] PRIMARY KEY CLUSTERED ( [idx] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY], CONSTRAINT [tPickPointH01] UNIQUE NONCLUSTERED ( [tcoutcd] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY] My rows as follow,tPickPointHidx | tcoutcd-----------------------1 iphCREATE TABLE [dbo].[tPickPointD]( [idx] [smallint] IDENTITY(1,1) NOT NULL, [tpphidx] [smallint] NOT NULL, [desn] [varchar](50) NOT NULL, CONSTRAINT [PK_tPickPointD] PRIMARY KEY CLUSTERED ( [idx] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY], CONSTRAINT [tPickPointD01] UNIQUE NONCLUSTERED ( [tpphidx] ASC, [desn] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GOSET ANSI_PADDING OFFGOALTER TABLE [dbo].[tPickPointD] WITH CHECK ADD CONSTRAINT [FK_tPickPointD_tpphidx] FOREIGN KEY([tpphidx])REFERENCES [dbo].[tPickPointH] ([idx])ON UPDATE CASCADEON DELETE CASCADEGOALTER TABLE [dbo].[tPickPointD] CHECK CONSTRAINT [FK_tPickPointD_tpphidx] My rows as follow,tPickPointDidx | tpphidx | desn---------------------------------- 1 1 sungai rokam2 1 gunung rapatNow i customised my code with yours as follow,declare @idx intset @idx=1declare @cout varchar(20)set @cout='iph'declare @data xmlset @data='<data><pickpoints><pickpoint>gopeng</pickpoint></pickpoints><pickpoints><pickpoint>sungai rokam</pickpoint></pickpoints></data>'BEGIN TRYbegin transactionupdate tPickPointH set tcoutcd=@coutwhere idx=@idx;update tset t.desn=t1.desnfrom tPickPointD tjoin (select @idx AS tpphidx,a.b.value('pickpoint[1]','varchar(50)') AS desn from @data.nodes('/data/pickpoints') a(b))t1on t.tpphidx=t1.tpphidx;commitEND TRYBEGIN CATCHIF @@TRANCOUNT > 0ROLLBACKDECLARE @ErrMsg varchar(4000), @ErrSeverity intSELECT @ErrMsg = ERROR_MESSAGE(),@ErrSeverity = ERROR_SEVERITY()RAISERROR(@ErrMsg, @ErrSeverity, 1)END CATCHThe result was error,Msg 50000, Level 14, State 1, Line 29Violation of UNIQUE KEY constraint 'tPickPointD01'. Cannot insert duplicate key in object 'dbo.tPickPointD'. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-03 : 12:13:26
|
| thats because you're trying to insert the same value of @idx along with all values of pickpoint from xml. so update will match all records in the query. i think you need to have a unique id value in xml as well to compare with id of table for correct updation------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-03-03 : 12:18:25
|
quote: Originally posted by visakh16 thats because you're trying to insert the same value of @idx along with all values of pickpoint from xml. so update will match all records in the query. i think you need to have a unique id value in xml as well to compare with id of table for correct updation------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
hello sir,i can't imagine it. can you show me the code? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-03 : 12:43:20
|
| you need to have a node inside your xml with a unique id value or else it will update all records each time ( as they all have same tpphidx value which is what you passed @idx and so the condition on t.tpphidx=t1.tpphidx will match multiple records------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-03-03 : 12:52:16
|
| i think, it easy to clear the value first, then perform insert. :)Is that ok mr visakh? |
 |
|
|
|