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
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
Are you running it from SQL Server, or a different RDBMS?
Great video... try to post more often
@jazzista1967 I will. Thanks for the feedback!