views:

395

answers:

3

I'd like to know if this kind of stored procedure is possible, do i need some sort of looping structure or something? I want to do this, basically in this order:

  1. get all rows from one table or view. (table1)
  2. based on columns from table 1, i want to set variables for use in insert/update table2.
  3. i want to reference another table, (table3), to find a key from table1, that will "Override", any cases that the row data might fall into.
  4. Insert or Update the table2.

If this is possible, could i PLEASE get some sort of draft in the answer? Thank you for reading! plz try to help! Here's another sort-of "diagram" of what I'm thinking:

  1. select * from table1
  2. case [table1].[table1column] - [table1].[table1column] <=0, parameter1= "a" (many cases)
  3. case [table1].[tableID] Exists in table3, parameter1 = [table3].[parameter]
  4. case [table1].[tableID] Exists in table2, update, else insert

Thanks for all the attempts so far!!If i figure this out I'm gonna post it.

+1  A: 

We need more information, but I'll give you 98% or better odds that this can all be done in two queries (one for the insert, one for the updates).

Here's a generic example for the insert:

INSERT INTO [Table2]
    SELECT 
        t1.[col1], /* use columns from table1 to update table2 */
        COALESCE(t3.[col2], t1.[col2]) /* table3 "overrides" table1 */
    FROM [Table1] t1 -- get all rows from table1
    LEFT JOIN [Table3] t3 ON t3.ID = t1.Table3ID
    LEFT JOIN [Table2] t2 ON t2.ID = t1.Table2ID 
    WHERE t2.OtherColumn IS NULL /* insert - only make changes where the record doesn't already exist */

and the update:

UPDATE t2
    SET t2.[col1] = t1.[col1],
        t2.[col2] = COALESCE(t3.[col2], t1.[col2])
FROM [table1] t1
LEFT JOIN [Table3] t3 ON t3.ID = t1.Table3ID
INNER JOIN [Table2] t2 ON t2.ID = t1.Table2ID /* update - only make changes where the record does already exist */
Joel Coehoorn
+1  A: 

It's hard to say without more info about your data model, but there are cases similar to the one you describe that could be handled without iteration.

For example, you could select on a left join of t1 and t3, use the t3 value if one is present, or use an expression based on the value of the t1 column. Here's a rough sample.

insert into t2 (column list)

Select case when t3.Column is not null then t3.Column 
when t1.Column = 'somevalue' then 'someothervalue' 
else...(other conditions/values) end
...
from t1 left join t3 on t1.Key = t3.Key

(increasing detail about your specific situation increases the quality of the help you get)

cmsjr
A: 

I'm going to jump the gun and assume that you're talking about MS SQL Server.

Yes, that kind of thing is possible. Here's a bit of psuedo code to get you started:

declare @someTable (
    idx int identity(1,1),
    column1 type,
    column2 type,
    etc type )

declare @counter

set @counter = 1

insert into @someTable (column1, column2, etc)
select column1, column2, etc from table1

while @counter < (select max(idx) from @someTable)
begin

   -- loop through records and perform logic
   insert result into table3

   set @counter = @counter + 1

end

If at all possible though...try to use a single query. Join your tables and use Case statements to perform the logic.

Justin Niessner
while that would probaly work, looping is a bad idea and should not be encouraged. A set-based solution will be better.
HLGEM
That is what I suggest...you just have to make it to the end of the post.
Justin Niessner
I'm going to select this. I haven't got it completely fleshed, but I'm going to start with this loop... Its gonna suck , until i figure out how to do it all in one query
Marlon