4

私はSQLにかなり慣れていないので、これが単なるSQLコンパイラの問題かどうかはわかりません。JAVA や PHP を使用する前に SQL を使用したことがある場合は、単純な 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 がまだ定義されていないため、エラーが発生します。これを実行して、列をコンパイルして追加して更新する方法はありますか、それとも複数回実行する必要がありますか?

ありがとう!

4

4 に答える 4

9

操作ごとに複数のファイルを使用するか、手順の間に「GO」を挿入します。

ALTER TABLE House DROP COLUMN CustomerType
GO

ALTER TABLE House ADD CustomerType char(11)
GO

UPDATE House
SET CustomerType = CASE
    WHEN ... THEN...
    WHEN ... THEN...
    ELSE "Other"
    END
GO

これは SQL Server Management Studio で機能します。これは T-SQL 機能ではなく、SQL コマンドの T-SQL "バッチ" を分離するための Mgmt Studio の機能です。

更新:スクリプトを複数回実行できるようにしたい場合、より便利な方法は、列の存在を確認し、まだ存在しない場合にのみ追加することです。常にドロップして再実行しても意味がありません- 既に存在する場合は追加します!

これを行うには、次のようなものを使用します。

IF NOT EXISTS(SELECT * FROM sys.columns WHERE name = 'CustomerType' 
                       AND object_id = OBJECT_ID('House'))
   ALTER TABLE dbo.House ADD CustomerType CHAR(11)
GO

このコード スニペットは、SQL カタログ ビューsys.columnsをチェックして、その列がそのテーブルに既に存在するかどうかを確認します。存在しない場合は作成されます。テーブルに対してこのコードを 1000 回実行すると、最初は列が作成され、それ以降の実行では何も実行されません。その列は既に存在するためです。常に列を削除して再追加するよりもはるかにクリーンです!

于 2010-08-11T15:43:33.973 に答える
4

GO2 番目のステートメントの後、ALTERステートメントの前にステートメントを入れてみてくださいUPDATE

于 2010-08-11T15:42:42.207 に答える
1

おそらく、UPDATE を実行する前に変更をスキーマにコミットする必要があります。

スキーマ DDL は、DML/DQL に到達するまでに解決する必要があります。なぜその列を削除したり追加したりしなければならないと思いますか?

于 2010-08-11T15:44:32.723 に答える
1

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:

CREATE TABLE #House (housekey int, number int, street char(20))

insert into #House (housekey, number, street) values (1, 123, 'Wilson Ave')
insert into #House (housekey, number, street) values (2, 124, 'Wilson Ave')
insert into #House (housekey, number, street) values (3, 125, 'Wilson Ave')

alter table #House DROP COLUMN street
alter table #House ADD street varchar(20)

update #House
set street = case
    when number = 123 then 'Wilson Ave'
    when number = 124 then 'Willson Ave'
    when number = 125 then 'Wulson Ave'
    else 'Xxy St'
end

select * from #house

drop table #house

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.

于 2010-08-11T15:57:19.427 に答える