是否可以告诉 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 脚本中是否存在列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Creating table with T-SQL - can#39;t see created tables in Object explorer(使用 T-SQL 创建表 - 在对象资源管理器中看不到创建的表)
How to check if VARCHAR strings are (not) hexadecimal?(如何检查 VARCHAR 字符串是否为(非)十六进制?)
Arithmetic overflow error converting IDENTITY to data type int(将 IDENTITY 转换为数据类型 int 的算术溢出错误)
Where clause if there are multiple of the same ID(如果有多个相同的 ID,Where 子句)
Azure SQL: Invalid Object Name using Powershell#39;s quot;Invoke-sqlcmdquot; on Adventureworks(Azure SQL:使用 Powershell 的“Invoke-sqlcmd的无效对象名称在 Adventureworks 上)
How to Convert Table Data into xml format using sql with multiple sub nodes(如何使用具有多个子节点的sql将表数据转换为xml格式)