交叉表的实殃及向SQL SERVER数据库中插入数据时,出现乱码或???(问号)的解决方法。
2008-12-22 17:20
651 查看
再向SQL SERVER数据库中插入数据时,出现乱码或???(问号)的解决方法。
ALTER DATABASE YourDataBaseName COLLATE Chinese_PRC_CI_AS
交叉表实例(转载)
1、 建表: 在查询分析器里运行:
CREATE TABLE [Test] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[name] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[subject] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[Source] [numeric](18, 0) NULL
) ON [PRIMARY]
GO
INSERT INTO [test] ([name],[subject],[Source]) values (N'张三',N'语文',60)
INSERT INTO [test] ([name],[subject],[Source]) values (N'李四',N'数学',70)
INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'英语',80)
INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'数学',75)
INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'语文',57)
INSERT INTO [test] ([name],[subject],[Source]) values (N'李四',N'语文',80)
INSERT INTO [test] ([name],[subject],[Source]) values (N'张三',N'英语',100)
Go
2、交叉表语句的实现: 用于:交叉表的列数是确定的
select name,sum(case subject when '数学' then source else 0 end) as '数学',
sum(case subject when '英语' then source else 0 end) as '英语',
sum(case subject when '语文' then source else 0 end) as '语文'
from test
group by name
--用于:交叉表的列数是不确定的
declare @sql varchar(8000)
set @sql = 'select name,'
select @sql = @sql + 'sum(case subject when '''+subject+'''
then source else 0 end) as '''+subject+''','
from (select distinct subject from test) as a
select @sql = left(@sql,len(@sql)-1) + ' from test group by name'
exec(@sql)
go
ALTER DATABASE YourDataBaseName COLLATE Chinese_PRC_CI_AS
交叉表实例(转载)
1、 建表: 在查询分析器里运行:
CREATE TABLE [Test] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[name] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[subject] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[Source] [numeric](18, 0) NULL
) ON [PRIMARY]
GO
INSERT INTO [test] ([name],[subject],[Source]) values (N'张三',N'语文',60)
INSERT INTO [test] ([name],[subject],[Source]) values (N'李四',N'数学',70)
INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'英语',80)
INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'数学',75)
INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'语文',57)
INSERT INTO [test] ([name],[subject],[Source]) values (N'李四',N'语文',80)
INSERT INTO [test] ([name],[subject],[Source]) values (N'张三',N'英语',100)
Go
2、交叉表语句的实现: 用于:交叉表的列数是确定的
select name,sum(case subject when '数学' then source else 0 end) as '数学',
sum(case subject when '英语' then source else 0 end) as '英语',
sum(case subject when '语文' then source else 0 end) as '语文'
from test
group by name
--用于:交叉表的列数是不确定的
declare @sql varchar(8000)
set @sql = 'select name,'
select @sql = @sql + 'sum(case subject when '''+subject+'''
then source else 0 end) as '''+subject+''','
from (select distinct subject from test) as a
select @sql = left(@sql,len(@sql)-1) + ' from test group by name'
exec(@sql)
go
相关文章推荐
- 在SQLServer2005中使用SQL语句插入数据出现乱码或问号的解决方法
- JSP里面向数据库里面插入数据时出现乱码的解决方法
- MySQL数据库 查询表数据出现中文乱码 及 插入中文报错Incorrect string value问题解决方法
- JSP MySQL插入数据时出现中文乱码问题的解决方法
- eclipse插入数据到MySQL数据库时,出现中文乱码问题的解决方法
- PHP插入数据到Mysql出现乱码的解决方法
- JSP MySQL插入数据时出现中文乱码问题的解决方法
- mysql插入表中的中文显示为乱码或问号的解决方法
- 在SQL 2005中用T-SQL插入中文数据时出现的问号或乱码的解决方案!!!!
- MySql中表单输入数据出现中文乱码的解决方法
- MySql中表单输入数据出现中文乱码的解决方法
- 在使用SQLite插入数据时出现乱码的解决办法
- PHP——向数据库插入数据时乱码的解决方法
- mysql插入大量数据(text类型)时出现MySQL Server has gone away 的解决方法
- 解决jsp插入数据库中的数据出现乱码问题
- php读取mysql中文数据出现乱码的解决方法
- Java读取UTF-8格式文件第一行出现乱码——问号“?”及解决 And Java读带有BOM的UTF-8文件乱码原因及解决方法
- 在SQL Server 2005(2008)中用T-SQL插入中文数据时出现的问号或乱码的解决方案
- C# mysql 插入数据,中文乱码的解决方法
- c#操作MySQL数据库中文出现乱码(很多问号)的解决方法