我尝试创建特定的表时遇到了一些问题。我已经在这个网站上阅读了一些类似的答案,但我不确定我是否理解他们的答案和/或它如何适用于我的问题。
我有一个现有的表,其中包含配方的成分(下面的表定义):
Create Table Recipes.dbo.IngredientsByRecipeID
(
RecipeID int,
IngredientName varchar(max),
UnitTypeOfIngredient varchar(max),
QuantityOfIngredient decimal(6,3)
);
我尝试创建并显示一个表的内容,该表包含所有配方中使用的所有成分名称,按字母顺序排列,并删除所有重复的条目。
我已尝试过如何实现它的一些不同变体,但我现在还不确定我是否遇到了我的实现问题或SQL Server如何工作的问题(I&# 39; T-SQL及其方法的新手。)
我按字母顺序将成分分类到新的无重复表中的方法如下:
/*these three lines represent code which I've been trying to use to control the problems I keep getting with tables existing or not existing, I don't know if they are really helping or not. The first two only drop and create the table, the second is one attempt at a working solution for my needs*/
--drop table AlphabeticallySortedIngredientsList;
--create table AlphabeticallySortedIngredientsList (IngredientName varchar(max));
--select IngredientName into AlphabeticallySortedIngredientsList from IngredientsByRecipeID where ((select count(IngredientName) from AlphabeticallySortedIngredientsList) = 0) order by IngredientName asc;
--here is my latest attempt at getting a working solution for my needs
select IngredientName
into AlphabeticallySortedIngredientsList
from IngredientsByRecipeID
where not exists (select *
from AlphabeticallySortedIngredientsList
where IngredientName = AlphabeticallySortedIngredientsList.IngredientName)
order by IngredientName asc;
对于每个实现,我最初都有某种语法错误,但在(显然)清除了这些错误之后,我总是留下最后一个错误,而当错误根据输入而改变时,同一个对象导致错误。如果我离开' drop table'从上面的行取消注释然后这是我得到的错误:
Msg 208,Level 16,State 1,Line 5
无效的对象名称' AlphabeticallySortedIngredientsList'。
如果我还取消注释'创建表AlphabeticallySortedIngredientsList'行,然后我得到这个错误:
Msg 2714,Level 16,State 6,Line 9
已经有一个名为' AlphabeticallySortedIngredientsList'在数据库中。
如果我放弃表格然后将两行注释掉,我会得到与上面相同的错误:
Msg 2714,Level 16,State 6,Line 9
已经有一个名为' AlphabeticallySortedIngredientsList'在数据库中。
我感到很困惑,因为我的选择将IngredientName选为AlphabeticallySortedIngredientList' line无法决定是否创建表。如果我在解决方案运行之前删除了表,那么就说它无法找到。如果我在解决方案运行之前创建表,那么它表示该表已经存在。
我猜测问题在于选择'该语句的一部分是创建AlphabeticallySortedIngredientsList(假设它不存在),但条件尝试在AlphabeticallySortedIngredientsList中查找一个值,该值尚不存在,因为创建它的操作尚未发生。我是否在这个评估的正确轨道上,如果是这样,我该如何解决这个问题呢?
我需要保留IngredientsByRecipeID的原始内容,作为记录。 AlphabeticallySortedIngredientList仅用于显示IngredientsByRecipeID中所有成分的清洁有序列表。
我还应该提一下,如果我删除条件(例如,我注释掉' where'子句),那么select into工作没有任何错误。这是drop table和create table注释掉的,并且没有表AlphabeticallySortedIngredientList已存在于数据库中。
也许最好只将它们全部添加到表中而不删除重复项,然后通过并删除重复项?我的问题可能是试图一步到位。
答案 0 :(得分:2)
SELECT/INTO
语法创建一个新表。您不能使用它并在WHERE
子句中引用您正在创建的表。您希望在DISTINCT
子句中使用SELECT
来删除重复项,然后再创建表。如果需要使用现有表,则需要使用INSERT
语句,其中要插入的值来自SELECT
子句。请注意,如果您需要按名称保留表顺序,则应将名称作为主键。既然你想要这个唯一的条目,你可能还是想要这个。
SELECT DISTINCT IngredientName
INTO AlphabeticallySortedIngredientsList
FROM IngredientsByRecipeID
ADD CONSTRAINT PK_AlphabeticallySortedIngredientsList PRIMARY KEY CLUSTERED (IncredientName)
通常,我不建议保留此表,因为您始终可以从现有表中获取数据。将数据保存在单独的表中将迫使您定期更新其中一个以使它们保持同步。如果您确实选择将名称保留在单独的表中,那么您应该添加Id
列,然后使用名称的Id
更新其他(原始)表,并从中删除名称列它。在名称Id
列上添加外键约束,以便您需要在其中添加名称并将其链接到配方。
答案 1 :(得分:1)
Create table Ingredients
(ingredient varChar(50) primary key not null)
Insert Ingredients(Ingredient)
Select distinct IngredientName
From IngredientsByRecipeID
答案 2 :(得分:1)
是的,你走在正确的轨道上。 SELECT ... INTO语句将基于SELECT表达式创建表。试图在WHERE NOT EXISTS子句中过早地访问它会导致冲突。
尝试使用SELECT DISTINCT或GROUP BY子句删除重复项,而不依赖于WHERE NOT EXISTS。