简体   繁体   English

是否可以使用 excel 单元格值 - 作为 T-SQL 语句中的参考?

[英]Is it possible to use excel cell value - as a reference in T-SQL statement?

I have table in SSMS (SQL Server Management Studio):我在 SSMS(SQL Server Management Studio)中有表:

在此处输入图片说明

from which I need to delete data, based on the SurveyCode value.我需要根据SurveyCode值从中删除数据。

Text part of the SurveyCode value CSS-2020-08- is stored in an Excel sheet, in B4 cell, file name Survey.xlsx : SurveyCodeCSS-2020-08-文本部分存储在 Excel 工作表中的B4单元格中,文件名为Survey.xlsx

在此处输入图片说明

And, for deleting / adding new data into my Survey table, I use the following code:而且,为了在我的Survey表中删除/添加新数据,我使用以下代码:

DELETE FROM [WH].[Fact].[Survey]
WHERE [WH].[Fact].[Survey].[SurveyCode] NOT LIKE '%2020-08%'

INSERT INTO
    [WH].Fact.[Survey]  (
          [SurveyCode]
         ,[SurveyDate]
         ,[Gender]
         ,[Age]
         ,[Questions]
         ,[Rating]
         ,[Score]
                       )
SELECT 
        staged.[SurveyCode]
       ,staged.[SurveyDate]
       ,staged.[Gender]
       ,staged.[Age]
       ,staged.[Question]
       ,staged.[Rating]
       ,staged.[Score]

FROM 
      [WH-SSIS].[WHCSS].[Staged_Survey] staged;

Is it possible - instead of using the SQL statement:是否有可能 - 而不是使用 SQL 语句:

WHERE [WH].[Fact].[Survey].[SurveyCode] NOT LIKE '%2020-08%'

to reference CSS-2020-08- directly from the Excel spreadsheet - right inside the SQL statement?直接从 Excel 电子表格中引用CSS-2020-08-就在 SQL 语句中?

I think我认为

declare @parameter nvarchar(50)
SELECT parameter =f2
FROM OPENROWSET(
'Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;HDR=NO;Database=T:\temp\Survey.xlsx',
'select f2 from [sheet1$] where f1='''SurveyCode'''')

then you can use parameter in然后你可以使用参数

WHERE [WH].[Fact].[Survey].[SurveyCode] NOT LIKE '%'+@parameter+'%'

For reading the xlsx file, first of all you will need to have the right provider installed, you can check that by:要读取 xlsx 文件,首先您需要安装正确的提供程序,您可以通过以下方式进行检查:

USE [master];
EXEC sys.sp_enum_oledb_providers;

You should see Microsoft.ACE.OLEDB.XX.0, where XX can be typically either 12 or 16. If you don't see it, then you will need to install the provider.您应该会看到 Microsoft.ACE.OLEDB.XX.0,其中 XX 通常可以是 12 或 16。如果您没有看到它,则需要安装提供程序。 Find it from MS here . 在这里从 MS 找到它。

Then, you will need to have 'Ad Hoc Distributed Queries' enabled.然后,您需要启用“Ad Hoc Distributed Queries”。 You can check it by running:您可以通过运行来检查它:

EXEC sys.sp_configure 'Ad Hoc Distributed Queries';

If above command does not work and you are admin, then you will need to run below commands:如果上述命令不起作用并且您是管理员,则需要运行以下命令:

USE [master];
EXEC sys.sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sys.sp_configure 'Ad Hoc Distributed Queries', 1;
RECONFIGURE;

It is also good to check some more settings:检查更多设置也很好:

USE master;
EXEC dbo.sp_MSset_oledb_prop;

See allow_in_process and dynamic_parameters values for Microsoft.ACE.OLEDB.16.0 on this example.请参阅本示例中 Microsoft.ACE.OLEDB.16.0 的 allow_in_process 和 dynamic_parameters 值。 If they are 0, you will need to enable them as well:如果它们是 0,您还需要启用它们:

USE master;
EXEC dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.16.0', N'AllowInProcess', 1;
EXEC dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.16.0', N'DynamicParameters', 1;

After all above checks, be sure the user which is running SSMS has read permissions for the xlsx file you want to read.完成上述所有检查后,请确保运行 SSMS 的用户对要读取的 xlsx 文件具有读取权限。

Finally, you should be able to run a query like below with no issues:最后,您应该能够毫无问题地运行如下查询:

DECLARE @parameter NVARCHAR(50);
SELECT @parameter = F2
    FROM OPENROWSET(
        'Microsoft.ACE.OLEDB.16.0',
        'Excel 8.0;HDR=NO;Database=D:\temp\Survey.xlsx',
        'select * from [sheet1$]') AS xlsx
    WHERE xlsx.F1 = 'SurveyCode';

And you can use the @parameter value as part of your query:您可以使用 @parameter 值作为查询的一部分:

...NOT LIKE '%'+@parameter+'%'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM