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)