SQL Server automatic table creation and storage process

Source: Internet
Author: User
-- Table creation and storage process

Create   Proc   [ DBO ] . [ Pn_createhistorydatatable ]  
As
Begin
  Declare   @ Count   Int
  Declare   @ Collcode   Varchar ( 4 )
  Declare   @ Tablename   Varchar ( 30 )
  Declare   @ Strsql   Nvarchar ( 2000 )
  Declare   @ Createsql   Varchar ( 2000 )
 
  Declare Curcolliery Cursor   For   Select Collcode From Tn_colliery
  Open Curcolliery
  Fetch   Next   From Curcolliery Into   @ Collcode
  While   @ Fetch_status = 0
  Begin
Set   @ Tablename   =   ' Tn_data '   +   @ Collcode   +   Replace ( Replace ( Replace ( Convert ( Varchar , Getdate (), 112 ), ' - ' , '' ), '   ' , '' ), ' : ' , '' )

Set   @ Strsql = N ' Select @ insidecount = count (*) from sysobjects where id = object_id ( ''' +   @ Tablename   + ''' ) And type = '' U '''

Exec Sp_executesql @ Strsql , N ' @ Insidecount int output ' , @ Count Output

If ( @ Count   =   0 )
Begin
Set   @ Createsql = ' Create Table ' +   @ Tablename   +  
' (Datacode varchar (20) primary key,
Devicecode varchar (4) not null,
Devicename nvarchar (20 ),
Ergonomic address nvarchar (100 ),
Collcode varchar (4) not null,
Collname nvarchar (50 ),
Coalip varchar (20 ),
Sortname nvarchar (20 ),
Typename varchar (20 ),
Devicedata varchar (4 ),
Typeunit varchar (4 ),
Statusname varchar (50 ),
Lowerlimit varchar (4 ),
Upperlimit varchar (4 ),
Uploadtime datetime default getdate ()) '

Exec ( @ Createsql )
End
Fetch   Next   From Curcolliery Into   @ Collcode
  End
  Close Curcolliery
  Deallocate Curcolliery
End

-- Execution statement

Exec Pn_createhistorydatatable
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.