首页 \ 问答 \ 复杂删除SQL语句(Complex Delete SQL Statement)

复杂删除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
更新时间:2022-02-11 11:02

最满意答案

我会通过使用脚本任务(在数据流之前)来读取第一行并使用它执行任何操作来处理此问题。

然后在数据流任务中,我将平面文件源设置为忽略第一行并将第二行作为数据导入。


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.

enter image description here

相关问答

更多
  • 解决方案概述 你必须使用脚本组件来实现这一点。 根据您自己的逻辑,使用非同步输出缓冲区从行开始生成多行。 解决方案详情 添加一个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 ...
  • 我会通过使用脚本任务(在数据流之前)来读取第一行并使用它执行任何操作来处理此问题。 然后在数据流任务中,我将平面文件源设置为忽略第一行并将第二行作为数据导入。 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"
  • (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 ...

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)