久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

是否可以告訴 SSMS 不檢查 t-sql 腳本中是否存在列

Is it possible to tell SSMS not to check if a column exists in a t-sql script?(是否可以告訴 SSMS 不檢查 t-sql 腳本中是否存在列?)
本文介紹了是否可以告訴 SSMS 不檢查 t-sql 腳本中是否存在列?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我試著用谷歌搜索它,但沒有找到方法

I tried to google it, but din't find a way

我有一個 t-sql 腳本,它向表中添加一個新列,然后根據同一個表中的一些其他列用值填充該列,最后刪除一些列.這一切正常.

I have a t-sql script that adds a new column to a table, then fills that columns with values depending on some other columns in the same table and finally removes some columns. This all works fine.

當我想再次運行腳本時出現問題.我有一個 if 子句來檢查缺失的列是否存在,但是即使 if 子句中的代碼沒有運行,SSMS 仍然會抱怨并顯示錯誤消息.該腳本必須能夠多次運行,而且我不希望顯示錯誤消息!

The problem occures when I want to run the script again. I have a if clause that checks if the missing columns exists, but SSMS still complains and displays error messaged even though the code inside the if clause if not run. The script must be able to run more then once, and I don't want the error messages to be displayed!

在代碼中(顯然是測試代碼,不想在此處轉儲生產代碼...):

In code (obviously test code, don't want to dump production code here...):

create table test (
 Name text,
 Switch int,
 ValueA int,
 ValueB int)
go

insert into test values ('Name', 0, 5, 10)

if not exists (select 1 from INFORMATION_SCHEMA.COLUMNS
      where COLUMN_NAME = 'ValueC' and TABLE_NAME = 'test')
begin
 alter table test
 add ValueC int
end
go

-- This batch rasies error when run more then once!
if exists (select 1 from INFORMATION_SCHEMA.COLUMNS
     where COLUMN_NAME = 'ValueA' and TABLE_NAME = 'test')
begin
 update test
 set ValueC = (select case Switch
      when 0 then (select (ValueA - ValueB))
      when 1 then (select (ValueB - ValueA))
     end)
end
go

if exists (select 1 from INFORMATION_SCHEMA.COLUMNS
     where COLUMN_NAME = 'ValueA' and TABLE_NAME = 'test')
begin
 alter table test drop column ValueA
end
go

select * from test
--Name 0 10 -5

錯誤信息如下:

Msg 207, Level 16, State 1, Line 6
Invalid column name 'ValueA'.
Msg 207, Level 16, State 1, Line 7
Invalid column name 'ValueA'.

干杯--喬克

推薦答案

是的,沒有動態 SQL 也是可能的,但需要一些笨拙的解決方法.我會為此使用 EXEC.

Yes it is possible without dynamic SQL but with a bit of a kludgey workaround. I would just use EXEC for this.

SQL 2000 中的行為 在此處解釋

The behaviour in SQL 2000 is explained here

Erland Sommarskog 提到一旦查詢中的所有表都存在,SQL Server 就會對查詢執行全面檢查."

Erland Sommarskog mentions "once all tables in a query exist, SQL Server performs full checks on the query."

因此,通過在查詢中向不存在的表添加 no-op 引用,可以推遲編譯.通過這種調整,下面的腳本可以多次運行而不會出錯.

So by adding a no-op reference in the query to a table that doesn't exist compilation can be deferred. With this adjustment the script below can be run multiple times without getting the error.

insert into test values ('Name', 0, 5, 10)

if not exists (select 1 from INFORMATION_SCHEMA.COLUMNS
      where COLUMN_NAME = 'ValueC' and TABLE_NAME = 'test')
begin
 alter table test
 add ValueC int
end
go

create table #dummy
(i int)

-- This batch raised error when run more then once!
if exists (select 1 from INFORMATION_SCHEMA.COLUMNS
     where COLUMN_NAME = 'ValueA' and TABLE_NAME = 'test')
begin
 update test
 set ValueC = (select case Switch
      when 0 then (select (ValueA - ValueB))
      when 1 then (select (ValueB - ValueA))
     end) where not exists(select * from #dummy)
end

drop table #dummy
go


if exists (select 1 from INFORMATION_SCHEMA.COLUMNS
     where COLUMN_NAME = 'ValueA' and TABLE_NAME = 'test')
begin
 alter table test drop column ValueA
end



go


select * from test
--Name 0 10 -5

這篇關于是否可以告訴 SSMS 不檢查 t-sql 腳本中是否存在列?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 亚洲综合视频 | www.狠狠干| 午夜小电影 | h片在线看 | 性做久久久久久免费观看欧美 | 国产精品一卡 | 奇米视频777 | 成人av一区 | 亚洲97| 99色播| 国产日韩欧美激情 | 9191成人精品久久 | 三a毛片| 国产精品亚洲片在线播放 | 视频1区2区 | 久久99网| 日韩第一页 | 国产精品我不卡 | 国产一区久久精品 | 999久久久久久久 | 一区二区三区亚洲精品国 | 欧美一级小视频 | 国产精品不卡一区 | 欧美午夜精品 | 亚洲天天| 久久久久久久夜 | 国产精品久久欧美久久一区 | 99久久精品免费看国产免费软件 | 91伊人网 | 国产精品色 | 久久精品国产一区二区电影 | 日韩精品一区二区三区免费观看 | 亚洲自拍偷拍欧美 | 欧美一区二区三区四区五区无卡码 | 自拍偷拍亚洲一区 | 中文二区 | 亚洲精品日韩精品 | www.99re| 亚洲在线一区 | 色天堂影院 | 天天干夜夜操 |