简体   繁体   English

选择,条件,设置变量,然后在表中插入/更新的存储过程

[英]Stored procedure that Selects, Conditions, sets variables, then inserts/updates into a table

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) (表格1)
  2. based on columns from table 1, i want to set variables for use in insert/update table2. 基于表1中的列,我想设置要在插入/更新表2中使用的变量。
  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. 我想引用另一个表(table3)从table1中查找一个键,该键将“覆盖”行数据可能会遇到的任何情况。
  4. Insert or Update the table2. 插入或更新表2。

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 从表1中选择*
  2. case [table1].[table1column] - [table1].[table1column] <=0, parameter1= "a" (many cases) 情况[table1]。[table1column]-[table1]。[table1column] <= 0,parameter1 =“ a”(许多情况)
  3. case [table1].[tableID] Exists in table3, parameter1 = [table3].[parameter] 情况[table1]。[tableID]存在于表3中,参数1 = [表3]。[参数]
  4. case [table1].[tableID] Exists in table2, update, else insert case [table1]。[tableID]存在于table2中,更新,否则插入

Thanks for all the attempts so far!!If i figure this out I'm gonna post it. 感谢到目前为止的所有尝试!如果我弄清楚了,我将其发布。

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). 我们需要更多信息,但我将为您提供98%或更高的赔率,它们可以全部通过两个查询(一个用于插入,一个用于更新)来完成。

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 */

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. 例如,您可以选择t1和t3的左连接,如果存在t3值则使用t3值,或者使用基于t1列值的表达式。 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) (增加有关您的具体情况的详细信息会提高获得帮助的质量)

I'm going to jump the gun and assume that you're talking about MS SQL Server. 我将大开眼界,并假设您正在谈论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. 连接表并使用Case语句执行逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM