新年快乐,有条SQL语句想请教高手一下:
数据库有两张表,
表一(ptree)类别表:
id name
10 软件
11 硬件表二(products)产品表,数据有500条,暂略
id ptreeid name ptype color
1 10 A001 L-01 白
2 10 A002 L-02 红
3 10 A003 L-03 黑
表三(products_attribute)产品属性表:
id ptreeid name
1 10 ptype
2 10 color
现在需要把这些表二(products)的一些数据转换到另一张表(tb),变成如下数据:
product_id attribute_id product_content
1 1 L-01
1 2 白
2 1 L-02
2 2 红
3 1 L-03
3 2 黑
生产新表的规则如下:
product_id就是表(products)的id,attribute_id就是表(products_attribute)中的id,product_content就是
表(products)中的内容.
在查询器里写了以下语句,但是提示:
将 varchar 值 'L-01' 转换为数据类型为 int 的列时发生语法错误,要怎么修改才好,谢谢了。
select * into tb from (
select id as product_id, '1' as attribute_id,ptype as product_content from products
union
select id as product_id, '2' as attribute_id,color as product_content from products
) tb
order by product_id
数据库有两张表,
表一(ptree)类别表:
id name
10 软件
11 硬件表二(products)产品表,数据有500条,暂略
id ptreeid name ptype color
1 10 A001 L-01 白
2 10 A002 L-02 红
3 10 A003 L-03 黑
表三(products_attribute)产品属性表:
id ptreeid name
1 10 ptype
2 10 color
现在需要把这些表二(products)的一些数据转换到另一张表(tb),变成如下数据:
product_id attribute_id product_content
1 1 L-01
1 2 白
2 1 L-02
2 2 红
3 1 L-03
3 2 黑
生产新表的规则如下:
product_id就是表(products)的id,attribute_id就是表(products_attribute)中的id,product_content就是
表(products)中的内容.
在查询器里写了以下语句,但是提示:
将 varchar 值 'L-01' 转换为数据类型为 int 的列时发生语法错误,要怎么修改才好,谢谢了。
select * into tb from (
select id as product_id, '1' as attribute_id,ptype as product_content from products
union
select id as product_id, '2' as attribute_id,color as product_content from products
) tb
order by product_id
有错误的,生成的新表product_content字段是"int"类型的,存储不了“'L-01'”,有没有办法,
让生成的新表product_content字段变为"nvarchar"类型的。
谢谢了。。
如果有int 可能是你原有数据第一条数据是int类型的
可将查询改动为
select * into tb from (
select id as product_id, '1' as attribute_id,CAST(ptype AS VARCAHR(20)) as product_content from products
union
select id as product_id, '2' as attribute_id,CAST(color AS VARCHAR(20)) as product_content from products
) tb
order by product_id
/*创建表*/
CREATE TABLE [products]([id] int,[ptreeid] int,[name] char(4),[ptype] char(4),[color] nchar(1))
CREATE TABLE [products_attribute]([id] int,[ptreeid] int,[name] varchar(10))
GO
/*插入一些数据*/
INSERT INTO [products]
SELECT 1,10,'A001','L-01',N'白' UNION ALL
SELECT 2,10,'A002','L-02',N'红' UNION ALL
SELECT 3,10,'A003','L-03',N'黑'INSERT INTO [products_attribute]
SELECT 1,10,'ptype' UNION ALL
SELECT 2,10,'color'
GO
/*原表内容*/
SELECT * FROM [products]
SELECT * FROM [products_attribute]
GO
--创建新表
CREATE TABLE [tb]
(
[product_id] INT,
[attribute_id] INT,
[product_content] VARCHAR(20)
)
GO
--执行数据转换
DECLARE @sql VARCHAR(200)
DECLARE select_cursor CURSOR FOR
SELECT 'INSERT INTO [tb] SELECT '+CAST([products].[id] AS VARCHAR(10))+','+CAST([products_attribute].[id] AS VARCHAR(10))+
',(SELECT ['+[products_attribute].[name]+'] FROM [products] WHERE [id]='''+CAST([products].[id] AS VARCHAR(10))+''')'
FROM [products]
cross join [products_attribute]
JOIN [syscolumns] ON [syscolumns].[name]=[products_attribute].[name]
WHERE OBJECT_NAME([syscolumns].[id])='products' AND [syscolumns].[name]<>'id'
ORDER BY [products].[id],[products_attribute].[id]
FOR READ ONLY --Creat a cursor
OPEN select_cursor
WHILE(0=0)
BEGIN
FETCH NEXT FROM select_cursor
INTO @sql
IF(@@FETCH_STATUS<>0) BREAK
EXEC(@sql)
END
CLOSE select_cursor
DEALLOCATE select_cursor
--新表的数据
SELECT * FROM [tb]GO
/*删除*/
DROP TABLE [tb]
DROP TABLE [products]
DROP TABLE [products_attribute]/*
(所影响的行数为 3 行)
(所影响的行数为 2 行)id ptreeid name ptype color
----------- ----------- ---- ----- -----
1 10 A001 L-01 白
2 10 A002 L-02 红
3 10 A003 L-03 黑(所影响的行数为 3 行)id ptreeid name
----------- ----------- ----------
1 10 ptype
2 10 color(所影响的行数为 2 行)
(所影响的行数为 1 行)
(所影响的行数为 1 行)
(所影响的行数为 1 行)
(所影响的行数为 1 行)
(所影响的行数为 1 行)
(所影响的行数为 1 行)product_id attribute_id product_content
----------- ------------ --------------------
1 1 L-01
1 2 白
2 1 L-02
2 2 红
3 1 L-03
3 2 黑(所影响的行数为 6 行)
*/
搞定了,按照你那个cast就可以了.
谢谢了.