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 |
|
zaurska
Starting Member
12 Posts |
Posted - 2008-08-21 : 09:31:48
|
| Hi,I have two tables set out as follows:-----------------------------------------------------------------------------------------Table A col colPKey ID_a Name 1 abc12 abc2--------------------------------------------------------------------- Table B col col col col col colPKey ID Name x y ID_a otherID 1 xyz1 a s Null 1052 xyz2 a s Null 1053 xyz3 a s Null 1054 xyz4 a s Null 1055 xyz5 a t Null 1056 xyz6 a t Null 1057 xyz7 b s Null 1078 xyz8 b s Null 107 9 xyz9 b s Null 107 10 xyz10 b t Null 107 ---------------------------------------------------------------------------------------I want to insert a new row to Table A for each unique group in Table B and write the new back to Table B.The uniqueness of the entries in Table B is defined by the combination of col. x and col. Y.So, for example I’d want to insert a new ref: [3], [abc3] to Table A and then simultaneously/imediately populate Table B -> column: ID_a, with [3] on rows 1,2,3 & 4, for the unique combination of a + s And would want to apply this restricted by the value in otherID, with a @parameter. So in the example above, only affect rows 1 to 6 when using the parameter ‘105’, and create 2 new rows in Table A.I guess I need to use SCOPE IDENTITY(), INSERT and @parameters, but don’t know syntax well enough (at all) I’d like to use a stored procedure to do this.Any help gratefully received |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-21 : 09:42:44
|
| Didnt get what you're asking for? How do you relate which rows to be populated on table B based on values put in A?I see a common ID_a field but its having all NULLs in tableB. Can you explain the relation please? |
 |
|
|
zaurska
Starting Member
12 Posts |
Posted - 2008-08-21 : 11:25:24
|
| Yes, Sorry, I wasn't clear. The nulls are the starting point; I want to finish with the nulls populated by the ID_a, which should be created by the insert to TableA. I am finding difficult the concept of INSERTing the number of records equal to the number of distinct rows in the TableB query and passing back that new reference to TableB.The idea is that I want to associate the items in table B by their common attributes and ID them by the TableA PK.So, I want to end up with the following, after running a stored Proc Insert, Scope Identity() thing (maybe?) for @param = 105Table A1 abc12 abc23 abc3 (new)4 abc4 (new)1 xyz1 a s 3 1052 xyz2 a s 3 1053 xyz3 a s 3 1054 xyz4 a s 3 1055 xyz5 a t 4 1056 xyz6 a t 4 1057 xyz7 b s Null 1078 xyz8 b s Null 107 9 xyz9 b s Null 107 10 xyz10 b t Null 107am I doing something fundamentally wrong? I'm pretty new to SQL, sorry. I know I'm not clear but I don't really know the words to use.I guesss I'm saying that TableB is my starting point to determine what I want to INSERT into TableA then use those new Rows to Update TableB now that I have a unique referenceThe data in TableB has rows that are not uniquely related except in combination and I want to associate them to eachother and a PK by adding another field: being the PK inTable A. So I want to say: for every row in Table B where X and Y are the same and the final field = @paramater, INSERT a new row to TableA, then use this row's ID and write that back to the relevant rows in TableB....and I thought I could use ScopeIdentity() for that.I could achieve it in C# with loops but that feels not elegant.thanksD |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-21 : 12:53:59
|
| Still unclear what's relation between abc3 and xyz1,xyz2,..? what prompted you to put them ID_a value of 3 which is abc3 row's pk value? |
 |
|
|
zaurska
Starting Member
12 Posts |
Posted - 2008-08-21 : 12:58:45
|
| abc3 is just a name to attach to the single primary key. xyz1, xyz have no priamry relationhip except by evaluating the groupBy / distinct for them until I specifically make the association. TableA only exists to define this uniqueness. Here's something that nearly works:Declare @paramA intDeclare @scopeIdValue intSet @paramA = 105 /*for test, would get from c# application*/SELECT DISTINCT x, y, otherID=@paramAFROM TableBINSERT INTO TableA VALUES('') SET @scopeIdValue = SCOPE_IDENTITY()UPDATE TableB SET ID_A =@scopeIdValue only I want to INSERT for each row of the SELECT DISTINCT rather than get the same scopeIdentity for the whole SELECT |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-21 : 13:09:24
|
do like this:-DECLARE @INSERTED_VALUES table(ID_a int,Name varchar(100))INSERT INTO TableA (Name)OUTPUT INSERTED.ID_a,INSERTED.Name INTO @INSERTED_VALUESSELECT DISTINCT x + yFROM TableBWHERE otherID=@ParameterValueUPDATE bSET b.ID_a=i.ID_aFROM TableB bINNER JOIN @INSERTED_VALUES iON i.Name=b.x+b.y |
 |
|
|
zaurska
Starting Member
12 Posts |
Posted - 2008-08-21 : 13:57:43
|
Perfect.Thank you very much! |
 |
|
|
|
|
|
|
|