Guest User

Untitled

a guest
Jun 17th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. IF EXISTS (SELECT name FROM syscolumns
  2. WHERE name = 'COLUMN_THAT_NO_LONGER_EXISTS')
  3. BEGIN
  4. INSERT INTO TABLE1
  5. (
  6. COLUMN_THAT_NO_LONGER_EXISTS,
  7. COLUMN_B,
  8. COLUMN_C
  9. )
  10. SELECT 1,2,3 FROM TABLE2
  11.  
  12. ALTER TABLE TABLE1 DROP COLUMN COLUMN_THAT_NO_LONGER_EXISTS
  13. END
  14.  
  15. IF Not EXISTS (SELECT name FROM sys.columns
  16. WHERE name = 'COLUMN_THAT_NO_LONGER_EXISTS' and Object_Name(object_id) = 'Table1')
  17.  
  18. IF Not EXISTS (SELECT name FROM sys.columns
  19. WHERE name = 'COLUMN_THAT_NO_LONGER_EXISTS' and Object_Name(object_id) = 'Table1')
  20.  
  21. create table tblTests
  22. (
  23. TestID int identity (1,1),
  24. TestColA int null,
  25. TestColB int null
  26. )
  27.  
  28. go -- Ran this on its own
  29.  
  30.  
  31.  
  32. insert into tblTests (TestColA, TestColB)
  33. Select 1,2
  34. go 10
  35. -- Insert some initial data
  36.  
  37. alter table tblTests
  38. add TestColC Int
  39. go -- alter the table to add new column
  40.  
  41. -- Run this with column and then after it has removed it
  42.  
  43. IF EXISTS (SELECT name FROM sys.columns a
  44. WHERE name = 'TestColC' AND
  45. OBJECT_NAME(object_id) = 'tblTests')
  46. Begin
  47. insert into tblTests (TestColA, TestColB, testcolc)
  48. select 1,2,3
  49.  
  50. alter table tblTests
  51. drop column TestColC
  52. End
Add Comment
Please, Sign In to add comment