如何更新 SQL Server 数据库列

huangapple go评论88阅读模式
英文:

How to update SQL Server database column

问题

I have a table called Table1 that looks something like this:

  1. MEID Type
  2. KELO mb
  3. OLEM mb-sd

And another table called Table2:

  1. MEID SD
  2. KELO NO
  3. OLEM YES

I'm updating Table2 SD column like this:

  1. UPDATE Table2
  2. SET SD = (CASE
  3. WHEN EXISTS (SELECT 1
  4. FROM Table1
  5. WHERE Table1.MEID = Table2.MEID AND Table1.[Type] LIKE '%sd%')
  6. THEN 'YES'
  7. ELSE 'NO'
  8. END)

I'm just wondering how can I update Table2 SD column if the ID is no longer "mb-sd" in Table1 the next day.

For example:

  1. MEID Type
  2. KELO mb
  3. OLEM mb
  1. MEID SD
  2. KELO NO
  3. OLEM NO

It sounds easy but I still couldn't figure it out yet, so any help would be really appreciated.

英文:

I have a table called Table1 that looks something like this :

  1. MEID Type
  2. KELO mb
  3. OLEM mb-sd

And another table called Table2:

  1. MEID SD
  2. KELO NO
  3. OLEM YES

I'm updating Table2 SD column like this :

  1. UPDATE Table2
  2. SET SD = (CASE
  3. WHEN EXISTS (SELECT 1
  4. FROM Table1
  5. WHERE Table1.MEID = Table2.MEID AND Table1.[Type] LIKE '%sd')
  6. THEN 'YES'
  7. ELSE 'NO'
  8. END)

I'm just wondering how can I update Table2 SD column if the ID is no longer "mb-sd" in Table1 the next day.

For example :

  1. MEID Type
  2. KELO mb
  3. OLEM mb
  1. MEID SD
  2. KELO NO
  3. OLEM NO

It sound easy but I still couldn't figure out yet so any help would be really appreciated.

答案1

得分: 2

首先,正如Panagiotis Kanavos提到的,在这里你的设计是非规范化的。与其Type具有'mb-sd'这样的值,你应该有两行分别带有'mb''sd'的值。

正如我在评论中提到的,这不是你应该存储在数据库中的东西;使用一个VIEW(或者可能是参数化的内联表值函数)来处理这样的情况。在一个表中存储每当另一个表中的值发生变化时都需要更改的数据几乎总是一个坏主意;通常涉及到会很快变得昂贵的TRIGGERs。如果你使用类似VIEW这样的东西,那么数据只需要在你查询时确定,而无需不断地对你的表进行UPDATE

由于我们这里只有两个表,后者似乎与第一个表相同,只是多了一个yes/no,我将转换为一个表。我还展示了如何查看表中是否都出现了mdsd,因为我怀疑这是一个后续问题。

然而,我也会对你的数据进行规范化,这是你应该做的事情:

  1. --创建示例表
  2. CREATE TABLE dbo.YourTable (MEID varchar(4),
  3. Type varchar(2));
  4. --示例数据
  5. INSERT INTO dbo.YourTable (MEID, Type)
  6. VALUES('KELO','sd'),
  7. ('OLEM','md'),
  8. ('OLEM','sd');
  9. GO
  10. --创建视图
  11. CREATE VIEW dbo.YourView AS
  12. SELECT MEID,
  13. CASE COUNT(CASE Type WHEN 'md' THEN 1 END) WHEN 0 THEN 'No' ELSE 'Yes' END AS md,
  14. CASE COUNT(CASE Type WHEN 'sd' THEN 1 END) WHEN 0 THEN 'No' ELSE 'Yes' END AS sd
  15. FROM dbo.YourTable
  16. GROUP BY MEID;
  17. GO
  18. --查询视图
  19. SELECT MEID,
  20. sd,
  21. md
  22. FROM dbo.YourView;

db<>fiddle

  1. <details>
  2. <summary>英文:</summary>
  3. Firstly, as [mentioned](https://stackoverflow.com/questions/76220049/how-to-update-sql-server-database-column#comment134412474_76220049) by [Panagiotis Kanavos](https://stackoverflow.com/users/134204/panagiotis-kanavos), it appears that your design is denormalised here. Instead of `Type` having a value like `&#39;mb-sd&#39;`, you should have 2 rows with the values `&#39;mb&#39;` and `&#39;sd&#39;`.
  4. As I [mentioned](https://stackoverflow.com/questions/76220049/how-to-update-sql-server-database-column#comment134412418_76220049) in the comments, this isn&#39;t something you should be storing in your database; use a `VIEW` (or perhaps a parametrised inline table value function) for something like this. Storing data in a table that needs to change every time a value in *another* table changes is almost always a bad idea; it normally involves `TRIGGER`s which can get expensive quickly. if you use something like a `VIEW` then the data only needs to be determined at the time you query that, and there&#39;s no need to constantly `UPDATE` your table.
  5. As we only have two tables here, and latter appears to be the same table as the first with a yes/no, I&#39;m going to switch to a single table. I also demonstrate how you could see if both `md` or `sd` appear in the table, as I suspect that is a follow on question.
  6. I also, however, normallise your data, which is something you should be doing:
  7. ```sql
  8. --Create sample table
  9. CREATE TABLE dbo.YourTable (MEID varchar(4),
  10. Type varchar(2));
  11. --Sample data
  12. INSERT INTO dbo.YourTable (MEID, Type)
  13. VALUES(&#39;KELO&#39;,&#39;sd&#39;),
  14. (&#39;OLEM&#39;,&#39;md&#39;),
  15. (&#39;OLEM&#39;,&#39;sd&#39;);
  16. GO
  17. --Create View
  18. CREATE VIEW dbo.YourView AS
  19. SELECT MEID,
  20. CASE COUNT(CASE Type WHEN &#39;md&#39; THEN 1 END) WHEN 0 THEN &#39;No&#39; ELSE &#39;Yes&#39; END AS md,
  21. CASE COUNT(CASE Type WHEN &#39;sd&#39; THEN 1 END) WHEN 0 THEN &#39;No&#39; ELSE &#39;Yes&#39; END AS sd
  22. FROM dbo.YourTable
  23. GROUP BY MEID;
  24. GO
  25. --Query view
  26. SELECT MEID,
  27. sd,
  28. md
  29. FROM dbo.YourView;

db<>fiddle

huangapple
  • 本文由 发表于 2023年5月10日 23:08:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220049.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定