IF EXISTS, DROP TABLE, SELECT INTO, INSERT INTO, SYSOBJECTS, XTYPE

แชร์
ฝัง
  • เผยแพร่เมื่อ 9 ก.ย. 2024
  • This video is the beginning portion of a much larger video where I will cover how I used sql code to generate data for a puzzle book. In this particular video, I cover IF EXISTS, DROP TABLE, SELECT INTO, INSERT INTO, SYSOBJECTS, XTYPE, and certain errors that you will need to look out for.
    Link to my SQL Courses on Udemy: www.udemy.com/...
    --------------------------------------------------------------------------
    -- THIS WILL CRASH, IF THE TABLE DOES NOT EXIST
    --------------------------------------------------------------------------
    --IF EXISTS (SELECT * FROM A)
    --DROP TABLE dbo.A
    --------------------------------------------------------------------------
    -- THIS WILL ALWAYS WORK
    --------------------------------------------------------------------------
    IF EXISTS (SELECT * FROM sysobjects WHERE name = 'A' AND xtype = 'u')
    DROP TABLE dbo.A
    --------------------------------------------------------------------------
    SELECT '1' A INTO A
    INSERT INTO A SELECT '2'
    INSERT INTO A SELECT '3'
    INSERT INTO A SELECT '4'
    INSERT INTO A SELECT '5'
    INSERT INTO A SELECT '6'
    SELECT * FROM A

ความคิดเห็น • 4

  • @dima8832
    @dima8832 15 วันที่ผ่านมา +1

    Thank you very useful.. one thing though .. the query - If exists (Select * from A) Drop table A will work only if there are any records in the table .. If you delete first from the table then it will not work as select * will not return data

    • @SQLUniversity
      @SQLUniversity  13 วันที่ผ่านมา

      Are you running it from SQL Server, or a different RDBMS?

  • @jazzista1967
    @jazzista1967 หลายเดือนก่อน +1

    Great video... try to post more often

    • @SQLUniversity
      @SQLUniversity  หลายเดือนก่อน

      @jazzista1967 I will. Thanks for the feedback!