将SQL server表数据从列结构转换为其他列结构

将SQL server表数据从列结构转换为其他列结构,sql,sql-server,sql-server-2012,Sql,Sql Server,Sql Server 2012,我刚刚接手了我所在的开发工作,基本上已经陷入了SQL server的深渊,我对查询和格式不是很了解,所以请对我放轻松 我使用的是MicrosoftSQLServer2012,我试图实现的基本目标是将一个数据库表中的数据重新格式化为不同的结构,然后将其导入新的数据库。我从创建软件的开发人员那里得到了下面的代码。我对它的功能有一个基本的了解,但是我遇到了一些错误,无论我修复了什么,我似乎都无法跟踪,并且需要一些帮助来确保格式正确,因为开发人员需要很长时间来回答任何电子邮件问题 --Specify t

我刚刚接手了我所在的开发工作,基本上已经陷入了SQL server的深渊,我对查询和格式不是很了解,所以请对我放轻松

我使用的是MicrosoftSQLServer2012,我试图实现的基本目标是将一个数据库表中的数据重新格式化为不同的结构,然后将其导入新的数据库。我从创建软件的开发人员那里得到了下面的代码。我对它的功能有一个基本的了解,但是我遇到了一些错误,无论我修复了什么,我似乎都无法跟踪,并且需要一些帮助来确保格式正确,因为开发人员需要很长时间来回答任何电子邮件问题

--Specify the database, table and columns we want to insert into
INSERT INTO new_database.dbo.QuizQuestions (uuid, [type], question, answer, created_date, modified_date,
master_category, master_decade, master_difficulty, is_editable, is_deletable, media)

--Get the data from the old database (old_database) and map the media filename to the media2 table in the new database

select 
   questions.uuid, 
   questions.newtype, 
   questions.QuestionText, 
   questions.AnswerText, 
   questions.CreatedDate, 
   questions.ModifiedDate, 
   questions.master_category, 
   questions.master_decade, 
   questions.master_difficulty,
   questions.IsEditable, 
   questions.IsDeletable,
   media.id as mediaid 
from
(
   select NEWID() as uuid, 
   CASE 
          WHEN qqt.TypeName = 'Sound' THEN 'Audio'
          ELSE qqt.TypeName
   END AS newtype,
   qq.QuestionText, qq.AnswerText, 
   qq.CreatedDate, 
   CASE 
          WHEN qq.ModifiedDate is NOT NULL THEN qq.ModifiedDate
          ELSE GETDATE()
   END as ModifiedDate, 
   CASE
          WHEN cat.id is NOT NULL THEN cat.id
          ELSE 1
   END as master_category,
   qqdc.QuizQuestionDecadeID as master_decade,
   qd.id as master_difficulty,
   qq.IsEditable, qq.IsDeletable,
   qq.FilePath,
   SUBSTRING(
          qq.FilePath, 
          len(qq.FilePath) - charindex('/', reverse(qq.FilePath)) + 2, 
          (len(qq.FilePath) - charindex('.', reverse(qq.FilePath))) - (len(qq.FilePath) - charindex('/', reverse(qq.FilePath))) - 1) as fname
   --len(qq.FilePath) - charindex('.', reverse(qq.FilePath)) as positionoflastdot,
   --len(qq.FilePath) - charindex('/', reverse(qq.FilePath)) as positionoflastslash
   from old_database.dbo.QuizQuestion qq
   left join old_database.dbo.QuizQuestionType qqt on qq.QuizQuestionTypeID = qqt.QuizQuestionTypeID
   left join old_database.dbo.QuizQuestionDifficulty qqd on qq.QuizQuestionDifficultyID = qqd.QuizQuestionDifficultyID
   left join old_database.dbo.QuizQuestionCategory qqc on qq.QuizQuestionCategoryID = qqc.QuizQuestionCategoryID
   left join old_database.dbo.QuizQuestionDecade qqdc on qq.QuizQuestionDecadeID = qqdc.QuizQuestionDecadeID
   left join old_database.dbo.QuizQuestionCategory qqmc on qq.MasterCategoryID = qqmc.MasterQuestionCategoryID
   left join old_database_Demo.dbo.QuizCategories cat on qqc.CategoryName = cat.name
   left join old_database_Demo.dbo.QuizDifficulties qd on qd.id = qq.MasterDifficultyID
   where qq.MasterCategoryID is not null
) as questions
left join new_database.dbo.Media2 media on media.name = REPLACE(fname,'QUIZ','')
一开始,我是

Msg 537, Level 16, State 2, Line 2
Invalid length parameter passed to the LEFT or SUBSTRING function.
The statement has been terminated.
因此,我添加了从行末尾移动媒体,并将其放在答案旁边 并将选择列表调整为匹配,因为这是表格的结构

--Specify the database, table and columns we want to insert into
INSERT INTO new_database.dbo.QuizQuestions (uuid, [type], question, answer,  media, created_date, modified_date,
master_category, master_decade, master_difficulty, is_editable, is_deletable)

--Get the data from the old database (old_database) and map the media filename to the media2 table in the new database

select 
   questions.uuid, 
   questions.newtype, 
   questions.QuestionText, 
   questions.AnswerText,
   media.id as mediaid, 
   questions.CreatedDate, 
   questions.ModifiedDate, 
   questions.master_category, 
   questions.master_decade, 
   questions.master_difficulty,
   questions.IsEditable, 
   questions.IsDeletable,

 from
(
   select NEWID() as uuid, 
   CASE 
          WHEN qqt.TypeName = 'Sound' THEN 'Audio'
          ELSE qqt.TypeName
   END AS newtype,
   qq.QuestionText, qq.AnswerText, 
   qq.CreatedDate, 
   CASE 
          WHEN qq.ModifiedDate is NOT NULL THEN qq.ModifiedDate
          ELSE GETDATE()
   END as ModifiedDate, 
   CASE
          WHEN cat.id is NOT NULL THEN cat.id
          ELSE 1
   END as master_category,
   qqdc.QuizQuestionDecadeID as master_decade,
   qd.id as master_difficulty,
   qq.IsEditable, qq.IsDeletable,
   qq.FilePath,
   SUBSTRING(
          qq.FilePath, 
          len(qq.FilePath) - charindex('/', reverse(qq.FilePath)) + 2, 
          (len(qq.FilePath) - charindex('.', reverse(qq.FilePath))) - (len(qq.FilePath) - charindex('/', reverse(qq.FilePath))) - 1) as fname
   --len(qq.FilePath) - charindex('.', reverse(qq.FilePath)) as positionoflastdot,
   --len(qq.FilePath) - charindex('/', reverse(qq.FilePath)) as positionoflastslash
   from old_database.dbo.QuizQuestion qq
   left join old_database.dbo.QuizQuestionType qqt on qq.QuizQuestionTypeID = qqt.QuizQuestionTypeID
   left join old_database.dbo.QuizQuestionDifficulty qqd on qq.QuizQuestionDifficultyID = qqd.QuizQuestionDifficultyID
   left join old_database.dbo.QuizQuestionCategory qqc on qq.QuizQuestionCategoryID = qqc.QuizQuestionCategoryID
   left join old_database.dbo.QuizQuestionDecade qqdc on qq.QuizQuestionDecadeID = qqdc.QuizQuestionDecadeID
   left join old_database.dbo.QuizQuestionCategory qqmc on qq.MasterCategoryID = qqmc.MasterQuestionCategoryID
   left join old_database_Demo.dbo.QuizCategories cat on qqc.CategoryName = cat.name
   left join old_database_Demo.dbo.QuizDifficulties qd on qd.id = qq.MasterDifficultyID
   where qq.MasterCategoryID is not null
) as questions
left join new_database.dbo.Media2 media on media.name = REPLACE(fname,'QUIZ','')
现在当我执行命令的时候

Msg 156, Level 15, State 1, Line 19
Incorrect syntax near the keyword 'from'.
Msg 156, Level 15, State 1, Line 55
Incorrect syntax near the keyword 'as'.
这就是我目前所处的困境,我不知道如何前进以使其正确执行,我已经在谷歌上搜索并研究了allot,我甚至让负责访问的客户数据库人员查看了一下,他和我一样困惑不解

如果有人能帮我指出哪里出了问题,那就太好了

致意
dan

我将研究子串函数中Len和Charindex函数的结果

也许可以在单独的查询中重复它们。 “无效长度”错误表明正在使用不正确的条件搜索字符串的一部分

例如

    len(qq.FilePath) - charindex('/', reverse(qq.FilePath)) + 2
如果结果

charindex('/', reverse(qq.FilePath)) + 2
比…的结果更大

len(qq.FilePath)
然后,您将尝试使用负数查找字符串(子字符串)的一部分-因此,如果上面的结果是-2,例如,您将从FilePath中请求-2个字符…这是传递给子字符串的无效长度参数

我会运行这个

Select   
len(qq.FilePath) - charindex('/', reverse(qq.FilePath)) + 2 as 'SubStrStartPos', 
(len(qq.FilePath) - charindex('.', reverse(qq.FilePath))) - (len(qq.FilePath) - charindex('/', reverse(qq.FilePath))) - 1 as 'NoOfChars'

from old_database.dbo.QuizQuestion qq
 left join old_database.dbo.QuizQuestionType qqt on qq.QuizQuestionTypeID = qqt.QuizQuestionTypeID
 left join old_database.dbo.QuizQuestionDifficulty qqd on qq.QuizQuestionDifficultyID = qqd.QuizQuestionDifficultyID
left join old_database.dbo.QuizQuestionCategory qqc on qq.QuizQuestionCategoryID = qqc.QuizQuestionCategoryID
left join old_database.dbo.QuizQuestionDecade qqdc on qq.QuizQuestionDecadeID = qqdc.QuizQuestionDecadeID
left join old_database.dbo.QuizQuestionCategory qqmc on qq.MasterCategoryID = qqmc.MasterQuestionCategoryID
left join old_database_Demo.dbo.QuizCategories cat on qqc.CategoryName = cat.name
left join old_database_Demo.dbo.QuizDifficulties qd on qd.id = qq.MasterDifficultyID
where qq.MasterCategoryID is not null

检查结果是否为负值,排除故障并修改子字符串。

我将调查子字符串函数中Len和Charindex函数的结果

也许可以在单独的查询中重复它们。 “无效长度”错误表明正在使用不正确的条件搜索字符串的一部分

例如

    len(qq.FilePath) - charindex('/', reverse(qq.FilePath)) + 2
如果结果

charindex('/', reverse(qq.FilePath)) + 2
比…的结果更大

len(qq.FilePath)
然后,您将尝试使用负数查找字符串(子字符串)的一部分-因此,如果上面的结果是-2,例如,您将从FilePath中请求-2个字符…这是传递给子字符串的无效长度参数

我会运行这个

Select   
len(qq.FilePath) - charindex('/', reverse(qq.FilePath)) + 2 as 'SubStrStartPos', 
(len(qq.FilePath) - charindex('.', reverse(qq.FilePath))) - (len(qq.FilePath) - charindex('/', reverse(qq.FilePath))) - 1 as 'NoOfChars'

from old_database.dbo.QuizQuestion qq
 left join old_database.dbo.QuizQuestionType qqt on qq.QuizQuestionTypeID = qqt.QuizQuestionTypeID
 left join old_database.dbo.QuizQuestionDifficulty qqd on qq.QuizQuestionDifficultyID = qqd.QuizQuestionDifficultyID
left join old_database.dbo.QuizQuestionCategory qqc on qq.QuizQuestionCategoryID = qqc.QuizQuestionCategoryID
left join old_database.dbo.QuizQuestionDecade qqdc on qq.QuizQuestionDecadeID = qqdc.QuizQuestionDecadeID
left join old_database.dbo.QuizQuestionCategory qqmc on qq.MasterCategoryID = qqmc.MasterQuestionCategoryID
left join old_database_Demo.dbo.QuizCategories cat on qqc.CategoryName = cat.name
left join old_database_Demo.dbo.QuizDifficulties qd on qd.id = qq.MasterDifficultyID
where qq.MasterCategoryID is not null

并检查结果是否为负值,排除故障并修改子字符串。

我已将from置于选择列表的末尾,而不是下方,从而使查询正常工作

select 
   questions.uuid, 
   questions.newtype, 
   questions.QuestionText, 
   questions.AnswerText,
   media.id as mediaid, 
   questions.CreatedDate, 
   questions.ModifiedDate, 
   questions.master_category, 
   questions.master_decade, 
   questions.master_difficulty,
   questions.IsEditable, 
   questions.IsDeletable from
(
也可以在子查询中将
-1
更改为
-0
,以删除任何负值

select
(len(qq.FilePath) - charindex('/', reverse(qq.FilePath)) + 2),`

`(len(qq.FilePath) - charindex('.', reverse(qq.FilePath))) - (len(qq.FilePath) - charindex('/', reverse(qq.FilePath))) - 0) as fname
但它没有正确映射媒体文件,并在新表中的媒体列返回null,但我可以解决这个问题


谢谢大家的帮助

我通过将from放在select列表的末尾而不是下面,成功地实现了查询

select 
   questions.uuid, 
   questions.newtype, 
   questions.QuestionText, 
   questions.AnswerText,
   media.id as mediaid, 
   questions.CreatedDate, 
   questions.ModifiedDate, 
   questions.master_category, 
   questions.master_decade, 
   questions.master_difficulty,
   questions.IsEditable, 
   questions.IsDeletable from
(
也可以在子查询中将
-1
更改为
-0
,以删除任何负值

select
(len(qq.FilePath) - charindex('/', reverse(qq.FilePath)) + 2),`

`(len(qq.FilePath) - charindex('.', reverse(qq.FilePath))) - (len(qq.FilePath) - charindex('/', reverse(qq.FilePath))) - 0) as fname
但它没有正确映射媒体文件,并在新表中的媒体列返回null,但我可以解决这个问题


谢谢大家的帮助

我忘了加上iv'e在new_database和old_database中重新命名了真实的数据库名称,因为它们的名称是新旧软件的品牌。您发布了哪个版本?您列出的第一个错误的版本还是第二个错误的版本?很抱歉,我忘了添加我使用的服务器我是新加入的,它的Microsoft SQL server 2012和我发布的版本是第一个出现错误的版本,我发现它没有反映列结构,因此更改了查询,我将把它添加到帖子中我所做的更改您是否与提供此代码的开发人员谈过这些错误?我尝试了多种方法,但都不是很好的解决方法快给我回电话。我自己和他们一起试着解决这个问题已经有一个月了。我想问题是有很多人都在解决这个问题。我忘了补充一句,iv'e将真实的数据库名称重新命名为new_database和old_database,因为它们的名称是新旧软件的品牌。你发布了哪个版本?您列出的第一个错误的版本还是第二个错误的版本?很抱歉,我忘了添加我使用的服务器我是新加入的,它的Microsoft SQL server 2012和我发布的版本是第一个出现错误的版本,我发现它没有反映列结构,因此更改了查询,我将把它添加到帖子中我所做的更改您是否与提供此代码的开发人员谈过这些错误?我尝试了多种方法,但都不是很好的解决方法快给我回电话。我自己和他们一起努力解决这个问题已经有大约一个月了,我想问题是有很多人都在解决这个问题。这帮了大忙,获得了新的见解谢谢这帮了大忙,获得了新的见解谢谢