SQL查询时间问题?
SQL Query Timing Issue?
我有一个存储过程,它使用大量动态 SQL 来为客户处理一些地址数据。
第一步是查找空白地址并将它们放入带有 [reason]
代码的 table 中。
下一步查找只是缺少地址行字段的地址,我想排除在第一步中找到的任何内容。但是,它从第一步开始一直返回所有内容。
如果我 运行 之后自己查询它 returns 正确的结果,这让我认为在第二个查询 运行 之前第一次插入可能不完整。如果是这种情况,我该怎么做才能确保一个查询在下一个查询之前完成?
这是我的代码:
/***************************** BLANK ADDRESS *****************************/
/* */
/* Finds records that are missing Address Line 1, City and State */
/* */
/*************************************************************************/
-- Address 1
IF (@add1 IS NOT NULL) AND (@city IS NOT NULL) AND (@state IS NOT NULL)
BEGIN
SET @sql = 'INSERT INTO ' + @db + '.[address_issues] ' +
'SELECT ''Blank Address 1''' + @sql_output +
' FROM ' + @db + '.' + @table + ' WHERE '
IF (@add1 IS NOT NULL) SET @sql = @sql + '(' + @add1 + ' = '''' OR ' + @add1 + ' IS NULL) AND'
IF (@add2 IS NOT NULL) SET @sql = @sql + '(' + @add2 + ' = '''' OR ' + @add2 + ' IS NULL) AND'
IF (@city IS NOT NULL) SET @sql = @sql + '(' + @city + ' = '''' OR ' + @city + ' IS NULL) AND'
IF (@state IS NOT NULL) SET @sql = @sql + '(' + @state + ' = '''' OR ' + @state + ' IS NULL)'
-- Inserts query into table for output
PRINT 'Blank Address 1 SQL: ' + @sql
EXEC sp_executesql @sql
SET @sql = ''
END
/***************************** EMPTY ADDRESS FIELD *****************************/
/* */
/* Identifies records that are missing the Add1 field, but are not already */
/* included in the Blank Addresses above */
/* */
/*******************************************************************************/
-- Address 1, Line 1
IF (@add1 IS NOT NULL) AND (@city IS NOT NULL) AND (@state IS NOT NULL)
BEGIN
SET @sql = 'INSERT INTO ' + @db + '.[address_issues] ' +
'SELECT ''Address 1: Empty Address Line 1''' + @sql_output +
' FROM ' + @db + '.' + @table + ' WHERE ('
IF (@add1 IS NOT NULL) SET @sql = @sql + '(' + @add1 + ' = '''' OR ' + @add1 + ' IS NULL) AND ( '
IF (@add2 IS NOT NULL) SET @sql = @sql + '(' + @add2 + ' <> '''' OR ' + @add2 + ' IS NOT NULL) OR '
IF (@city IS NOT NULL) SET @sql = @sql + '(' + @city + ' <> '''' OR ' + @city + ' IS NOT NULL) OR '
IF (@state IS NOT NULL) SET @sql = @sql + '(' + @state + ' <> '''' OR ' + @state + ' IS NOT NULL) )'
SET @sql = @sql +
') AND (' +
@id + ' NOT IN (SELECT [Data ID] FROM [address_issues] WHERE [Reason] = ''Blank Address 1'' AND [Data ID] = ' + @id
IF (@system IS NOT NULL) SET @sql = @sql + ' AND [System] = ' + @system
IF (@id2 IS NOT NULL) SET @sql = @sql + ' AND [Data ID2] = ' + @id2
SET @sql = @sql + ') )'
-- Inserts query into table for output
PRINT 'Address 1: Empty Address Line 1 SQL: ' + @sql
EXEC sp_executesql @sql
SET @sql = ''
END
后面的代码是几个有类似问题的类似检查。然后我打印出 address_issues table.
这是两个查询的 SQL,因为动态 SQL 可能没有多大意义。
空白地址 #1 SQL:
INSERT INTO [SW].[dbo].[address_issues]
SELECT 'Blank Address 1', [Company], [Supplier_No], [Name], [Address], [City], [State], [Zip], [Country]
FROM [SW].[dbo].[vendors_plex_raw]
WHERE ([Address] = '' OR [Address] IS NULL)
AND ([City] = '' OR [City] IS NULL)
AND ([State] = '' OR [State] IS NULL)
地址 1:空地址行 1 SQL:
INSERT INTO [SW].[dbo].[address_issues]
SELECT 'Address 1: Empty Address Line 1', [Company], [Supplier_No], [Name], [Address], [City], [State], [Zip], [Country]
FROM [SW].[dbo].[vendors_plex_raw] r
WHERE (([Address] = '' OR [Address] IS NULL) AND
(([City] <> '' OR [City] IS NOT NULL) OR
([State] <> '' OR [State] IS NOT NULL))) AND
([Supplier_No] NOT IN (SELECT [Data ID]
FROM [address_issues]
WHERE [Reason] = 'Blank Address 1'
AND [Data ID] = [Supplier_No]
AND [System] = [Company]))
Sample Data and Query Results - as shown below
Company Supplier_No Name Address City State Zip Country
CO1 1252725 Sample Company 1 29833 Narwall Ave Chicago IL 60673-1298 USA
CO1 1252726 Sample Company 2 Cleveland OH 44114 USA
CO1 1252727 Sample Company 3 Cleveland OH 44118 USA
CO1 1252728 Sample Company 4 USA
CO1 1252729 Sample Company 5
CO2 1252730 Sample Company 6 375 Saxon Pl Saxonburg PA 16056 USA
CO2 1252731 Sample Company 7 Austin TX 78745 USA
CO2 1252732 Sample Company 8 Brooklyn NY 11210 USA
CO2 1252798 Sample Company 9 USA
CO2 1252797 Sample Company 10
Expected Query 1 Results (Works as Expected)
CO1 1252728 Sample Company 4 USA
CO1 1252729 Sample Company 5
CO2 1252798 Sample Company 9 USA
CO2 1252797 Sample Company 10
Expected Query 2 Results
CO1 1252726 Sample Company 2 Cleveland OH 44114 USA
CO1 1252727 Sample Company 3 Cleveland OH 44118 USA
CO2 1252731 Sample Company 7 Austin TX 78745 USA
CO2 1252732 Sample Company 8 Brooklyn NY 11210 USA
Query 2 Current/Actual Results
CO1 1252726 Sample Company 2 Cleveland OH 44114 USA
CO1 1252727 Sample Company 3 Cleveland OH 44118 USA
CO1 1252728 Sample Company 4 USA
CO1 1252729 Sample Company 5
CO2 1252731 Sample Company 7 Austin TX 78745 USA
CO2 1252732 Sample Company 8 Brooklyn NY 11210 USA
CO2 1252798 Sample Company 9 USA
CO2 1252797 Sample Company 10
- SSMS v18.5
- SQL 服务器 2019 (v15.0.4083.2)
首先,我会拼出列名。
INSERT INTO table
(Col1, Col2, ...)
SELECT ...
否则,有人添加您根本不关心的列会破坏您的代码。
其次,你有
INSERT INTO [SW].[dbo].[address_issues]
然后 FROM [address_issues]
如果你是运行这个来自除SW之外的另一个数据库,就会出现不匹配。
SW.dbo。嵌套 select
中的 address_issues table 缺失
我有一个存储过程,它使用大量动态 SQL 来为客户处理一些地址数据。
第一步是查找空白地址并将它们放入带有 [reason]
代码的 table 中。
下一步查找只是缺少地址行字段的地址,我想排除在第一步中找到的任何内容。但是,它从第一步开始一直返回所有内容。
如果我 运行 之后自己查询它 returns 正确的结果,这让我认为在第二个查询 运行 之前第一次插入可能不完整。如果是这种情况,我该怎么做才能确保一个查询在下一个查询之前完成?
这是我的代码:
/***************************** BLANK ADDRESS *****************************/
/* */
/* Finds records that are missing Address Line 1, City and State */
/* */
/*************************************************************************/
-- Address 1
IF (@add1 IS NOT NULL) AND (@city IS NOT NULL) AND (@state IS NOT NULL)
BEGIN
SET @sql = 'INSERT INTO ' + @db + '.[address_issues] ' +
'SELECT ''Blank Address 1''' + @sql_output +
' FROM ' + @db + '.' + @table + ' WHERE '
IF (@add1 IS NOT NULL) SET @sql = @sql + '(' + @add1 + ' = '''' OR ' + @add1 + ' IS NULL) AND'
IF (@add2 IS NOT NULL) SET @sql = @sql + '(' + @add2 + ' = '''' OR ' + @add2 + ' IS NULL) AND'
IF (@city IS NOT NULL) SET @sql = @sql + '(' + @city + ' = '''' OR ' + @city + ' IS NULL) AND'
IF (@state IS NOT NULL) SET @sql = @sql + '(' + @state + ' = '''' OR ' + @state + ' IS NULL)'
-- Inserts query into table for output
PRINT 'Blank Address 1 SQL: ' + @sql
EXEC sp_executesql @sql
SET @sql = ''
END
/***************************** EMPTY ADDRESS FIELD *****************************/
/* */
/* Identifies records that are missing the Add1 field, but are not already */
/* included in the Blank Addresses above */
/* */
/*******************************************************************************/
-- Address 1, Line 1
IF (@add1 IS NOT NULL) AND (@city IS NOT NULL) AND (@state IS NOT NULL)
BEGIN
SET @sql = 'INSERT INTO ' + @db + '.[address_issues] ' +
'SELECT ''Address 1: Empty Address Line 1''' + @sql_output +
' FROM ' + @db + '.' + @table + ' WHERE ('
IF (@add1 IS NOT NULL) SET @sql = @sql + '(' + @add1 + ' = '''' OR ' + @add1 + ' IS NULL) AND ( '
IF (@add2 IS NOT NULL) SET @sql = @sql + '(' + @add2 + ' <> '''' OR ' + @add2 + ' IS NOT NULL) OR '
IF (@city IS NOT NULL) SET @sql = @sql + '(' + @city + ' <> '''' OR ' + @city + ' IS NOT NULL) OR '
IF (@state IS NOT NULL) SET @sql = @sql + '(' + @state + ' <> '''' OR ' + @state + ' IS NOT NULL) )'
SET @sql = @sql +
') AND (' +
@id + ' NOT IN (SELECT [Data ID] FROM [address_issues] WHERE [Reason] = ''Blank Address 1'' AND [Data ID] = ' + @id
IF (@system IS NOT NULL) SET @sql = @sql + ' AND [System] = ' + @system
IF (@id2 IS NOT NULL) SET @sql = @sql + ' AND [Data ID2] = ' + @id2
SET @sql = @sql + ') )'
-- Inserts query into table for output
PRINT 'Address 1: Empty Address Line 1 SQL: ' + @sql
EXEC sp_executesql @sql
SET @sql = ''
END
后面的代码是几个有类似问题的类似检查。然后我打印出 address_issues table.
这是两个查询的 SQL,因为动态 SQL 可能没有多大意义。
空白地址 #1 SQL:
INSERT INTO [SW].[dbo].[address_issues]
SELECT 'Blank Address 1', [Company], [Supplier_No], [Name], [Address], [City], [State], [Zip], [Country]
FROM [SW].[dbo].[vendors_plex_raw]
WHERE ([Address] = '' OR [Address] IS NULL)
AND ([City] = '' OR [City] IS NULL)
AND ([State] = '' OR [State] IS NULL)
地址 1:空地址行 1 SQL:
INSERT INTO [SW].[dbo].[address_issues]
SELECT 'Address 1: Empty Address Line 1', [Company], [Supplier_No], [Name], [Address], [City], [State], [Zip], [Country]
FROM [SW].[dbo].[vendors_plex_raw] r
WHERE (([Address] = '' OR [Address] IS NULL) AND
(([City] <> '' OR [City] IS NOT NULL) OR
([State] <> '' OR [State] IS NOT NULL))) AND
([Supplier_No] NOT IN (SELECT [Data ID]
FROM [address_issues]
WHERE [Reason] = 'Blank Address 1'
AND [Data ID] = [Supplier_No]
AND [System] = [Company]))
Sample Data and Query Results - as shown below
Company Supplier_No Name Address City State Zip Country
CO1 1252725 Sample Company 1 29833 Narwall Ave Chicago IL 60673-1298 USA
CO1 1252726 Sample Company 2 Cleveland OH 44114 USA
CO1 1252727 Sample Company 3 Cleveland OH 44118 USA
CO1 1252728 Sample Company 4 USA
CO1 1252729 Sample Company 5
CO2 1252730 Sample Company 6 375 Saxon Pl Saxonburg PA 16056 USA
CO2 1252731 Sample Company 7 Austin TX 78745 USA
CO2 1252732 Sample Company 8 Brooklyn NY 11210 USA
CO2 1252798 Sample Company 9 USA
CO2 1252797 Sample Company 10
Expected Query 1 Results (Works as Expected)
CO1 1252728 Sample Company 4 USA
CO1 1252729 Sample Company 5
CO2 1252798 Sample Company 9 USA
CO2 1252797 Sample Company 10
Expected Query 2 Results
CO1 1252726 Sample Company 2 Cleveland OH 44114 USA
CO1 1252727 Sample Company 3 Cleveland OH 44118 USA
CO2 1252731 Sample Company 7 Austin TX 78745 USA
CO2 1252732 Sample Company 8 Brooklyn NY 11210 USA
Query 2 Current/Actual Results
CO1 1252726 Sample Company 2 Cleveland OH 44114 USA
CO1 1252727 Sample Company 3 Cleveland OH 44118 USA
CO1 1252728 Sample Company 4 USA
CO1 1252729 Sample Company 5
CO2 1252731 Sample Company 7 Austin TX 78745 USA
CO2 1252732 Sample Company 8 Brooklyn NY 11210 USA
CO2 1252798 Sample Company 9 USA
CO2 1252797 Sample Company 10
- SSMS v18.5
- SQL 服务器 2019 (v15.0.4083.2)
首先,我会拼出列名。
INSERT INTO table
(Col1, Col2, ...)
SELECT ...
否则,有人添加您根本不关心的列会破坏您的代码。
其次,你有
INSERT INTO [SW].[dbo].[address_issues]
然后 FROM [address_issues]
如果你是运行这个来自除SW之外的另一个数据库,就会出现不匹配。
SW.dbo。嵌套 select
中的 address_issues table 缺失