读书人

行转列加where 条件的有关问题

发布时间: 2013-07-01 12:33:04 作者: rapoo

行转列,加where 条件的问题
行转列,加where 条件的问题。
建表:


IF OBJECT_ID('[tb]') IS NOT NULL DROP TABLE [tb]
GO
create table tb(姓名 varchar(10) , 课程 varchar(10) , 分数 int)
insert into tb values('张三' , '语文' , 74)
insert into tb values('张三' , '数学' , 83)
insert into tb values('张三' , '物理' , 93)
insert into tb values('李四' , '语文' , 74)
insert into tb values('李四' , '数学' , 84)
insert into tb values('李四' , '物理' , 94)
go

where 课程 in('数学','物理') 是可以查的。

declare @where varchar(100)
declare @sql varchar(8000)
set @where='''数学'',''物理''';
set @sql = 'select 姓名 '
select @sql = @sql + ' , max(case 课程 when ''' + 课程 + ''' then 分数 else 0 end) [' + 课程 + ']'
from (select distinct 课程 from tb where 课程 in('数学','物理') ) as a
set @sql = @sql + ' from tb group by 姓名'
print @where
exec(@sql)

where 课程 in(@where) 这样就查不到了。。

declare @where varchar(100)
declare @sql varchar(8000)
set @where='''数学'',''物理''';
set @sql = 'select 姓名 '
select @sql = @sql + ' , max(case 课程 when ''' + 课程 + ''' then 分数 else 0 end) [' + 课程 + ']'
from (select distinct 课程 from tb where 课程 in(@where) ) as a
set @sql = @sql + ' from tb group by 姓名'
print @where
print @sql
exec(@sql)


请问,where 条件 不能是定值,只能是变量传入的,我该怎么写。


-_-
[解决办法]

IF OBJECT_ID('[tb]') IS NOT NULL DROP TABLE [tb]
GO
create table tb(姓名 varchar(10) , 课程 varchar(10) , 分数 int)
insert into tb values('张三' , '语文' , 74)
insert into tb values('张三' , '数学' , 83)
insert into tb values('张三' , '物理' , 93)
insert into tb values('李四' , '语文' , 74)
insert into tb values('李四' , '数学' , 84)
insert into tb values('李四' , '物理' , 94)
go

declare @where table(课程 varchar(100))
declare @sql varchar(8000)
insert @where select '数学' union all select '物理';
set @sql = 'select 姓名 '
select @sql = @sql + ' , max(case 课程 when ''' + 课程 + ''' then 分数 else 0 end) [' + 课程 + ']'


from (select distinct 课程 from tb where 课程 in(select * from @where) ) as a
set @sql = @sql + ' from tb group by 姓名'
print @sql
exec(@sql)


[解决办法]

IF OBJECT_ID('[tb]') IS NOT NULL DROP TABLE [tb]
GO
create table tb(姓名 varchar(10) , 课程 varchar(10) , 分数 int)
insert into tb values('张三' , '语文' , 74)
insert into tb values('张三' , '数学' , 83)
insert into tb values('张三' , '物理' , 93)
insert into tb values('李四' , '语文' , 74)
insert into tb values('李四' , '数学' , 84)
insert into tb values('李四' , '物理' , 94)
go
declare @where varchar(100)
declare @sql varchar(8000)
set @where='''数学'',''物理''';
set @sql = 'select 姓名 '
select @sql = @sql + ' , max(case 课程 when ''' + 课程 + ''' then 分数 else 0 end) [' + 课程 + ']'
from (select distinct 课程 from tb where 课程 in(''+@where+'') ) as a
set @sql = @sql + ' from tb group by 姓名'
print @where
print @sql
exec(@sql)

[解决办法]
可以直接把"数学"和"物理"当成一个字符串呀.'数学,物理'.用逗号分隔一下.
创建一个自定义表值函数.其参数有两个,第一个是字符串.第二是分隔符.返回一个表.



[解决办法]
if exists (select 1
from sysobjects
where id = object_id('_Split')
and type = 'TF')
drop function dbo._Split
go
create FUNCTION _Split(@Text NVARCHAR(max),@Separator varchar(100))
RETURNS @tempTable TABLE(ID int identity,Value NVARCHAR(max))
AS
BEGIN
DECLARE @StartIndex INT --开始查找的位置
DECLARE @FindIndex INT --找到的位置
DECLARE @Content VARCHAR(max) --找到的值
--初始化一些变量
SET @StartIndex = 1 --T-SQL中字符串的查找位置是从开始的
SET @FindIndex=0

--开始循环查找字符串逗号
WHILE(@StartIndex <= LEN(@Text))
BEGIN
--查找字符串函数CHARINDEX 第一个参数是要找的字符串
-- 第二个参数是在哪里查找这个字符串
-- 第三个参数是开始查找的位置


--返回值是找到字符串的位置
SELECT @FindIndex = CHARINDEX(@Separator,@Text,@StartIndex)
--判断有没找到没找到返回
IF(@FindIndex =0 OR @FindIndex IS NULL)
BEGIN
--如果没有找到就表示找完了
SET @FindIndex = LEN(@Text)+len(@Separator)
END
--截取字符串函数SUBSTRING 第一个参数是要截取的字符串
-- 第二个参数是开始的位置
-- 第三个参数是截取的长度
SET @Content =SUBSTRING(@Text,@StartIndex,@FindIndex-@StartIndex)
--初始化下次查找的位置
SET @StartIndex = @FindIndex+len(@Separator)
--把找的的值插入到要返回的Table类型中
INSERT INTO @tempTable (Value) VALUES (@Content)
END
RETURN
END
go

DROP PROCEDURE set_课程
Go
CREATE PROCEDURE set_课程
@KC varchar(100)

AS
IF OBJECT_ID('temp_课程') IS NOT NULL DROP TABLE temp_课程;
create table temp_课程(课程 varchar(10));

insert into temp_课程 select distinct 课程 from tb where 课程 in (select value from dbo._split(@KC,','))
go

exec set_课程 '语文,物理'

读书人网 >SQL Server

热点推荐