SQL Server,如果满足条件如何删除列?
我一直在谷歌上寻找这个问题的答案,但没有运气。任何帮助都会很棒!
我有一个 SQL 表:tblFeedback。它存储反馈问题的答案。问题保存在不同的表中:tblQuestions。反馈表上的问题可以通过用户界面进行更改,因此当用户编辑替换问题时,我将问题重写为 tblQuestions 并在 tblFeedback 中提供答案列(每个问题一个)。希望这很容易理解。
所以现在我有 3 个问题。我的 tblFeedback 列如下所示:
反馈ID
姓名
日期
问题_1
问题_2
Question_3
我想删除这些列,并在必要时用新列替换它们。我正在使用 ALTER TABLE tblFeedback DROP COLUMN 的路径......但我无法使用 WHERE 或其他任何内容添加任何条件。 3' 但如果我在 COLUMN 之后添加 WHERE 则会出现错误。
我正在使用 C# 为 ASP.NET 应用程序编写此内容。
对此的任何帮助将非常感激。我要去散步冷静一下。
I have been trawling google for answers to this but no luck. Any help would be great!
I have a SQL table: tblFeedback. It stores answers to feedback questions. The Questions are held in a different table: tblQuestions. The questions on the feedback form can be changed via a user interface, therefore when the user edits the questions of replaces them, I rewrite the questions to tblQuestions and provide answer columns in tblFeedback (one for each question). Hope thats easy enough to understand.
So at the minute I have 3 questions. My tblFeedback columns look like:
FeedbackID
Name
Date
Question_1
Question_2
Question_3
I want to delete these columns and replace them with new ones when necessary. I am going down the path of using the ALTER TABLE tblFeedback DROP COLUMN ... but I cannot add any criteria using WHERE or anything else.. If I could specify something like 'if column_name starts with Question_%' or 'if column_ID > 3' but if I add WHERE after the COLUMN I get errors.
I am writing this for an asp.net app using c#.
Any help with this would be really really appreciated. Im going for a walk to calm down.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
请重新考虑您的数据库设计。完全删除问题列并添加表格,
然后使用 Question.Deleted == null 作为在表单上显示或隐藏问题的条件。
Please reconsider your DB design. Drop question columns entirely and add tables
then use Question.Deleted == null as you criteria wherever to show or hide questions on your form.
但是,这将删除所有行:例如,您不能让某些行包含答案而某些行包含问题。
如果我理解正确的话,我将实现为子表,以允许 0 到 n 个问题/答案/反馈,并使用定义问题/答案/反馈等的类型。
However, this will drop all rows: you can't have some rows with Answer and some with Question for example.
If I understand you correctly, I would implement as a child table to allow 0 to n question/answer/feedback with a type to define question/answer/feedback etc.
对于这种事情,您可能更适合使用 EAV(实体属性值)样式的数据库结构。
您可能需要其他东西来将问题“分组”在一起。
不过,它确实让报道变得更加复杂。
You might be better with an EAV (entity attribute vaue) style database structure for this kind of thing.
You will probably need something else to "group" the questions together.
It does make reporting a little more involved though.
如果您确实想在每次反馈表单更改时根据列名称更改表结构,则需要为此使用动态 SQL 并查询 sys.columns。这似乎是一个不寻常的要求,因为它会删除您可能出于某种原因存储的历史数据,
但如果您只是想自动化一些原本需要手动执行的操作...
示例语法
You would need to use dynamic SQL for this and query
sys.columns
if you really want to change the table structure based on column name every time the feedback form changes. This does seem an unusual requirement as it will delete historic data that you are presumably storing for some reason in the first placebut if you just want to automate something that you would otherwise need to do manually...
Example Syntax
为了以后发现这个问题的人的利益,这种事情是可能的,作为对查询结果有条件地执行命令的基础(在这种情况下,在采取之前检查列只包含
NULL
)诸如删除列之类的操作):For the benefit of those finding this question later, this kind of thing is possible as the basis of conditionally executing a command on the results of a query (in this case checking a column contains nothing but
NULL
before taking an action like dropping the column) :