Be careful when adding-comments in the middle of exec SQL

Source: Internet
Author: User

Sometimes in an SQL operation, a table is used as a variable, and the name of the table must be suffixed by time, so it cannot be directly executed.

So we have to pass in as a variable, as shown below:CodeFragment

 Set   @ Alarmtable   =   '  D_alarm_log  '   +   Substring ( Convert ( Varchar ( 20 ), Getdate (), 112 ), 1 , 6  )........  --  Add Object Name      If    Not   Exists ( Select   *   From Syscolumns Where ID =   Object_id (N ' [DBO]. [  '  +  @ Alarmtable  +  '  ]  ' ) And Name =  '  Object_name  '  )  Begin          Set   @ SQL  =   '  ALTER TABLE  '   +   @ Alarmtable   +   '  Add object_name varchar (128)  '          Print   @ SQL          Execute ( @ SQL  )  End 

In this exec @ SQL method, as long as there is a -- originally intended to comment the current row, but when the result is included in @ SQL, -- all subsequent statements are injected.

Be careful not to confirm @ SQL print ....

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.