0

I am attempting to create a stored procedure that will update a specific row and column location based on user input. My table has approximately 100 columns utilizing names in progressive increments. Ex Page1, Page2, Page3, Page 4.... etc. The data must also be updated in progressive increments as users finish different versions of each page.

When the procedure is called I need it to find the user's row(Page1 is the key and unique), and put the information in where their version of the file is saved into the first NULL column. I have seen talk about using cursors for similar applications but I am not sure if this is the proper solution.

 ----------------------------------------------------
 |  Page1    |   Page2    |   Page3    |   Page4    |   
 ----------------------------------------------------
 |  /pg1.htm |   /pg2.htm |  /pg3.htm  |   NULL     |
 ----------------------------------------------------
 | /pg1.doc  |   /pg2.doc |   NULL     |   NULL     |
 ----------------------------------------------------
 | /pg1.pdf  |  NULL      |   NULL     |   NULL     |
 ----------------------------------------------------

I need the procedure to update the row sequentially each time with one piece of data when it is called. My issue is in making this scaleable instead of limiting it with 100 + IF statements.

The pseudo code I cooked up looks like this but is terribly inefficient:

 FIND ROW that matches unique key
 LOOP Find_NULL
     IF Column2 == NULL
         UPDATE DATA HERE
     ELSE IF Column3 == NULL
         UPDATE DATA HERE
     ELSE IF Column4 == NULL
         UPDATE DATA HERE
     ELSE IF.... and on and on
 END LOOP Find_NULL

I am utilizing MySQL which I have been told does not support Dynamic SQL. I attempted to create a variable and modify it as I went through the data to store the next NULL column however this did not work.

If anyone has any solutions or advice I would appreciate it.

Thanks in advance.

4

3 回答 3

3

乍一看,您似乎遭受了相当糟糕的数据库设计的困扰。

您不想将列命名为“Page1”、“Page2”...“Page 100”,然后在很多时候让这些列为 NULL。这违反了合理的数据库设计。您可能想要查看诸如数据库规范化之类的概念(例如,第一范式、第二范式等)。

我认为拥有一个名为“Page”的列会更好,然后每一行的值都为 1 到 100,以及与页面相关的信息。这样,您就不需要在形成插入/更新查询时尝试动态地拼凑列名。

于 2013-08-08T14:21:31.113 回答
2

我建议您将列转换为行。这样你只需要插入一个新行,效率更高。

该表将包含一个序列列以保留值的顺序。

User,Seq,Val
1,1,/pg1.htm
1,2,/pg2.htm
1,3,/pg3.htm
2,1,/pg1.doc
2,2,/pg2.doc
3,1,/pg1.pdf
于 2013-08-08T14:23:19.503 回答
0

您的表格不是第一范式,因此您正遭受与该设计问题密切相关的所有问题。

如果您查看该链接,您会看到建议的解决方案是创建一个单独的表,将重复组链接到父实体(您的当前行)。在您的特定情况下,这意味着您将有一个单独的 Pages 表,将页码(1、2、3、...、n)链接到此(父)表中的唯一键,并存储与该页面相关的数据在页表中。

于 2013-08-08T14:26:06.407 回答