使用 MS SQL,我需要选择一个表,同时将整个字符串负载转换为整数!可以吗?
基本上,我是新IT类型的人,老家伙给我留下了一堆烂摊子!我们有一个 MS-Access 数据库,用于存储在线调查问卷的答案,这个特定的数据库有大约 45,000 条记录,每个调查问卷有 220 个问题。老家伙凭他的智慧决定将问卷问题的答案存储为文本,即使答案是0-5整数!
不管怎样,我们现在需要向调查问卷添加大量新问题,使其多达 240 个问题。 255 个字段的访问限制和 30 列传记数据也存储在该数据库中,这意味着我需要拆分数据库。
因此,我已经成功地将所有生物信息非常愉快地放入一个新表中:
SELECT id,[all bio column names] INTO resultsBioData FROM results;
这并没有造成太大问题,因为我没有转换任何内容,但对于问题数据,我想将其全部转换为整数,位于我的那一刻:
SELECT id,CInt(q1) AS nq1.......CInt(q220) AS nq220 INTO resultsItemData FROM results;
这似乎对大约 400 条记录工作正常,但随后就停止了,我想这可能是因为它遇到了无法转换为整数的东西,所以我编写了一个小java程序,删除了其中的任何记录220 个答案中的任何一个都不是0,1,2,3,4 或 5,它仍然放弃大约 400(尽管从来没有相同的记录!)
有人有任何想法吗?我目前正在我的测试系统上执行此操作,并且在对我们的实时系统执行此操作之前,我真的希望有一些强大的功能!
抱歉问了这么长的问题,但这让我很头疼!
Basically, I am the new IT type guy, old guy left a right mess for me! We have a MS-Access DB which stores the answers to an online questionnaire, this particular DB has about 45,000 records and each questionnaire has 220 questions. The old guy, in his wisdom decided to store the answers to the questionnaire questions as text even though the answers are 0-5 integers!
Anyway, we now need to add a load of new questions to the questionnaire taking it upto 240 questions. The 255 field limit for access and the 30ish columns of biographical data also stored in this database means that i need to split the DB.
So, I have managed to get all the bioinfo quite happily into a new table with:
SELECT id,[all bio column names] INTO resultsBioData FROM results;
this didn't cause to much of a problem as i am not casting anything, but for the question data i want to convert it all to integers, at the moment I have:
SELECT id,CInt(q1) AS nq1.......CInt(q220) AS nq220 INTO resultsItemData FROM results;
This seems to work fine for about 400 records but then just stops, I thought it may be because it hit something it cant convert to a integer to start with, so i wrote a little java program that deleted any record where any of ther 220 answers wasnt 0,1,2,3,4 or 5 and it still gives up around 400 (never the same record though!)
Anyone got any ideas? I am doing this on my test system at the moment and would really like something robust before i do it to our live system!
Sorry for the long winded question, but its doing my head in!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定您是在谈论在 Access 还是 SQL Server 中进行数据转换。不管怎样,由于您正在重新设计架构,现在是时候考虑您是否真的希望 resultsItemData 表包含 200 多个字段,从 nq1 到 nq220(或最终 nq240)。未来添加任何问题都需要再次更改表结构。
经验法则是“列很贵;行很便宜”。无论表是在 Access 还是 SQL Server 中,这都适用。
考虑每个 ID/问题组合一行。
我不明白为什么你当前的方法在 400 行时崩溃。不过,在您确定自己拥有最佳的桌子设计之前,我什至不会担心这一点。
编辑:由于您坚持使用您所描述的方法,我想知道它是否可以使用“追加”查询而不是“制作表”查询。创建 resultsItemData 表结构并向其附加查询,将 qx 值转换为数字。
I'm unsure whether you're talking about doing the data transformation in Access or SQL Server. Either way, since you're redesigning the schema, now is the time to consider whether you really want resultsItemData table to include 200+ fields, from nq1 through nq220 (or ultimately nq240). And any future question additions would require changing the table structure again.
The rule of thumb is "columns are expensive; rows are cheap". That applies whether the table is in Access or SQL Server.
Consider one row per id/question combination.
I don't understand why your current approach crashes at 400 rows. I wouldn't even worry about that, though, until you are sure you have the optimal table design.
Edit: Since you're stuck with the approach you described, I wonder if it might work with an "append" query instead of a "make table" query. Create resultsItemData table structure and append to it with a query which transforms the qx values to numeric.
尝试此解决方案:
参考:更改 a 的类型包含从 varchar 到 int 的数字的列
Try this solution:
Reference: Change type of a column with numbers from varchar to int
最后只是编写了一个小的java程序来创建新表并逐一检查每个记录,将字段转换为整数,需要大约一个半小时才能完成整个事情,所以当我来时我仍然在寻求更好的解决方案使用实时系统执行此操作。
Just wrote a small java program in the end that created the new table and went through each record individually casting the fields to integers, takes about an hour and a half to do the whole thing though so i am still after a better solution when i come to do this with the live system.