请大家给一个表结构建立的思路!麻烦各位了,谢谢(100分)

  • 主题发起人 主题发起人 新来的菜鸟
  • 开始时间 开始时间

新来的菜鸟

Unregistered / Unconfirmed
GUEST, unregistred user!
我需要建立一个成绩表,但里面的课程是不固定的,(也就是说不能用一张表来实现,因为总不能临时增加字段吧)<br><br>然后用户会以Excel表以下的格式将成绩导入进表里,然后我还必须已以下的形式在DBgrind里列出来!我该如何来建立这个表结构呢?麻烦各位给些建议,谢谢!另外有那位大哥有Excel导入sql server 的例子麻烦发一份给小弟,万分感谢!Email:yangzongling18@163.com<br><br>班级 学号 姓名 语文 数学 英语 物理 总分<br>1 20080101 韦娴 101.5 101 &nbsp; 102.5 75 537.5<br>2 20080202 陈捷 109 85 110.5 71 79 &nbsp; &nbsp; &nbsp;518.5<br>3 20080303 苏婷 100.5 58 &nbsp; 93.5 &nbsp; &nbsp;37 411
 
一张表A存学号、姓名和班级,另一张表B放学号和科目,以学号来关联<br>用嵌套SQL语句来实现上面的效果
 
呵,关键是成绩该用什么样的表结构来存储,因为科目是不固定的
 
A表<br>字段: 学号,姓名,班级<br>B表<br>字段:学号,科目,成绩<br><br>就这样吗?嵌套一下SQL查询一下就行,一会我帮你试一下,很简单的
 
A表<br>字段: 学号,姓名,班级<br>B表<br>字段:学号,科目,成绩<br>select a.學號,a.姓名,case b.科目 when '語文' then b.成績,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;when '數學' then b.成績,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;when '英語' then b.成績,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;when '物理' then b.成績<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end<br>from A a,B b<br>where a.學號=b.學號<br>------------------總分再合計就可以了。。。 亂寫的。。。呵呵。。
 
学生信息建一个表:学号,姓名,班级。。。。。<br>成绩建一个表:学号,考试日期,学科,成绩<br><br><br>SQL语句导入导出大全 <br><br>/******* &nbsp;导出到excel<br>EXEC master..xp_cmdshell 'bcp SettleDB.dbo.shanghu out c:/temp1.xls -c -q -S"GNETDATA/GNETDATA" -U"sa" -P""'<br><br>/*********** &nbsp;导入Excel<br>SELECT * <br>FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',<br>&nbsp; 'Data Source="c:/test.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...xactions<br><br>/*动态文件名<br>declare @fn varchar(20),@s varchar(1000)<br>set @fn = 'c:/test.xls'<br>set @s ='''Microsoft.Jet.OLEDB.4.0'',<br>''Data Source="'+@fn+'";User ID=Admin;Password=;Extended properties=Excel 5.0'''<br>set @s = 'SELECT * FROM OpenDataSource ('+@s+')...sheet1$'<br>exec(@s)<br>*/<br><br>SELECT cast(cast(科目编号 as numeric(10,2)) as nvarchar(255))+' ' 转换后的别名<br>FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',<br>&nbsp; 'Data Source="c:/test.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...xactions<br><br>/********************** EXCEL导到远程SQL<br>insert OPENDATASOURCE(<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'SQLOLEDB',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Data Source=远程ip;User ID=sa;Password=密码'<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;).库名.dbo.表名 (列名1,列名2)<br>SELECT 列名1,列名2<br>FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',<br>&nbsp; 'Data Source="c:/test.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...xactions<br><br><br>/** 导入文本文件<br>EXEC master..xp_cmdshell 'bcp dbname..tablename in c:/DT.txt -c -Sservername -Usa -Ppassword'<br><br>/** 导出文本文件<br>EXEC master..xp_cmdshell 'bcp dbname..tablename out c:/DT.txt -c -Sservername -Usa -Ppassword'<br>或<br>EXEC master..xp_cmdshell 'bcp "Select * from dbname..tablename" queryout c:/DT.txt -c -Sservername -Usa -Ppassword'<br><br>导出到TXT文本,用逗号分开<br>exec master..xp_cmdshell 'bcp "库名..表名" out "d:/tt.txt" -c -t ,-U sa -P password'<br><br>BULK INSERT 库名..表名<br>FROM 'c:/test.txt'<br>WITH (<br>&nbsp; &nbsp; FIELDTERMINATOR = ';',<br>&nbsp; &nbsp; ROWTERMINATOR = '/n'<br>)<br>--/* dBase IV文件<br>select * from <br>OPENROWSET('MICROSOFT.JET.OLEDB.4.0'<br>,'dBase IV;HDR=NO;IMEX=2;DATABASE=C:/','select * from [客户资料4.dbf]')<br>--*/<br><br>--/* dBase III文件<br>select * from <br>OPENROWSET('MICROSOFT.JET.OLEDB.4.0'<br>,'dBase III;HDR=NO;IMEX=2;DATABASE=C:/','select * from [客户资料3.dbf]')<br>--*/<br><br>--/* FoxPro 数据库<br>select * from openrowset('MSDASQL',<br>'Driver=Microsoft Visual FoxPro Driver;SourceType=DBF;SourceDB=c:/',<br>'select * from [aa.DBF]')<br>--*/<br><br>/**************导入DBF文件****************/<br>select * from openrowset('MSDASQL',<br>'Driver=Microsoft Visual FoxPro Driver;<br>SourceDB=e:/VFP98/data;<br>SourceType=DBF',<br>'select * from customer where country != "USA" order by country')<br>go<br>/***************** 导出到DBF ***************/<br>如果要导出数据到已经生成结构(即现存的)FOXPRO表中,可以直接用下面的SQL语句<br><br>insert into openrowset('MSDASQL',<br>'Driver=Microsoft Visual FoxPro Driver;SourceType=DBF;SourceDB=c:/',<br>'select * from [aa.DBF]')<br>select * from 表<br><br>说明:<br>SourceDB=c:/ &nbsp;指定foxpro表所在的文件夹<br>aa.DBF &nbsp; &nbsp; &nbsp; &nbsp;指定foxpro表的文件名.<br><br>/*************导出到Access********************/<br>insert into openrowset('Microsoft.Jet.OLEDB.4.0', <br>&nbsp; &nbsp;'x:/A.mdb';'admin';'',A表) select * from 数据库名..B表<br><br>/*************导入Access********************/<br>insert into B表 selet * from openrowset('Microsoft.Jet.OLEDB.4.0', <br>&nbsp; &nbsp;'x:/A.mdb';'admin';'',A表)<br><br>文件名为参数<br>declare @fname varchar(20)<br>set @fname = 'd:/test.mdb'<br>exec('SELECT a.* FROM opendatasource(''Microsoft.Jet.OLEDB.4.0'',<br>&nbsp; &nbsp; '''+@fname+''';''admin'';'''', topics) as a ')<br><br>SELECT * <br>FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',<br>&nbsp; 'Data Source="f:/northwind.mdb";Jet OLEDB:Database Password=123;User ID=Admin;Password=;')...产品<br><br>********************* &nbsp;导入 xml 文件<br><br>DECLARE @idoc int<br>DECLARE @doc varchar(1000)<br>--sample XML document.BR&gt; SET @doc ='<br>&lt;root&gt;<br>&nbsp; &lt;Customer cid= "C1" name="Janine" city="Issaquah"&gt;<br>&nbsp; &nbsp; &nbsp; &lt;Order oid="O1" date="1/20/1996" amount="3.5" /&gt;<br>&nbsp; &nbsp; &nbsp; &lt;Order oid="O2" date="4/30/1997" amount="13.4"&gt;Customer was very satisfied<br>&nbsp; &nbsp; &nbsp; &lt;/Order&gt;<br>&nbsp; &nbsp;&lt;/Customer&gt;<br>&nbsp; &nbsp;&lt;Customer cid="C2" name="Ursula" city="Oelde" &gt;<br>&nbsp; &nbsp; &nbsp; &lt;Order oid="O3" date="7/14/1999" amount="100" note="Wrap it blue <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;white red"&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;Urgency&gt;Important&lt;/Urgency&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Happy Customer.<br>&nbsp; &nbsp; &nbsp; &lt;/Order&gt;<br>&nbsp; &nbsp; &nbsp; &lt;Order oid="O4" date="1/20/1996" amount="10000"/&gt;<br>&nbsp; &nbsp;&lt;/Customer&gt;<br>&lt;/root&gt;<br>'<br>-- Create an internal representation of the XML document.<br>EXEC sp_xml_preparedocument.nbsp@idoc OUTPUT, @doc<br><br>-- Execute a SELECT statement using OPENXML rowset provider.<br>SELECT *<br>FROM OPENXML (@idoc, '/root/Customer/Order', 1)<br>&nbsp; &nbsp; &nbsp; WITH (oid &nbsp; &nbsp; char(5), <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; amount &nbsp;float, <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; comment ntext 'text()')<br>EXEC sp_xml_removedocument.nbsp@idoc<br><br>&nbsp;<br><br>???????<br><br>/**********************Excel导到Txt****************************************/<br>想用<br>select * into opendatasource(...) from opendatasource(...)<br>实现将一个Excel文件内容导入到一个文本文件<br><br>假设Excel中有两列,第一列为姓名,第二列为很行帐号(16位)<br>且银行帐号导出到文本文件后分两部分,前8位和后8位分开。<br><br><br>邹健:<br>如果要用你上面的语句插入的话,文本文件必须存在,而且有一行:姓名,银行账号1,银行账号2<br>然后就可以用下面的语句进行插入<br>注意文件名和目录根据你的实际情况进行修改.<br><br>insert into<br>opendatasource('MICROSOFT.JET.OLEDB.4.0'<br>,'Text;HDR=Yes;DATABASE=C:/'<br>)...[aa#txt]<br>--,aa#txt)<br>--*/<br>select 姓名,银行账号1=left(银行账号,8),银行账号2=right(银行账号,8) <br>from <br>opendatasource('MICROSOFT.JET.OLEDB.4.0'<br>,'Excel 5.0;HDR=YES;IMEX=2;DATABASE=c:/a.xls'<br>--,Sheet1$)<br>)...[Sheet1$]<br>如果你想直接插入并生成文本文件,就要用bcp<br>declare @sql varchar(8000),@tbname varchar(50)<br><br>--首先将excel表内容导入到一个全局临时表<br>select @tbname='[##temp'+cast(newid() as varchar(40))+']'<br>&nbsp;,@sql='select 姓名,银行账号1=left(银行账号,8),银行账号2=right(银行账号,8) <br>&nbsp;into '+@tbname+' from <br>opendatasource(''MICROSOFT.JET.OLEDB.4.0''<br>,''Excel 5.0;HDR=YES;IMEX=2;DATABASE=c:/a.xls''<br>)...[Sheet1$]'<br>exec(@sql)<br><br>--然后用bcp从全局临时表导出到文本文件<br>set @sql='bcp "'+@tbname+'" out "c:/aa.txt" /S"(local)" /P"" /c'<br>exec master..xp_cmdshell @sql<br><br>--删除临时表<br>exec('drop table '+@tbname)<br>/********************导整个数据库*********************************************/<br>用bcp实现的存储过程<br>/*<br>&nbsp;实现数据导入/导出的存储过程<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;根据不同的参数,可以实现导入/导出整个数据库/单个表<br>&nbsp;调用示例:<br>--导出调用示例<br>----导出单个表<br>exec file2table 'zj','','','xzkh_sa..地区资料','c:/zj.txt',1<br>----导出整个数据库<br>exec file2table 'zj','','','xzkh_sa','C:/docman',1<br><br>--导入调用示例<br>----导入单个表<br>exec file2table 'zj','','','xzkh_sa..地区资料','c:/zj.txt',0<br>----导入整个数据库<br>exec file2table 'zj','','','xzkh_sa','C:/docman',0<br><br>*/<br>if exists(select 1 from sysobjects where name='File2Table' and objectproperty(id,'IsProcedure')=1)<br>&nbsp;drop procedure File2Table<br>go<br>create procedure File2Table<br>@servername varchar(200) &nbsp;--服务器名<br>,@username varchar(200) &nbsp; --用户名,如果用NT验证方式,则为空''<br>,@password varchar(200) &nbsp; --密码<br>,@tbname varchar(500) &nbsp; --数据库.dbo.表名,如果不指定:.dbo.表名,则导出数据库的所有用户表<br>,@filename varchar(1000) &nbsp;--导入/导出路径/文件名,如果@tbname参数指明是导出整个数据库,则这个参数是文件存放路径,文件名自动用表名.txt<br>,@isout bit &nbsp; &nbsp; &nbsp;--1为导出,0为导入<br>as<br>declare @sql varchar(8000)<br><br>if @tbname like '%.%.%' --如果指定了表名,则直接导出单个表<br>begin<br>&nbsp;set @sql='bcp '+@tbname<br>&nbsp; +case when @isout=1 then ' out ' else ' in ' end<br>&nbsp; +' "'+@filename+'" /w'<br>&nbsp; +' /S '+@servername<br>&nbsp; +case when isnull(@username,'')='' then '' else ' /U '+@username end<br>&nbsp; +' /P '+isnull(@password,'')<br>&nbsp;exec master..xp_cmdshell @sql<br>end<br>else<br>begin --导出整个数据库,定义游标,取出所有的用户表<br>&nbsp;declare @m_tbname varchar(250)<br>&nbsp;if right(@filename,1)&lt;&gt;'/' set @filename=@filename+'/'<br><br>&nbsp;set @m_tbname='declare #tb cursor for select name from '+@tbname+'..sysobjects where xtype=''U'''<br>&nbsp;exec(@m_tbname)<br>&nbsp;open #tb<br>&nbsp;fetch next from #tb into @m_tbname<br>&nbsp;while @@fetch_status=0<br>&nbsp;begin<br>&nbsp; set @sql='bcp '+@tbname+'..'+@m_tbname<br>&nbsp; &nbsp;+case when @isout=1 then ' out ' else ' in ' end<br>&nbsp; &nbsp;+' "'+@filename+@m_tbname+'.txt " /w'<br>&nbsp; &nbsp;+' /S '+@servername<br>&nbsp; &nbsp;+case when isnull(@username,'')='' then '' else ' /U '+@username end<br>&nbsp; &nbsp;+' /P '+isnull(@password,'')<br>&nbsp; exec master..xp_cmdshell @sql<br>&nbsp; fetch next from #tb into @m_tbname<br>&nbsp;end<br>&nbsp;close #tb<br>&nbsp;deallocate #tb <br>end<br>go<br>/************* Oracle **************/<br>EXEC sp_addlinkedserver 'OracleSvr', <br>&nbsp; &nbsp;'Oracle 7.3', <br>&nbsp; &nbsp;'MSDAORA', <br>&nbsp; &nbsp;'ORCLDB'<br>GO<br>delete from openquery(mailser,'select * &nbsp;from yulin')<br>select * &nbsp;from openquery(mailser,'select * &nbsp;from yulin')<br>update openquery(mailser,'select * from &nbsp;yulin where id=15')set disorder=555,catago=888<br>insert into openquery(mailser,'select disorder,catago from &nbsp;yulin')values(333,777)<br>补充:<br>对于用bcp导出,是没有字段名的.<br>用openrowset导出,需要事先建好表.<br>用openrowset导入,除ACCESS及EXCEL外,均不支持非本机数据导入
 
不用这么麻烦,就我先前说的我试了一下,直接用SQL语句一句嵌套好象不行,我给你想个简单办法,查询前先动态赋SQL语句值<br>表结构如下:<br>学生表:学号,班级,姓名<br>成绩表:学号,科目,成绩<br>{ SELECT A.学号,A.班级,A.姓名,<br>&nbsp; &nbsp;(SELECT 成绩 FROM 成绩表 WHERE 学号=A.学号 AND 科目='物理') AS '物理',<br>&nbsp; &nbsp;(SELECT 成绩 FROM 成绩表 WHERE 学号=A.学号 AND 科目='英语') AS '英语',<br>&nbsp; &nbsp;(select count(成绩) FROM 成绩表 WHERE &nbsp;学号=A.学号) AS '总分' <br>&nbsp; FROM 学生表 A;<br>} //如果科目固定其实可以往里嵌套<br><br>VAR TMP:STRING;<br><br>WITH QRY1 DO //从成绩表中取不重复科目列值<br>BEGIN<br>&nbsp; &nbsp; CLOSE;<br>&nbsp; &nbsp; SQL.TEXT:='SELECT DISTINCT(科目) FROM 成绩表';<br>&nbsp; &nbsp; OPEN;<br>&nbsp; &nbsp; FIST;<br>END;<br><br>TMP:='SELECT A.学号,A.班级,A.姓名';<br>WHILE NOT QRY1.EOF DO<br>&nbsp;BEGIN<br>&nbsp;TMP: =TMP+<br>&nbsp; &nbsp; &nbsp; ',(SELECT 成绩 FROM 成绩表 WHERE 学号=A.学号 AND 科目='+<br>&nbsp; &nbsp; &nbsp; QUOTEDSTR(QRY1.FIELDS[0].ASSTRING)+' AS &nbsp;'+<br>&nbsp; &nbsp; &nbsp; QRY1.FIELDS[0].ASSTRING;<br>&nbsp;QRY1.NEXT;<br>&nbsp;END;<br><br>TMP:=TMP+',select count(成绩) FROM 成绩表 WHERE &nbsp;学号=A.学号) AS '总分' &nbsp;FROM 学生表 A';<br><br>WITH QRY2 DO //打开行列互换查询<br>BEGIN<br>&nbsp; &nbsp;CLOSE;<br>&nbsp; &nbsp;SQL.TEXT:=TMP;<br>&nbsp; &nbsp;OPEN;<br>END;<br><br>OK,没问题了,顺利解决,就是行列互换问题,科目表最好做个数据字典,别让乱添,要不可要出乱子的.
 
科目信息单独用一张表,让客户自己维护。
 
没问题可以结帐了
 
多人接受答案了。
 
后退
顶部