
是的,没有动态SQL是可能的,但是有一些麻烦的解决方法。我只会用
EXEC这个。
此处说明了SQL 2000中的行为
Erland Sommarskog提到“一旦查询中的所有表都存在,SQL Server会对查询执行完全检查。”
因此,通过在查询中向不存在的表添加无 *** 作引用,可以推迟编译。通过此调整,下面的脚本可以多次运行而不会出现错误。
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 intendgocreate 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)enddrop table #dummygoif exists (select 1 from INFORMATION_SCHEMA.COLUMNS where COLUMN_NAME = 'ValueA' and TABLE_NAME = 'test')begin alter table test drop column ValueAendgoselect * from test--Name 0 10 -5欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)