This article is a mirror article of machine translation, please click here to jump to the original article.

View: 12475|Reply: 0

[Source] SQL Server 2000 creates 1024 tables in a loop

[Copy link]
Posted on 2/9/2015 2:04:26 PM | | |
  1. declare @i int
  2. declare @j int
  3. declare @sql varchar(4000)
  4. declare @sqlsource_delete varchar(4000)
  5. declare @sqlsource_create varchar(4000)
  6. declare @sn varchar(10)

  7. set @sql = ''
  8. set @i=1
  9. set @sqlsource_delete = 'drop table [dbo].[Test_{SN}]'
  10. set @sqlsource_create = 'CREATE TABLE [dbo].[Test_{SN}] (
  11. [iid] [int] IDENTITY (1, 1) NOT NULL ,
  12. [headid] [int] NULL ,
  13. [istid] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL ,
  14. [istname] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL ,
  15. [isturl] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL ,
  16. [ititle] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL ,
  17. [icontents] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL ,
  18. [icontent] [ntext] COLLATE Chinese_PRC_CI_AS NULL ,
  19. [iurl] [ntext] COLLATE Chinese_PRC_CI_AS NULL ,
  20. [ipic] [ntext] COLLATE Chinese_PRC_CI_AS NULL ,
  21. [ipdate] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
  22. [iedate] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
  23. [icompname] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
  24. [icompemail] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL ,
  25. [iphone] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL ,
  26. [ifax] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
  27. [ishengf] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
  28. [iaddress] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL ,
  29. [ilianxiren] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
  30. [imobile] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
  31. [isite] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL ,
  32. [isamecount] [int] NULL ,
  33. [imainid] [int] NULL ,
  34. [creationDate] [datetime] NOT NULL DEFAULT getdate() ,
  35. [lastModifiedDate] [datetime] NOT NULL DEFAULT getdate()
  36. CONSTRAINT [PK_Test_{SN}] PRIMARY KEY  CLUSTERED
  37. (
  38.   [iid]
  39. )  ON [PRIMARY]  
  40. ) ON [PRIMARY]'

  41. while @i<=1024
  42. begin
  43.     set @sn = cast(@i as varchar(10))
  44.     set @j=len(@sn)
  45.    
  46.     if exists (select * from dbo.sysobjects where id = object_id('[dbo].[Test_' + @sn + ']') and OBJECTPROPERTY(id, 'IsUserTable') = 1)
  47.     begin
  48.         set @sql = replace(@sqlsource_delete, '{SN}', @sn)
  49. --print @sql
  50.         execute(@sql)
  51.     end

  52.     set @sql = replace(@sqlsource_create, '{SN}', @sn)
  53.     --print @sql
  54.     execute(@sql)

  55.     set @i = @i + 1
  56. end
Copy code





Previous:XML Beginner Syntax
Next:SQL method of emptying table data
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com