想知道SQL Server中的一个简单SQL概念。 我有两个表,基于某些IF条件,我想更新并插入来自不同表的数据。我正在使用下面的查询来存储过程。
IF( SELECT COUNT(colname) FROM [dbo].[Table_BKP] WHERE colname LIKE ('%Dear(DEC''D)444-0292/555-4528C%'))> 0
BEGIN
;WITH Dear AS
(
SELECT ID,colname,SUBSTRING(colname,PATINDEX('%(DEC''D)%',colname),LEN(colname)-1) AS colname_4SUPPL,LEFT(colname,PATINDEX('%(DEC''D)%',colname)-1) AS colname_4MAIN
FROM [dbo].[Table_BKP] WHERE RTRIM(LTRIM(colname)) LIKE ('Dear(DEC''D)444-0292/555-4528C')
) SELECT * INTO Dear_ FROM Dear
UPDATE A SET A.colname = B.colname_4MAIN FROM Table AS A INNER JOIN
Dear_ AS B ON A.ID = B.ID
INSERT INTO Table_SUPPL(ID,colnameMSC,[SOURCE],[TARGET])
SELECT ID,replace(colname_4SUPPL,'(DEC''D)',''),'colname','colnameMSC'
FROM Dear_
END
更新语句工作正常,数据已更新到所需的表中。但是,数据未插入到所需的表中。
但是,当我改变下面的顺序时,它运作得很好。
IF( SELECT COUNT(colname) FROM [dbo].[Table_BKP] WHERE colname LIKE ('%Dear(DEC''D)444-0292/555-4528C%'))> 0
BEGIN
;WITH Dear AS
(
SELECT ID,colname,SUBSTRING(colname,PATINDEX('%(DEC''D)%',colname),LEN(colname)-1) AS colname_4SUPPL,LEFT(colname,PATINDEX('%(DEC''D)%',colname)-1) AS colname_4MAIN
FROM [dbo].[Table_BKP] WHERE RTRIM(LTRIM(colname)) LIKE ('Dear(DEC''D)444-0292/555-4528C')
) SELECT * INTO Dear_ FROM Dear
INSERT INTO Table_SUPPL(ID,colnameMSC,[SOURCE],[TARGET]) SELECT
ID,replace(colname_4SUPPL,'(DEC''D)',''),'colname','colnameMSC' FROM
Dear_
UPDATE A SET A.colname = B.colname_4MAIN FROM Table AS A INNER JOIN
Dear_ AS B ON A.ID = B.ID
END
为什么,formar问题没有按预期工作?而且,后一个[改变sequesnce之后(第一次INSERT然后更新)];它的工作完美...... SQL Server如何管理这些序列?任何人都可以澄清这个简单的疑问吗?感谢。