code_junkie

How do I conditionally create a table in Sybase (TSQL)?

Nov 12th, 2011
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. IF EXISTS (
  2. SELECT 1
  3. FROM sysobjects
  4. WHERE name = 'a_table'
  5. AND type = 'U'
  6. )
  7. DROP TABLE a_table
  8. GO
  9.  
  10. IF NOT EXISTS (
  11. SELECT 1
  12. FROM sysobjects
  13. WHERE name = 'a_table'
  14. AND type = 'U'
  15. )
  16. CREATE TABLE a_table (
  17. col1 int not null,
  18. col2 int null
  19. )
  20. GO
  21.  
  22. IF NOT EXISTS (
  23. SELECT 1
  24. FROM sysobjects
  25. WHERE name = 'a_table'
  26. AND type = 'U'
  27. )
  28. EXECUTE("CREATE TABLE a_table (
  29. col1 int not null,
  30. col2 int null
  31. )")
  32. GO
  33.  
  34. IF(SELECT count(*) FROM sysobjects WHERE name="tableNameWithoutUserPart") > 0
  35. DROP TABLE tableNameWithUserPart
  36. GO
  37.  
  38. CREATE TABLE tableNameWithUserPart ...
  39.  
  40. IF object_id('a_table') IS NULL
  41. BEGIN
  42. CREATE TABLE a_table (
  43. col1 int not null,
  44. col2 int null
  45. )
  46. END
Add Comment
Please, Sign In to add comment