重命名表列

发布于 2024-08-31 14:23:40 字数 500 浏览 3 评论 0原文

我正在尝试执行以下查询。我没有“CrewID”列,因此在这种情况下它将绕过脚本的更新部分。但它给出错误 Invalid object CrewID'。您能否告诉我为什么即使我的 if 条件不匹配它也会执行更新部分。还有另一种方法可以做到同样的事情吗?我有需要重命名列的要求,但在重命名之前,我必须复制其他列中的数据,并且需要多次执行脚本。

if exists (select * from syscolumns where name ='CrewID' and id in (select id from dbo.sysobjects where id = object_id(N'[dbo].[WorkPlanAssignees]') and OBJECTPROPERTY(id, N'IsUserTable') = 1))
BEGIN   
update A set A.TempCrewID=B.ID from WorkPlanAssignees A inner join Crew B on A.CrewID=B.ID
END

I am trying to execute the following query. I don't have 'CrewID' column so in that case it will by pass update part of the script. but it gives error Invalid object CrewID'. Can you please tell me why it excute update part even my if condition does not matched. Is there is another way to do the same. I have the requirement where need to rename the column but before rename i have to copied data in other column and need to excute script many times.

if exists (select * from syscolumns where name ='CrewID' and id in (select id from dbo.sysobjects where id = object_id(N'[dbo].[WorkPlanAssignees]') and OBJECTPROPERTY(id, N'IsUserTable') = 1))
BEGIN   
update A set A.TempCrewID=B.ID from WorkPlanAssignees A inner join Crew B on A.CrewID=B.ID
END

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

把人绕傻吧 2024-09-07 14:23:40

SQL不是逐行执行的。它被编译为批处理。在编译时,该列不存在。所以它给出了一个错误。 IF 是无关紧要的。

使用动态 SQL 或让我们知道您想要做什么。

并之前询问:

存储过程包括添加列、更新该列的数据以及从该表中选择所有数据

SQL is not executed line by line. It's compiled as a batch. At compile time, the column does not exists. So it gives an error. The IF is irrelevant.

Use dynamic SQL or let us know what you're trying to do.

And asked before:

Stored Procedure consist Add column, Update data for that column, and Select all data from that table

梦毁影碎の 2024-09-07 14:23:40

我遇到了同样的问题,发现除了使用动态 sql 之外,我还可以通过交叉连接到只有一行的临时表来解决它。这导致脚本编译器在编译时不会尝试解析重命名的列。下面是我在不使用动态 SQL 的情况下解决问题的示例

select '1' as SomeText into #dummytable

update q set q.ValueTXT = convert(varchar(255), q.ValueTXTTMP) from [dbo].[SomeImportantTable] q cross join #dummytable p

I ran into this same issue and found that in addition to using dynamic sql I could solve it by cross joining to a temp table that had only one row. That caused the script compiler to not try to resolve the renamed column at compile time. Below is an example of what I did to solve the issue without using dynamic SQL

select '1' as SomeText into #dummytable

update q set q.ValueTXT = convert(varchar(255), q.ValueTXTTMP) from [dbo].[SomeImportantTable] q cross join #dummytable p
神爱温柔 2024-09-07 14:23:40

首先,重命名旧表:

ALTER TABLE orig_table_name RENAME TO tmp_table_name;

然后基于旧表但使用更新的列名称创建新表:

CREATE TABLE orig_table_name (
  col_a INT
, col_b INT
);

然后从原始表复制内容。

 INSERT INTO orig_table_name(col_a, col_b)
SELECT col_a, colb
FROM tmp_table_name;

最后,删除旧表。

DROP TABLE tmp_table_name;

将所有这些包装在 BEGIN TRANSACTION 中;并提交;也可能是个好主意。

希望有帮助。

First you rename the old table:

ALTER TABLE orig_table_name RENAME TO tmp_table_name;

Then create the new table, based on the old table but with the updated column name:

CREATE TABLE orig_table_name (
  col_a INT
, col_b INT
);

Then copy the contents across from the original table.

 INSERT INTO orig_table_name(col_a, col_b)
SELECT col_a, colb
FROM tmp_table_name;

Lastly, drop the old table.

DROP TABLE tmp_table_name;

Wrapping all this in a BEGIN TRANSACTION; and COMMIT; is also probably a good idea.

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文