检查表是否存在 SQL
我有一个查询如下:
INSERT INTO CarnetMaster.GlassLookupCapacitySpecs
(ID, CODE, NVIC, RELEASE, DISCON, DRV, TORQUE, KW, BORESTROKE, VINNUMBER, WIDTH, WHEELBASE, SEATS, COMPRATIO, TOWCAP, STEER,
TURNCIR, HEIGHT, LENGTH, VWIDTH, KERBWT, PAYLOAD, GCM, GVM)
SELECT NEWID(), CODE, NVIC, RELEASE, DISCON, DRV, TORQUE, KW, [BORE-STROKE], [VIN NUMBER], WIDTH, WHEELBASE, SEA, [TS COMPRAT], [IO TOWC],
[AP STE], [ER TURNC], [IR HEIG], [HT LENG], [TH VWID], [TH KERB], [WT PAYLO], [AD GCM], GV
FROM CVG86_SPE
如何检查表是否存在,然后从表CVG86_SPE
中选择并插入?如果表不存在,那么它不应该执行任何操作。
I have a query as below:
INSERT INTO CarnetMaster.GlassLookupCapacitySpecs
(ID, CODE, NVIC, RELEASE, DISCON, DRV, TORQUE, KW, BORESTROKE, VINNUMBER, WIDTH, WHEELBASE, SEATS, COMPRATIO, TOWCAP, STEER,
TURNCIR, HEIGHT, LENGTH, VWIDTH, KERBWT, PAYLOAD, GCM, GVM)
SELECT NEWID(), CODE, NVIC, RELEASE, DISCON, DRV, TORQUE, KW, [BORE-STROKE], [VIN NUMBER], WIDTH, WHEELBASE, SEA, [TS COMPRAT], [IO TOWC],
[AP STE], [ER TURNC], [IR HEIG], [HT LENG], [TH VWID], [TH KERB], [WT PAYLO], [AD GCM], GV
FROM CVG86_SPE
How can I check if table exists, then select from the table CVG86_SPE
and Insert? If table do not exists then it should do nothing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以这样做:
SQL 2000 语法
SQL 2005 及更高版本:
you can do it like this:
SQL 2000 syntaxis
SQL 2005 and later:
在 SQL Server 中执行相同操作的另一种方法是使用 OBJECT_ID() 函数:
第二个参数告诉函数专门搜索表,而不是搜索任意对象,例如存储过程或用户定义的函数。
如果要检查临时表是否存在,请像这样使用它:(
这里第二个参数是不必要的,因为从对象的名称可以清楚地看出它只能是一个表。)
Another way to do the same in SQL Server is to use the
OBJECT_ID()
function:The second parameter tells the function to search specifically for the table, not for an arbitrary object, like a stored procedure or a user-defined function.
If you are checking for the existence of a temporary table, use it like this:
(Here the second parameter is unnecessary because from the name of the object it is clear that it can only be a table.)