复杂删除SQL语句(Complex Delete SQL Statement)
为什么删除语句有超出其选择对应项的限制?
我没有被卡住,因为问题很容易解决,但我宁愿修复我的理解而不是继续使用变通办法。
例如,我有一个无向的边缘列表,其中包含字段V1和V2。 不幸的是,在创建表时,我引入了重复项(即V1 - > V2和V2 - > V1)。 为了识别重复,我运行了查询:
SELECT t1.V1, t1.V2 FROM table t1 WHERE t1.V1 > t1.V2 and EXISTS ( SELECT * FROM table t2 WHERE t2.V1 = t1.V2 and t2.V2 = t1.V1 )
因为这从表中返回了一组很好的行,我想我应该能够用delete替换select行并重新运行相同的查询。 然而,Sql server生气了,而是给了我红色字母。 关于语法和调整之后,关于绑定多部分标识符的事情。
我设法将select存储在一个表变量中并完成删除 - 这没关系,但是我的原始方法有什么问题,我可以用一个SQL语句完成它吗?
Why do delete statements have limitations beyond their select counterparts?
I am not stuck because the problem was easy enough to work around but I'd rather fix my understanding than continue using workarounds.
Case in point, I have an undirected, edge list with fields, V1 and V2. Unfortunately, when creating the table, I introduced duplicates (i.e. V1 -> V2 and V2 -> V1). To identify the dups, I ran the query:
SELECT t1.V1, t1.V2 FROM table t1 WHERE t1.V1 > t1.V2 and EXISTS ( SELECT * FROM table t2 WHERE t2.V1 = t1.V2 and t2.V2 = t1.V1 )
since this returned a nice set of rows from the table, I thought I should be able to replace the select line with delete and rerun the same query. Sql server however, got mad and gave me red letters instead. Once about syntax and after a tweak, something about binding multipart identifiers.
I managed to store the select in a table variable and get the delete done - which is ok, but what was wrong with my original approach and could I have done it with a single SQL statement?
原文:https://stackoverflow.com/questions/16285034
最满意答案
我会通过使用脚本任务(在数据流之前)来读取第一行并使用它执行任何操作来处理此问题。
然后在数据流任务中,我将平面文件源设置为忽略第一行并将第二行作为数据导入。
Thank you all. Here is an alternative solution
I used a script component in SSIS to do this.
Step1: Create a variable called RowNumber.
Step2: Then add a script component which will add an additional column and increments row numbers.
SSIS Script component
private int m_rowNumber; public override void PreExecute() { base.PreExecute(); m_rowNumber = 0; } public override void Input0_ProcessInputRow(Input0Buffer Row) { m_rowNumber++; Row.RowNumber = m_rowNumber; }
Step3: Use the output of Script component as the input of conditional split and create a condition with RowNumber == 1.
The Multicast will split the data accordingly.
相关问答
更多-
解决方案概述 你必须使用脚本组件来实现这一点。 根据您自己的逻辑,使用非同步输出缓冲区从行开始生成多行。 解决方案详情 添加一个DataFlow任务 在DataFlow Task添加Flat File Source , Script Component和目标 在脚本组件中,选择ID , Data列作为输入 转至Input and Outputs页面,单击输出并将Synchronous Input属性更改为none 将两个输出列ID和Data添加到Output 将脚本语言更改为Visual Basic 在脚本 ...
-
你需要的是这个 。 这里的想法是: 您在业务流程中(通过执行接收管道)分配信封架构,从而使用Microsoft.XLANGs.Pipeline.XLANGPipelineManager.ExecuteReceivePipeline获得对已分发的单个消息的控制权 使用Microsoft.XLANGs.Pipeline.SendPipelineInputMessages将已删除的单个消息(您想要的数量)添加到发送管道输入消息 最后,使用Microsoft.XLANGs.Pipeline.XLANGPipelin ...
-
它实际上是相同的公式,除了我们将使用双引号而不是单引号 LEN([email]) - LEN(REPLACE([email], "@", "")) 该表达式生成@字符的实例总数。 聪明的方法,我可能会陷入困境,试图使用FINDSTRING It's virtually the same formula except we'll use double quotes instead of single quotes LEN([email]) - LEN(REPLACE([email], "@", "")) ...
-
您可以通过添加REPLACENULL函数来使用这两种情况,该函数处理NULL将错误地FINDSTRING的情况 情况1:字符串包含数值 FINDSTRING(REPLACENULL(Column,""),"1",1) >= 1 || FINDSTRING(REPLACENULL(Column,""),"2",1) >= 1 ... 情况2:字符串不包含数值 FINDSTRING(REPLACENULL(Column,""),"1",1) == 0 && FINDSTRING(REPLACENULL(Col ...
-
我猜你的年龄有一些NULL值。 如果你想发送NULL到相同的未知输出,那么你可以改变表达式为: ISNULL(Age) || Age == -1 I am guessing that you have some NULL values for age. If you want to send NULL's to the same Unknown Output then you could change the expressions to be: ISNULL(Age) || Age == -1
-
这似乎是一个有趣的问题所以我对它进行了一些调整。 是的,你绝对可以使用JET驱动程序来读取平面文件。 HOW TO:使用Jet OLE DB提供程序4.0连接到ISAM数据库请参阅打开文本部分 默认情况下,它期望文件是CSV,但您可以在Schema.INI中指定格式,该格式与连接管理器指向的文件夹位于同一文件夹中。 有关CM的一点需要注意,它指向文本文件的文件夹,而不是特定文件。 创建Connection Manager时,需要进入“全部”选项卡(选择“本机OLE DB \ Microsoft Jet 4. ...
-
由于文件是引用限定的,而不是使用常规管道分割 我们可以拆分列之间的管道,即"|" 。 因此,条件拆分表达式变为(通过使用转义序列\"作为引号,并将其替换为任意1个长度字符,例如x : LEN([Column 0]) - LEN(REPLACE([Column 0],"\"|\"","xx")) != x Since the file is quote qualified, instead of splitting using the regular pipe | we can split for the p ...
-
SSIS使用条件拆分或脚本在flatfile中拆分行(SSIS Split rows in a flatfile using conditional split or script)[2023-04-29]
我会通过使用脚本任务(在数据流之前)来读取第一行并使用它执行任何操作来处理此问题。 然后在数据流任务中,我将平面文件源设置为忽略第一行并将第二行作为数据导入。 Thank you all. Here is an alternative solution I used a script component in SSIS to do this. Step1: Create a variable called RowNumber. Step2: Then add a script component which ... -
尝试这个 : SUBSTRING([Column 0],1,2) == "TH" Try this : SUBSTRING([Column 0],1,2) == "TH"
-
SSIS条件拆分NULL(SSIS Conditional Split NULL)[2022-05-29]
(ISNULL(ModuleLevelId) && !ISNULL(LEV_CODE)) || (!ISNULL(ModuleLevelId) && ISNULL(LEV_CODE)) || ((ISNULL(LEV_CODE) ? 0 - ModuleLevelId :(DT_I4)LEV_CODE) != ModuleLevelId) 我想你需要围绕你的第三个任期: (ISNULL(LEV_CODE) ? 0 - ModuleLevelId :(DT_I4)LEV_CODE) != ModuleL ...