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 |
|
malachi151
Posting Yak Master
152 Posts |
Posted - 2010-03-05 : 10:14:45
|
| I have a column that I need to auto increment each time a record is inserted, but unlike a straight auto inc column this needs to increment within a subset of values, i.e. given Columns W-X-Y-Z, where W is the PK, I want to increment column Z as follows:W-X-Y-Z------1-1-A-12-1-A-23-1-B-14-1-B-25-2-A-16-2-A-27-2-A-38-2-B-19-2-B-2I tried the code below, but it doesn't work because the subquery in the WHERE clause returns multiple records.ALTER TRIGGER [dbo].[set_col_order] ON [dbo].[DataDicts_Typed] FOR INSERTAS BEGIN SET NOCOUNT ON; UPDATE DataDicts_Typed SET typed_col_order = (SELECT MAX(ISNULL(ddt.typed_col_order, 0))+1 FROM DataDicts_Typed ddt INNER JOIN inserted i ON i.typed_dict_id = ddt.typed_dict_id WHERE ddt.source_spec_id = i.source_spec_id AND ddt.typed_container = i.typed_container) WHERE typed_dict_id = (SELECT typed_dict_id FROM inserted)END |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-05 : 10:21:38
|
| [code]ALTER TRIGGER [dbo].[set_col_order] ON [dbo].[DataDicts_Typed]FOR INSERTAS BEGINSET NOCOUNT ON;UPDATE tSET t.Z=COALESCE(t1.Cnt,0) + 1FROM Table tINNER JOIN INSERTED iON i.X = t.XAND i.Y=t.YOUTER APPLY (SELECT COUNT(*) AS Cnt FROM Table WHERE X=t.X AND Y=t.Y AND Z IS NOT NULL) t1WHERE t.Z IS NULLEND[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
malachi151
Posting Yak Master
152 Posts |
Posted - 2010-03-05 : 10:53:38
|
quote: Originally posted by visakh16
ALTER TRIGGER [dbo].[set_col_order] ON [dbo].[DataDicts_Typed]FOR INSERTAS BEGINSET NOCOUNT ON;UPDATE tSET t.Z=COALESCE(t1.Cnt,0) + 1FROM Table tINNER JOIN INSERTED iON i.X = t.XAND i.Y=t.YOUTER APPLY (SELECT COUNT(*) AS Cnt FROM Table WHERE X=t.X AND Y=t.Y AND Z IS NOT NULL) t1WHERE t.Z IS NULLEND
Close, but not quite.When done with "AND Z IS NOT NULL" I get a value of 1 for every row.When done without "AND Z IS NOT NULL" I get the max count for x&y +1 in every row, i.e. the results look like:W-X-Y-Z------1-1-A-22-1-A-23-1-B-24-1-B-25-2-A-36-2-A-37-2-A-38-2-B-29-2-B-2Looks like the right dirction though, I'm still working on it, thanks. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-05 : 11:22:20
|
| do you have any unique valued column in your table?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
malachi151
Posting Yak Master
152 Posts |
Posted - 2010-03-05 : 11:30:52
|
quote: Originally posted by visakh16 do you have any unique valued column in your table?
Yes, I was trying something with ranking, but its not working either. This was my latest attempt: UPDATE ddt SET ddt.typed_col_order = rn.col_order FROM DataDicts_Typed ddt INNER JOIN (SELECT ROW_NUMBER() OVER (ORDER BY i.typed_dict_id) +1 as col_order, i.typed_dict_id FROM inserted i WHERE ddt.source_spec_id = i.source_spec_id AND ddt.typed_container = i.typed_container) rn ON rn.typed_dict_id = ddt.typed_dict_id WHERE ddt.typed_col_order IS NULL typed_dict_id is the auto inc key. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-05 : 11:42:04
|
| [code]ALTER TRIGGER [dbo].[set_col_order] ON [dbo].[DataDicts_Typed]FOR INSERTAS BEGINSET NOCOUNT ON;UPDATE tSET t.Z=COALESCE(t1.Cnt,0) + 1FROM Table tINNER JOIN INSERTED iON i.X = t.XAND i.Y=t.YOUTER APPLY (SELECT COUNT(*) AS Cnt FROM Table WHERE X=t.X AND Y=t.Y AND Z IS NOT NULL AND typed_dict_id < t.typed_dict_id) t1WHERE t.Z IS NULLEND[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|