SQL Server:通过从临时表中选择值来删除
我有一个目标表 TBLA
和一个源表 TBLB
。
步骤 1:我从数据库的表中获取 SQL:
-- I ran this process in a cursor such that `@Sql` is a pass-in selected column value:
DECLARE @Sql NVARCHAR(MAX);
SET @Sql = 'SELECT TBLB.coloumA, TBLB.coloumB... into ##TempTable
FROM TBLB
WHERE CONVERT(VARCHAR(10),Date,120)='2011-04-05'';
EXEC sp_ExecuteSql @sql
步骤 2:我通过调用另一个 sp_ExecuteSql
删除目标表数据:
DECLARE @CheckClear NVARCHAR(MAX);
SET @CheckClear = 'DELETE FROM TBLA WHERE EXISTS(SELECT * FROM ##TempTable)';
EXEC sp_ExecuteSql @CheckClear ;
-- This section is where the problem starts, described below.
步骤 3:通过 select * from temp table 插入到 TBLA
DECLARE @DumpSql NVARCHAR(MAX);
SET @DumpSql = 'INSERT INTO TBLA
SELECT * FROM ##TempTable';
EXEC sp_ExecuteSql @DumpSql ;
如所述步骤 2 结束时,问题如下:
执行删除语句后,我发现所有数据都丢失了,而且似乎只执行了第一个命令(DELETE FROM TBLA
)。
在尝试删除语句之前,我注释掉了 ##TempTable
值,它起作用了,我只有 2011-11-04 的数据。 (TBLA
已有 2011-11-03 和 2011-11-02 数据)。
我实际上是按日期值选择数据。到目前为止似乎是成功的。
但随后,我打开 TBLA
并看到结果仅包含 2011-11-04
数据...其余的在哪里?
请注意:
上面的 SQL 只是我使用 sp_executesql 的动态存储产品的一部分... 我的实际数据包含大约 300 个需要处理的表。这些表中的每一个都有不同的唯一标识,因此我无法使用:SELECT * FROM ##Temptable WHERE id=bla
最好的方法是什么?
I have a destination table TBLA
and a source table TBLB
.
STEP 1: I get the SQL from a table of my database:
-- I ran this process in a cursor such that `@Sql` is a pass-in selected column value:
DECLARE @Sql NVARCHAR(MAX);
SET @Sql = 'SELECT TBLB.coloumA, TBLB.coloumB... into ##TempTable
FROM TBLB
WHERE CONVERT(VARCHAR(10),Date,120)='2011-04-05'';
EXEC sp_ExecuteSql @sql
STEP 2: I delete the destination table data by calling another sp_ExecuteSql
:
DECLARE @CheckClear NVARCHAR(MAX);
SET @CheckClear = 'DELETE FROM TBLA WHERE EXISTS(SELECT * FROM ##TempTable)';
EXEC sp_ExecuteSql @CheckClear ;
-- This section is where the problem starts, described below.
STEP 3: Insert into TBLA by select * from temp table
DECLARE @DumpSql NVARCHAR(MAX);
SET @DumpSql = 'INSERT INTO TBLA
SELECT * FROM ##TempTable';
EXEC sp_ExecuteSql @DumpSql ;
As mentioned at the end of STEP 2, here is the issue:
After performing the delete statement I found that all my data is missing and it seems it just performed the first command only (DELETE FROM TBLA
).
Before I tried the delete statement, I commented out the ##TempTable
value, and it worked and I only had 2011-11-04 data. (TBLA
already had 2011-11-03 and 2011-11-02 data).
I am actually selecting data by a date value. So far it seems to be successful.
But then, I open my TBLA
and see the result only contains 2011-11-04
data...where is the rest?
PLEASE NOTE THAT:
This above SQL is just a part of my dynamic stored prod that used sp_executesql
...
My actual data consists of about 300 tables that I need to process. Each of these tables have different unique identities so I can't use : SELECT * FROM ##Temptable WHERE id=bla
What would be the best way to go about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查这个:
使用 where 条件中的子查询从 mytable 中删除行:
如果您要根据日期查找要删除的唯一值,请在临时表中创建唯一值,然后按照您正在执行的操作执行删除操作。
其次,如果您正在寻找基于连接操作的删除操作:
然后点击此链接:
如何从多个表中删除在 SQL Server 中使用 INNER JOIN 的表
检查这些链接以了解如何使用 SP_Execute 在临时表中插入行:
Sql Server - 如何将单行插入临时表?
使用一个命令将多行插入到临时表中SQL2005
希望这些对你有一点帮助..
check this:
Delete rows from mytable using a subquery in the where condition:
If you are looking for unique values to delete according to date then create unique values in your Temporary table and then perform delete operation as you are doing.
Secondly if you are looking for delete operation somewhat based on join operation:
then follow this link:
How do I delete from multiple tables using INNER JOIN in SQL server
Check these links to know how to insert row in temp table using SP_Execute :
Sql server - how to insert single row into temporary table?
Insert multiple rows into temp table with one command in SQL2005
hope these help you little..
首先,您应该确定 TBLA 的主键字段。我假设 PK 是 FLDA。
当您从临时表中存在的 TBLA 行中删除时,正确的语法是:
使用 sql server 解决此问题的更优雅的方法是 MERGE 语句:
已编辑
我看到了你更详细的问题。
当你执行:
你的所有数据将被删除,因为where条件总是返回True。
解决方法可能是连接 TBLA 中的所有表字段,以与连接 ##TempTable 中的所有表字段进行比较。
First of all, you should identify primary key fields for TBLA. I assume that PK is FLDA.
When you delete from TBLA rows that exists in temp table, the right sintax is:
A more elegant way to solve this with sql server is MERGE statement:
Edited
I have seen your more detailed question.
When you execute:
all your data will be delete because where condition allways return True.
A work around may be concatenate all table fields from TBLA to compare with concatenate all table fields from ##TempTable.