检查表是否存在 SQL

发布于 2024-10-27 01:42:12 字数 599 浏览 3 评论 0原文

我有一个查询如下:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜访吸血鬼 2024-11-03 01:42:13

你可以这样做:
SQL 2000 语法

    IF EXISTS (SELECT 1 FROM sysobjects  WHERE xtype='u' AND name='CVG86_SPE') 
BEGIN
             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 
END

SQL 2005 及更高版本:

        IF EXISTS ( SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'CVG86_SPE' )
BEGIN
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
END

you can do it like this:
SQL 2000 syntaxis

    IF EXISTS (SELECT 1 FROM sysobjects  WHERE xtype='u' AND name='CVG86_SPE') 
BEGIN
             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 
END

SQL 2005 and later:

        IF EXISTS ( SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'CVG86_SPE' )
BEGIN
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
END
月光色 2024-11-03 01:42:13

在 SQL Server 中执行相同操作的另一种方法是使用 OBJECT_ID() 函数:

IF OBJECT_ID('table name', 'U') IS NOT NULL BEGIN
  INSERT ...
  ...
END

第二个参数告诉函数专门搜索表,而不是搜索任意对象,例如存储过程或用户定义的函数。

如果要检查临时表是否存在,请像这样使用它:(

IF OBJECT_ID('tempdb..#tmp table name') IS NOT NULL BEGIN
  INSERT ...
  ...
END

这里第二个参数是不必要的,因为从对象的名称可以清楚地看出它只能是一个表。)

Another way to do the same in SQL Server is to use the OBJECT_ID() function:

IF OBJECT_ID('table name', 'U') IS NOT NULL BEGIN
  INSERT ...
  ...
END

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:

IF OBJECT_ID('tempdb..#tmp table name') IS NOT NULL BEGIN
  INSERT ...
  ...
END

(Here the second parameter is unnecessary because from the name of the object it is clear that it can only be a table.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文