列的创建和使用
我对 SQL 相当陌生,所以不确定这是否只是 SQL 编译器的问题。当我之前使用过 SQL 时,都是通过 JAVA 或 PHP 来使用的,而不仅仅是直接的 SQL。我正在使用 SQL Server 2005 并尝试向表中添加一列,然后填充它。这是我现在所拥有的:
ALTER TABLE House DROP COLUMN CustomerType
ALTER TABLE House ADD CustomerType char(11)
UPDATE House
SET CustomerType = CASE
WHEN ... THEN...
WHEN ... THEN...
ELSE "Other"
END
但是,当我尝试编译它时,它会出错,因为 UPDATE 函数中的 CustomerType 尚未定义。有什么方法可以运行它,以便它可以编译并添加列并更新它,或者我是否必须在多次执行中运行它?
谢谢!
I am fairly new to SQL, so not sure if this is just a SQL compiler thing or not. When I have used SQL before it's been through JAVA or PHP, never just straight SQL. I am using SQL Server 2005 and attempting to add a column to my table, and then populate it. Here is what I have now:
ALTER TABLE House DROP COLUMN CustomerType
ALTER TABLE House ADD CustomerType char(11)
UPDATE House
SET CustomerType = CASE
WHEN ... THEN...
WHEN ... THEN...
ELSE "Other"
END
However, when I try and compile this it errors since CustomerType in the UPDATE function hasn't been defined yet. Is there any way to run this so that it will compile and add the column and update it, or do I have to run it in multiple executes?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
每个操作使用多个文件,或者在步骤之间放置“GO”:
这在 SQL Server Management Studio 中有效 - 它不是 T-SQL 功能,而是 Mgmt Studio 的一个功能,用于分隔 SQL 命令的 T-SQL“批次” 。
更新:如果您想让脚本可以多次运行,更有用的方法是检查列是否存在,并且仅在尚不存在时添加它 -如果它已经存在,那么总是删除并重新添加它是没有意义的!
为此,请使用如下内容:
此代码片段检查 SQL 目录视图 sys.columns 来查看该列是否已存在于该表中 - 如果不存在,则创建它。当您对表运行此代码一千次时,第一次会创建该列,并且任何后续运行都不会执行任何操作 - 因为该列已经存在。比不断删除和重新添加列要干净得多!
Use multiple files for each operation, or put "GO" between the steps:
This works in SQL Server Management Studio - it's not a T-SQL features, but a feature of Mgmt Studio to separate the T-SQL "batches" of SQL commands.
Update: if you want to make your script so that it can be run multiple times, the more useful approach would be to check for the column's existance, and only add it if it doesn't exist yet - no point in always dropping and re-adding it, if it's already there!
To do that, use something like:
This snippet of code checks the SQL catalog view
sys.columns
to see if that column already exists in that table - if not, it's created. When you run this code against your table a thousand times, the first time around, the column gets created, and any subsequent run will do nothing - since that column already exists. Much cleaner than constantly dropping and re-adding a column!尝试在第二个
ALTER
语句之后和UPDATE
语句之前放置GO
语句。Try putting a
GO
statement after the secondALTER
statement and before yourUPDATE
statement.也许您必须在执行 UPDATE 之前提交对架构的更改。
架构 DDL 应该在您到达 DML/DQL 时解决。您认为为什么必须不断删除和添加该列?
Perhaps you have to commit your changes to the schema prior to executing the UPDATE.
The schema DDL should be settled by the time you get to DML/DQL. Why do you think you have to keep dropping and adding that column?
它必须比您上面显示的更多。我无法让一个非常相似的脚本失败;以下效果非常好:
我认为通过在某些部分后面加上“GO”来分解脚本的想法会很有效,但是当我开始运行这个东西时,我不能让它失败。
换句话说,“它可以在我的机器上运行”。有没有什么你遗漏的事情可能会有所作为?
编辑添加:是的,GO 就是答案。我承认,“它可以在我的机器上运行”在这里有点不负责任,因为这个问题不涉及临时表,而是涉及常规表。我使用永久表尝试了这个问题,得到了与提问者相同的结果,GO 修复了它。事实上,从一开始这似乎是合乎逻辑的。
There has to be more to it than what you've shown above. I can't get a very similar script to fail; the following works just great:
I thought the idea of breaking up the script by putting "GO" after certain parts would be effective, but when I come to run the thing I can't get it to fail.
In other words, "It works on my machine." Is there anything you've left off that might make a difference?
EDIT TO ADD: Yes, the GO is the Answer. "It works on my machine" is a bit irresponsible here, I admit, because the question didn't deal with a temporary table -- but a regular one. I tried out the problem using a permanent table and got the same result as the questioner, and the GO fixed it. That seemed logical from the get-go, actually.