SQL Server,如果满足条件如何删除列?

发布于 2024-09-15 10:59:49 字数 511 浏览 13 评论 0原文

我一直在谷歌上寻找这个问题的答案,但没有运气。任何帮助都会很棒!

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

深爱成瘾 2024-09-22 10:59:49

请重新考虑您的数据库设计。完全删除问题列并添加表格,

Question (QuestionID int identity, Deleted datetime, Text text)  
Answer (FeedbackID int FK to Feedback, QuestionID int FK to Question, Text text)

然后使用 Question.Deleted == null 作为在表单上显示或隐藏问题的条件。

Please reconsider your DB design. Drop question columns entirely and add tables

Question (QuestionID int identity, Deleted datetime, Text text)  
Answer (FeedbackID int FK to Feedback, QuestionID int FK to Question, Text text)

then use Question.Deleted == null as you criteria wherever to show or hide questions on your form.

乙白 2024-09-22 10:59:49
IF COLUMNPROPERTY(OBJECT_ID('myTable'), 'ColumnID', 'Question_1') IS NOT NULL
    ALTER TABLE tblFeedback DROP COLUMN Question_1

但是,这将删除所有行:例如,您不能让某些行包含答案而某些行包含问题。

如果我理解正确的话,我将实现为子表,以允许 0 到 n 个问题/答案/反馈,并使用定义问题/答案/反馈等的类型。

IF COLUMNPROPERTY(OBJECT_ID('myTable'), 'ColumnID', 'Question_1') IS NOT NULL
    ALTER TABLE tblFeedback DROP COLUMN Question_1

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.

骄兵必败 2024-09-22 10:59:49

对于这种事情,您可能更适合使用 EAV(实体属性值)样式的数据库结构。

create table Question
(
    Id bigint not null primary key identity(1,1),
    Question nvarchar(max) not null
)

create table Feedback
(
    Id bigint not null primary key identity(1,1),
    Date datetime not null,
    Name nvarchar(512) not null,    
)

create table FeedbackAnswers
(
    Id bigint not null primary key identity(1,1),   
    FeedbackId bigint not null,
    QuestionId bigint not null,
    Answer nvarchar(max) not null
)

您可能需要其他东西来将问题“分组”在一起。

不过,它确实让报道变得更加复杂。

You might be better with an EAV (entity attribute vaue) style database structure for this kind of thing.

create table Question
(
    Id bigint not null primary key identity(1,1),
    Question nvarchar(max) not null
)

create table Feedback
(
    Id bigint not null primary key identity(1,1),
    Date datetime not null,
    Name nvarchar(512) not null,    
)

create table FeedbackAnswers
(
    Id bigint not null primary key identity(1,1),   
    FeedbackId bigint not null,
    QuestionId bigint not null,
    Answer nvarchar(max) not null
)

You will probably need something else to "group" the questions together.

It does make reporting a little more involved though.

弄潮 2024-09-22 10:59:49

如果您确实想在每次反馈表单更改时根据列名称更改表结构,则需要为此使用动态 SQL 并查询 sys.columns。这似乎是一个不寻常的要求,因为它会删除您可能出于某种原因存储的历史数据,

但如果您只是想自动化一些原本需要手动执行的操作...

示例语法

DECLARE @dynsql NVARCHAR(4000)

SELECT @dynsql = ISNULL(@dynsql + ',','') + QUOTENAME(name)
 FROM sys.columns WHERE name LIKE 'Question%'
 AND OBJECT_ID=OBJECT_ID('dbo.tblFeedback')

IF(@@ROWCOUNT > 0)
EXEC ('ALTER TABLE dbo.tblFeedback DROP COLUMN ' + @dynsql)

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 place

but if you just want to automate something that you would otherwise need to do manually...

Example Syntax

DECLARE @dynsql NVARCHAR(4000)

SELECT @dynsql = ISNULL(@dynsql + ',','') + QUOTENAME(name)
 FROM sys.columns WHERE name LIKE 'Question%'
 AND OBJECT_ID=OBJECT_ID('dbo.tblFeedback')

IF(@@ROWCOUNT > 0)
EXEC ('ALTER TABLE dbo.tblFeedback DROP COLUMN ' + @dynsql)
夜空下最亮的亮点 2024-09-22 10:59:49

为了以后发现这个问题的人的利益,这种事情是可能的,作为对查询结果有条件地执行命令的基础(在这种情况下,在采取之前检查列只包含NULL)诸如删除列之类的操作):

IF (SELECT COUNT([my column]) FROM [my table] WHERE [my column] IS NOT NULL) = 0
  PRINT 'remove' ELSE PRINT 'keep';

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) :

IF (SELECT COUNT([my column]) FROM [my table] WHERE [my column] IS NOT NULL) = 0
  PRINT 'remove' ELSE PRINT 'keep';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文