SQL Server generates a risk (or bug) when modifying the column type script through the interface)

Source: Internet
Author: User

I believe that many netizens have the habit of generating and executing scripts on the SQL interface. Today, we see a case in csdn, which is a big risk -_-!!!

In this example, the columns of a table are converted from datetime to Char:

Step 1 generate a database:

 Use  Master  Go  --  Create a Test Database  Create   Database   [  Db_test  ]   On   Primary  (Name  = N'  Db_test  '  , Filename  = N '  D: \ sql2008 \ data \ db_test.mdf  '  , Size  =  512 MB, filegrowth  =  1024kb, maxsize  =  524288kb)  Log  On  (Name  = N '  Db_test_log  '  , Filename  = N '  D: \ sql2008 \ log \ db_test_log.ldf  '  , Size  =  1024kb, filegrowth  =  1024kb) Go  

Step 2 generate a table and test data

 Use   [  Db_test  ]  Go  Create   Table Tb_test (ID Int   Identity ( 1 , 1 ) Primary   Key , BDatetime  )  Go  Insert   Into Tb_test Select   Getdate  ()  Go   100000 

Step 3 modify the column type on the interface and generate a script:

The generated script is:

 Begin   Transaction  Set Quoted_identifier On Set Arithabort On  Set Numeric_roundabort Off  Set Concat_null_yields_null On  Set Ansi_nulls On  Set Ansi_padding On  Set Ansi_warnings On  Commit  Begin  Transaction  Go  Create   Table  DBO. tmp_tb_test (ID  Int   Not   Null   Identity ( 1 , 1  ), B  Char ( 7000 ) Null )  On   [  Primary  ]  Go  Alter   Table DBO. tmp_tb_test Set (Lock_escalation =   Table  )  Go  Set   Identity_insert DBO. tmp_tb_test On Go  If   Exists ( Select   *   From  DBO. tb_test)  Exec ( '  Insert into DBO. tmp_tb_test (ID, B) Select ID, convert (char (7000), B) from DBO. tb_test with (holdlock tablockx)  '  )  Go  Set   Identity_insert DBO. tmp_tb_test Off  Go  Drop   Table  DBO. tb_test  Go  Execute Sp_rename n '  DBO. tmp_tb_test  ' , N '  Tb_test  ' , '  Object  '  Go  Alter   Table DBO. tb_test Add   Constraint  Pk1_tb_test1_3214ec277f60ed59  Primary   Key   Clustered  (ID)  With (Statistics_norecompute =   Off , Ignore_dup_key =   Off , Allow_row_locks =   On , Allow_page_locks =   On ) On   [  Primary  ]  Go  Commit 

Step 4: Get the generated script to the SSMs query for execution:

Text:

MSG 1105, Level 17, state 2, line 1
You cannot allocate space for the object 'dbo. tmp_tb_test 'in the database 'db _ test' because the 'primary' file group is full. Delete unnecessary files, delete objects in the file group, add other files to the file group, or enable automatic growth for existing files in the file group to increase the available disk space.
MSG 1088, level 16, State 11, line 1
The "DBO. tmp_tb_test" object cannot be found because it does not exist or you do not have the required permissions.
MSG 15248, level 11, state 1, procedure sp_rename, line 321
The @ objname parameter is not clear or the @ objtype (object) declared is incorrect.
MSG 4902, level 16, state 1, line 1
The "DBO. tb_test" object cannot be found because it does not exist or you do not have the required permissions.
MSG 3902, level 16, state 1, line 1
The commit transaction request does not have the corresponding begin transaction.

Step 5 check the table again and find that the table you want to modify is missing ..

 

Step 6: redo it again. This time we use the interface operation as shown in, but the table does not disappear:

Step 7 modify the generated script, remove go, and then execute:

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.