--建立测试环境
Create Table 表(ColID varchar(10),Col1 varchar(10),Col2 varchar(10),Col3 varchar(10))
--插入数据
insert into 表
select '1','abc','1','2.3' union
select '2','other','30','45.12'
--select * from 表
--测试语句
declare @s varchar(8000)
set @s='select * from(select null as ColID,null as 列'
select @s=@s+' union select ColID,'+ name+' from 表'
from syscolumns where id=object_id('表') and name<>'ColID'
set @s=@s+')a where colid is not null'exec(@s)
--删除测试环境
Drop Table 表/*
ColID 列
1 1
1 2.3
1 abc
2 30
2 45.12
2 other
*/
Create Table 表(ColID varchar(10),Col1 varchar(10),Col2 varchar(10),Col3 varchar(10))
--插入数据
insert into 表
select '1','abc','1','2.3' union
select '2','other','30','45.12'
--select * from 表
--测试语句
declare @s varchar(8000)
set @s='select * from(select null as ColID,null as 列'
select @s=@s+' union select ColID,'+ name+' from 表'
from syscolumns where id=object_id('表') and name<>'ColID'
set @s=@s+')a where colid is not null'exec(@s)
--删除测试环境
Drop Table 表/*
ColID 列
1 1
1 2.3
1 abc
2 30
2 45.12
2 other
*/
解决方案 »
- 效率与效果之与OA的重要性
- 数据库操作
- 兄弟指导一下2T的数据库迁移风险评估,谢谢
- 搞死人的错误:列名无效,明明都在啊!
- 请高手解决一下有关Access2000数据转换到SQLSERVER2000U问题
- 为什么有时运行挺快,有时运行很慢?
- 怎样使grid中的指针在视图刷新后仍然指向这个新记录呢?
- VFP中,工具栏中的事件程序访问所属的formset中的OLEBoundcontrol失败!
- 三维表怎么建?要存储每个人领每种物品的时间,并要求人和物品都是可变的!
- 关于根据数据统计上班天数
- 导出数据时提示:[DBNETLIB][ConectionOpen (connect()).]SQL Server不存在或拒绝访问,这该怎么办呢?
- 请教一下数据库运作的原理
表名: Table 列名: Col1,Col2, Col3 保存在另一表中。
Create Table tb(ColID int,Col1 varchar(10),Col2 varchar(10),Col3 varchar(10))
insert tb select 1,'abc' ,'1' ,'2.3'
union all select 2,'other','30','45.12'
go--查询的存储过程
create proc p_qry
@tbname sysname, --查询那个表中的数据
@ColID int --ColID列值
as
declare @s nvarchar(4000)
set @s=''
select @s=@s+N' union all select '+quotename(name,N'''')
+N','+quotename(name)
+N' from '+quotename(@tbname)
+N' where ColID=@ColID'
from syscolumns
where name<>'ColID' and id=object_id(@tbname)
order by colid
set @s=N'select fdname='+stuff(@s,1,18,N'')
exec sp_executesql @s,N'@ColID int',@ColID
go--调用
exec p_qry 'tb',2
go
--删除测试
drop table tb
drop proc p_qry/*--测试结果fdname Col1
------ ----------
Col1 other
Col2 30
Col3 45.12(所影响的行数为 3 行)
--*/
Create Table tb(ColID int,Col1 varchar(10),Col2 int,Col3 decimal(10,2))
insert tb select 1,'abc' ,1 ,2.30
union all select 2,'other',30,45.12
go--查询的存储过程
create proc p_qry
@tbname sysname, --查询那个表中的数据
@ColID int, --ColID列值
@ColID_Fd sysname=N'ColID' --ColID列名
as
declare @s nvarchar(4000)
set @s=''
select @s=@s+N' union all select '+quotename(name,N'''')
+N',Value=cast('+quotename(name)+N' as varchar)'
+N' from '+quotename(@tbname)
+N' where '+quotename(@ColID_Fd)+N'=@ColID'
from syscolumns
where name<>@ColID_Fd and id=object_id(@tbname)
order by colid
set @s=N'select fdname='+stuff(@s,1,18,N'')
exec sp_executesql @s,N'@ColID int',@ColID
go--调用
exec p_qry 'tb',2
exec p_qry 'sysobjects',2,N'id'
go
--删除测试
drop table tb
drop proc p_qry/*--测试结果fdname Value
------ ------------------------------
Col1 other
Col2 30
Col3 45.12(所影响的行数为 3 行)fdname Value
---------------- ------------------------------
name sysindexes
xtype S
uid 1
info 29
status -536870907
base_schema_ver 32
replinfo 0
parent_obj 0
crdate 08 6 2000 1:29AM
ftcatid 0
schema_ver 32
stats_schema_ver 0
type S
userstat 1
sysstat 8273
indexdel 0
refdate 08 6 2000 1:29AM
version 0
deltrig 0
instrig 0
updtrig 0
seltrig 0
category 2
cache 0(所影响的行数为 24 行)
--*/