A crosstab. Use character Deming as the value: the original table data is:
Field 1 Field 2 Field 3
A1 B1 C1
A2 B2 C2
After transformation:
COL1 COL2 COL3
Field 1 A1 A2
Field 2 B1 B2
Field 3 C1 C2
CREATE TABLE tablename (Field 1 varchar (100), Field 2 varchar (100), Field 3 varchar (100))
Insert TableName Select ' Gsm900/1800mhz/gprs ', ' 85*44*21mm ', ' 80 '
UNION ALL SELECT ' Gsm900/1800mhz/gprs ', ' 82*46*21.5mm ', ' 79 '
Go
SELECT Top 0
Field name =a.name
INTO ABC
From Syscolumns A
INNER JOIN sysobjects D on a.id=d.id
Left join Sysproperties F on d.id=f.id and f.smallid=0
where d.xtype= ' U ' and d.name = ' tablename '
ORDER BY A.id,a.colorder
SELECT Top 0
Note =a.name
into ABCD
From Syscolumns A
INNER JOIN sysobjects D on a.id=d.id
Left join Sysproperties F on d.id=f.id and f.smallid=0
where d.xtype= ' U '--Represents the user type
and d.name = ' tablename '--tables that need to get fields
ORDER BY A.id,a.colorder
while (@count > 0)
Begin
Select @Col = ' Col ' + convert (varchar, @num)
EXEC (' ALTER TABLE ABC add [' + @Col + '] varchar NULL ')
Select @num = @num +1
Set @count = @count-1
End
DECLARE addnamecolumns_cursor insensitive Cursor--take field values
For
SELECT
Filedname = A.name
From Syscolumns A
INNER JOIN sysobjects D on a.id=d.id
Left join Sysproperties F on d.id=f.id and f.smallid=0
where d.xtype= ' U ' and d.name = ' tablename '
ORDER BY A.id,a.colorder
OPEN Addnamecolumns_cursor
BEGIN
FETCH NEXT from Addnamecolumns_cursor into @Name--corresponding field values
While @ @FETCH_STATUS = 0
BEGIN
INSERT INTO ABC (field name)
Select fname = @Name
Delete ABCD
EXEC (' INSERT into ABCD "SELECT name = [' + @Name + '] from TableName ')
Select @num = 1
DECLARE Addnamecolumns_cursor1 insensitive CURSOR
For the select note from ABCD
OPEN Addnamecolumns_cursor1
BEGIN
FETCH NEXT from Addnamecolumns_cursor1 into @Name1
While @ @FETCH_STATUS = 0
BEGIN
Select @num1 = CONVERT (varchar, @num)
EXEC (' Update ABC set [Col ' + @num1 + '] = ' + @Name1 + ' ' WHERE field name = ' + @Name + ' ')
print ' Update ABC set [' + @Col + '] = ' + @Name1 + ' ' WHERE field name = ' + @Name + ' '
Select @num = @num +1
FETCH NEXT from Addnamecolumns_cursor1 into @Name1
End
End
Close Addnamecolumns_cursor1
Deallocate Addnamecolumns_cursor1
FETCH NEXT from Addnamecolumns_cursor into @Name
End
End
Close Addnamecolumns_cursor
Deallocate addnamecolumns_cursor
SELECT * FROM ABC
SELECT * FROM TableName
DROP TABLE TableName
DROP TABLE ABC
drop table ABCD
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