久久久久久久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 腳本中是否存在列?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

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

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

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

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.

當(dāng)我想再次運(yùn)行腳本時(shí)出現(xiàn)問題.我有一個(gè) if 子句來檢查缺失的列是否存在,但是即使 if 子句中的代碼沒有運(yùn)行,SSMS 仍然會(huì)抱怨并顯示錯(cuò)誤消息.該腳本必須能夠多次運(yùn)行,而且我不希望顯示錯(cuò)誤消息!

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!

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

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

錯(cuò)誤信息如下:

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

干杯--喬克

推薦答案

是的,沒有動(dòng)態(tài) SQL 也是可能的,但需要一些笨拙的解決方法.我會(huì)為此使用 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 就會(huì)對(duì)查詢執(zhí)行全面檢查."

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

因此,通過在查詢中向不存在的表添加 no-op 引用,可以推遲編譯.通過這種調(diào)整,下面的腳本可以多次運(yùn)行而不會(huì)出錯(cuò).

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

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

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

相關(guān)文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個(gè)子標(biāo)記轉(zhuǎn)換為具有多個(gè)分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個(gè)表創(chuàng)建視圖?)
Create calculated value based on calculated value inside previous row(根據(jù)前一行內(nèi)的計(jì)算值創(chuàng)建計(jì)算值)
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?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對(duì)?) - IT屋-程序員軟件開發(fā)技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉(zhuǎn)換為日期/月份編號(hào)(問題和答案的組合))
主站蜘蛛池模板: 福利成人 | 国产99热在线 | 国产精品久久久乱弄 | 一区二区免费看 | 成人av一区| 日韩欧美在| 亚洲欧美综合精品另类天天更新 | 国产日韩一区二区三区 | 特黄特色大片免费视频观看 | 国产精品黄 | 国产精品18久久久 | 成人在线免费观看 | 日韩欧美在线视频一区 | 欧美视频二区 | 久久伊人青青草 | 亚洲精选一区二区 | 欧美日韩1区2区 | 日韩在线小视频 | 精品九九 | 国产精品欧美一区二区三区不卡 | 亚洲欧美日韩精品久久亚洲区 | 自拍 亚洲 欧美 老师 丝袜 | 欧美成人一区二区 | 国产伦精品一区二区三区四区视频 | 精品视频久久久 | 久久国产精品视频 | 罗宾被扒开腿做同人网站 | 成人午夜免费视频 | 欧美aaa级 | 韩日精品一区 | 亚洲va欧美va天堂v国产综合 | 99久久精品国产麻豆演员表 | 精品视频免费 | 日本久久网 | 国产精品一区久久久久 | 日韩在线精品 | 91精品欧美久久久久久久 | 欧美视频三区 | 久久人人国产 | 99riav国产一区二区三区 | 99在线免费观看 |