String variable strap with Header

Source: Internet
Author: User
You can change the SQL Server String to a table as needed. You can flexibly use the declare @ LSTR_InsertHeadvarchar (500) -- column name declare @ LSTR_InsertFieldsvarchar (max) -- data string declare @ splitrolvarchar) -- row delimiter declare @ splitcolvarchar (2) -- column separator set @ spli

You can change the SQL Server String to a table based on your needs. You can flexibly use the declare @ LSTR_InsertHead varchar (500) -- column name declare @ LSTR_InsertFields varchar (max) -- data string declare @ splitrol v) -- row delimiter declare @ splitcol varchar (2) -- column separator set @ spli

SQL server
String to a table
Can be changed and used flexibly as needed <无>
Declare @ LSTR_InsertHead varchar (500) -- column name declare @ comment varchar (max) -- data string declare @ splitrol varchar (2) -- line delimiter declare @ splitcol varchar (2) -- column delimiter set @ splitrol = 'delimiter' set @ splitcol = 'category' set @ LSTR_InsertHead = 'product delimiter unitid inclulevelid inclucost' -- header parameter set @ LSTR_InsertFields = '1996 bytes 8 then 9 then ¥0000000507 then 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' -- Data parameter set @ LSTR_InsertHead = @ LSTR_InsertHead + @ splitcoldeclare @ HeadStr varchar (20) -- column name declare @ HeadSql varchar (200) -- The column header generates set @ HeadSql = 'select' while CHARINDEX ('hour', @ LSTR_InsertHead) <> 0 beginSET @ HeadStr = substring (@ LSTR_InsertHead, 1, charindex (@ splitcol, @ LSTR_InsertHead)-1) -- intercept the header if @ HeadSql = 'select' set @ HeadSql = @ HeadSql + ''' as '+ @ HeadStrelseset @ HeadSql = @ HeadSql + ', '''as' + @ HeadStrSET @ LSTR_InsertHead = stuff (@ LSTR_InsertHead, 1, charindex (@ splitcol, @ LSTR_InsertHead ),'') -- remove the intercepted content "end/*" and "generate a table below */set @ HeadSql = @ HeadSql + 'where 1 = 2 union 'set @ LSTR_InsertFields = REPLACE (@ LSTR_InsertFields, @ splitrol, '''union select') set @ LSTR_InsertFields = @ HeadSql + 'select' + REPLACE (@ LSTR_InsertFields, @ splitcol ,''',''') + ''' print (@ LSTR_InsertFields) exec (@ LSTR_InsertFields)

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.