Use SQL syscolumns to query table fields and splice the code, sqlsyscolumns

Source: Internet
Author: User

Use SQL syscolumns to query table fields and splice the code, sqlsyscolumns

We cannot avoid adding or updating data when writing code. However, if there are too many fields in a table, the amount of code to be written will increase. For example, we can concatenate an Insert SQL statement with StringBuilder;

You can use the code generator to generate this non-technical code, or use SQL to generate it based on the table fields:

1. the following code finds the names of all fields in a table.

Select Name From syscolumns where id = OBJECT_ID ('table name ')

2. On the basis of 1, we can splice our returned results at will. For example, we need to splice all fields with commas (, of course, we can implement this through SQL recursion, however, if you no longer make logical judgments on the results, you can try the following method to make it faster)

First run the select Name + ',' From syscolumns where id = OBJECT_ID ('table name') statement execution result, and then copy the entire column value to EditPlus, you can obtain a string after merging.



3. Based on the 2nd-step idea, we can splice and use it as needed.

Select '_ InsertSQL. append (",'' "+ _ k3dt. rows [I] ["'+ name +'"]. toString () + "'' "); 'from syscolumns where id = OBJECT_ID ('t _ ICItem ')



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.